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
"Unexpected end of token stream" with nested if in alternative PHP syntax #892
Comments
Hello 👋 properly formatted statements in PHP should end with It's sure we can better handle this syntax and eventually replace it with a non-fatal error like Always having a semi-colon at the end of a statement can save you trouble. See this example: <?php foo() ?><?php ($a == 1) ? 'ok' : 'KO' ?> Some smart dev comes here and want to cleanup the template file removing all But there in the example above, it's not heuristic and it won't neither gives you a syntax error as the result would be valid code: <?php foo()($a == 1) ? 'ok' : 'KO' ?> Let's say So I advice to put |
@kylekatarnls As much as I do stand with You for I would appreciate if PHPMD contained a rule to find those places where |
As I said, we should warn with I'm pretty aware the code is valid for PHP. But everything PHP allow you to do is not the wisest or safest choice, that's actually the whole point of using code smell detector like PHPMD. This may explain why we never had request to support this syntax before. PHPMD has its own parser: PDepend, so syntax rules have to be explicitly implemented in it to be supported. We would gladly merge a PR for this if anyone wants to give it a try. |
@kylekatarnls Hello Kyle, I would like to help in this issue, can you help me giving detailing of what we need to do in this issue, please? |
Hello @rafael-neris thanks for investigating this. I prepared a branch you can clone with a test that reproduce this issue: https://github.com/pdepend/pdepend/tree/fix/optional-semi-colon-before-php-end-tag After installing dependencies locally with
( It will gives the full stack trace of the error:
From that stack, I think if ($this->tokenizer->peek() === Tokens::T_SEMICOLON) {
$this->consumeToken(Tokens::T_SEMICOLON);
} else {
$this->parseNonePhpCode();
} Here we should also check if next token is a Thanks. |
Hello @rafael-neris, I started a fix on #553 (still the same branch: fix/optional-semi-colon-before-php-end-tag) and it might be more difficult than I expected (it broke other tests) but you can still work on it if you want, I would close my PR if you create a new one that pass tests. |
Current Behavior
phpmd stops with
Unexpected end of token stream in file ...
Expected Behavior
Successfully parses the file
Steps To Reproduce:
Create a file
test.php
with this valid PHP code:Run
vendor/bin/phpmd test.php text ruleset.xml
(format and ruleset don't matter). If I add another empty<?php ?>
to the end of the file, the file is checked as expected. It doesn't matter if there is any real condition in the if clauses or if there is other PHP code inside the conditional branches.Checks before submitting
The text was updated successfully, but these errors were encountered: