phpmd icon indicating copy to clipboard operation
phpmd copied to clipboard

PHP short tag check

Open johnwc opened this issue 5 years ago • 4 comments

Does PHPMD have a check for php files that are using short tag form? <? instead of <?php

johnwc avatar May 07 '20 06:05 johnwc

Hi @johnwc, no it doesn't. Feel free to propose a pull-request for this.

kylekatarnls avatar May 07 '20 06:05 kylekatarnls

@kylekatarnls I see this is marked "Good first issue" though I'm having trouble finding any metric or even getting a custom rule's apply invoked. Do I need to fork PDepend to create an 'aware' and/or metric for opening short tags?

Any other info or pointers on how to begin would be appreciated.

paulrrogers avatar Oct 04 '20 02:10 paulrrogers

Hello,

Indeed, PDepend needs to support it first. It contains a src/main/php/PDepend/Source/Language/PHP/PHPTokenizerInternal.php file who run token_get_all($source) inside the tokenize() method. token_get_all return following tokens that concern this issue:

  • T_INLINE_HTML for <?
  • T_OPEN_TAG for <?php
  • T_OPEN_TAG_WITH_ECHO for <?=

And it contains:

$source = preg_replace(
    array('(<\?=)', '(<\?(\s))'),
    array('<?php echo ', '<?php\1'),
    $source
);

This replacement (which is not really needed) should be removed so we can detect each token in src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php in the parse() method.

In the switch Tokens::T_INLINE_HTML will need to be added in the same case than Tokens::T_OPEN_TAG. In this case and the Tokens::T_CLOSE_TAG one (or until Tokenizer::T_EOF) we could record in an array the list PHP code chunks and the informations for start and end of each: file, line, token type, so PHPMD can use this list to raise rule violations if the type is forbidden.

kylekatarnls avatar Oct 04 '20 09:10 kylekatarnls

@kylekatarnls I've prepared pdepend/pdepend#490 though not sure where the docs are within that repo in order to update them. Do you have any insight on where I should look?

paulrrogers avatar Oct 08 '20 03:10 paulrrogers