The Wayback Machine - https://web.archive.org/web/20201117214603/https://github.com/phpmd/phpmd/issues/771
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP short tag check #771

Open
johnwc opened this issue May 7, 2020 · 4 comments
Open

PHP short tag check #771

johnwc opened this issue May 7, 2020 · 4 comments

Comments

@johnwc
Copy link

@johnwc johnwc commented May 7, 2020

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

@kylekatarnls
Copy link
Collaborator

@kylekatarnls kylekatarnls commented May 7, 2020

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

@paulrrogers
Copy link

@paulrrogers paulrrogers commented Oct 4, 2020

@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.

@kylekatarnls
Copy link
Collaborator

@kylekatarnls kylekatarnls commented Oct 4, 2020

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.

@paulrrogers
Copy link

@paulrrogers paulrrogers commented Oct 8, 2020

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.