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

PHPMD fails to parse php files where trailing comma is used in multiline function or method arguments. #889

Closed
6 tasks done
archfz opened this issue May 11, 2021 · 8 comments

Comments

@archfz
Copy link

archfz commented May 11, 2021

  • PHPMD version: 2.10.0
  • PHP Version: 8.0.5
  • Installation type: composer
  • Operating System / Distribution & Version: Ubuntu 20.04

Current Behavior

When using trailing comma in multi-line function / method arguments, phpmd fails with following message:

ERROR while parsing 
--------------------
Unexpected token: ), line: 25, col: 5, file:

Example function:

function myFunc(
  $a,
  $b,
) {}

Expected Behavior

It should not fail as this is supported since php 7.4.

Steps To Reproduce:

Explain all the steps you did to create this bug so we can reproduce it.

Checks before submitting

  • Be sure that there isn't already an issue about this. See: Issues list
  • Be sure that there isn't already a pull request about this. See: Pull requests
  • I have added every step to reproduce the bug.
  • If possible I added relevant code examples.
  • This issue is about 1 bug and nothing more.
  • The issue has a descriptive title. For example: "JSON rendering failed on Windows for filenames with space".
@JBx0
Copy link

JBx0 commented May 26, 2021

same here:

phpmd 2.10.1
pdepend 2.9.1
php 8.0.6

/phpmd(61): PHPMD\TextUI\Command::main(Array)
#24 {main}
        -       Unexpected token: ), line: 26, col: 5, file: foo.php
    public function __construct(
        private foo $foo,
        private bar $bar,
        private foobar $foobar,
    ) {
    }

Reproduction:
create a class with a constructor with a trailing comma and run phpmd

@tvbeek
Copy link
Member

tvbeek commented May 26, 2021

@JBx0 thanks for your comment, as you can see this issue is still open. If you have the possibility you can create a PR with a fix, else we need to wait till someone else has the time to fix this. 😃

@JBx0
Copy link

JBx0 commented May 26, 2021

@tvbeek xD yes I can see, but those are new versions I referenced ;)

@tvbeek
Copy link
Member

tvbeek commented May 26, 2021

@JBx0 as long as the issue is open it will not be changed in new versions :)

@JBx0
Copy link

JBx0 commented May 26, 2021

@tvbeek I'd need some assistance for that since I have no clue of the codebase!
But I'll check it out could u point me to the right classes?
Trailing comma and false positive promoted properties I'd take a look at.

@dshoreman
Copy link

dshoreman commented May 27, 2021

@JBx0 This bug is more likely upstream rather than in PHPMD. See this stacktrace:

 Error: Unexpected token: ), line: 26, col: 5, file: ...
#0 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(7322): PDepend\Source\Parser\UnexpectedTokenException->__construct()
#1 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(7288): PDepend\Source\Language\PHP\AbstractPHPParser->getUnexpectedTokenException()
#2 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(6757): PDepend\Source\Language\PHP\AbstractPHPParser->consumeToken()
#3 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/PHPParserVersion70.php(505): PDepend\Source\Language\PHP\AbstractPHPParser->parseVariableDeclarator()
#4 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(5674): PDepend\Source\Language\PHP\PHPParserVersion70->parseFormalParameter()
#5 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/PHPParserVersion71.php(119): PDepend\Source\Language\PHP\AbstractPHPParser->parseFormalParameterOrTypeHintOrByReference()
#6 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(5578): PDepend\Source\Language\PHP\PHPParserVersion71->parseFormalParameterOrTypeHintOrByReference()
#7 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/PHPParserVersion80.php(180): PDepend\Source\Language\PHP\AbstractPHPParser->parseFormalParameterOrPrefix()
#8 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(5611): PDepend\Source\Language\PHP\PHPParserVersion80->parseFormalParameterOrPrefix()
#9 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(1273): PDepend\Source\Language\PHP\AbstractPHPParser->parseFormalParameters()
#10 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(1231): PDepend\Source\Language\PHP\AbstractPHPParser->parseCallableDeclaration()
#11 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(988): PDepend\Source\Language\PHP\AbstractPHPParser->parseMethodDeclaration()
#12 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/PHPParserVersion74.php(87): PDepend\Source\Language\PHP\AbstractPHPParser->parseMethodOrFieldDeclaration()
#13 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(887): PDepend\Source\Language\PHP\PHPParserVersion74->parseMethodOrFieldDeclaration()
#14 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(723): PDepend\Source\Language\PHP\AbstractPHPParser->parseTypeBody()
#15 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(6075): PDepend\Source\Language\PHP\AbstractPHPParser->parseClassDeclaration()
#16 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(397): PDepend\Source\Language\PHP\AbstractPHPParser->parseOptionalStatement()
#17 ./vendor/pdepend/pdepend/src/main/php/PDepend/Engine.php(587): PDepend\Source\Language\PHP\AbstractPHPParser->parse()
#18 ./vendor/pdepend/pdepend/src/main/php/PDepend/Engine.php(335): PDepend\Engine->performParseProcess()
#19 ./vendor/phpmd/phpmd/src/main/php/PHPMD/Parser.php(95): PDepend\Engine->analyze()
#20 ./vendor/phpmd/phpmd/src/main/php/PHPMD/PHPMD.php(240): PHPMD\Parser->parse()
#21 ./vendor/phpmd/phpmd/src/main/php/PHPMD/TextUI/Command.php(136): PHPMD\PHPMD->processFiles()
#22 ./vendor/phpmd/phpmd/src/main/php/PHPMD/TextUI/Command.php(184): PHPMD\TextUI\Command->run()
#23 ./vendor/phpmd/phpmd/src/bin/phpmd(61): PHPMD\TextUI\Command::main()
#24 {main}

It might already be fixed by pdepend/pdepend#534. It was merged to master on May 13th, but it hasn't been tagged in a release yet. Short of depending on 2.x-dev, there's not much we can do here beyond waiting.

For now though, composer require --dev "pdepend/pdepend:2.x-dev" is enough to "fix" it in your own project until next pdepend release.

@JBx0
Copy link

JBx0 commented May 27, 2021

@JBx0 This bug is more likely upstream rather than in PHPMD. See this stacktrace:

 Error: Unexpected token: ), line: 26, col: 5, file: ...
#0 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(7322): PDepend\Source\Parser\UnexpectedTokenException->__construct()
#1 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(7288): PDepend\Source\Language\PHP\AbstractPHPParser->getUnexpectedTokenException()
#2 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(6757): PDepend\Source\Language\PHP\AbstractPHPParser->consumeToken()
#3 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/PHPParserVersion70.php(505): PDepend\Source\Language\PHP\AbstractPHPParser->parseVariableDeclarator()
#4 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(5674): PDepend\Source\Language\PHP\PHPParserVersion70->parseFormalParameter()
#5 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/PHPParserVersion71.php(119): PDepend\Source\Language\PHP\AbstractPHPParser->parseFormalParameterOrTypeHintOrByReference()
#6 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(5578): PDepend\Source\Language\PHP\PHPParserVersion71->parseFormalParameterOrTypeHintOrByReference()
#7 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/PHPParserVersion80.php(180): PDepend\Source\Language\PHP\AbstractPHPParser->parseFormalParameterOrPrefix()
#8 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(5611): PDepend\Source\Language\PHP\PHPParserVersion80->parseFormalParameterOrPrefix()
#9 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(1273): PDepend\Source\Language\PHP\AbstractPHPParser->parseFormalParameters()
#10 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(1231): PDepend\Source\Language\PHP\AbstractPHPParser->parseCallableDeclaration()
#11 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(988): PDepend\Source\Language\PHP\AbstractPHPParser->parseMethodDeclaration()
#12 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/PHPParserVersion74.php(87): PDepend\Source\Language\PHP\AbstractPHPParser->parseMethodOrFieldDeclaration()
#13 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(887): PDepend\Source\Language\PHP\PHPParserVersion74->parseMethodOrFieldDeclaration()
#14 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(723): PDepend\Source\Language\PHP\AbstractPHPParser->parseTypeBody()
#15 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(6075): PDepend\Source\Language\PHP\AbstractPHPParser->parseClassDeclaration()
#16 ./vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(397): PDepend\Source\Language\PHP\AbstractPHPParser->parseOptionalStatement()
#17 ./vendor/pdepend/pdepend/src/main/php/PDepend/Engine.php(587): PDepend\Source\Language\PHP\AbstractPHPParser->parse()
#18 ./vendor/pdepend/pdepend/src/main/php/PDepend/Engine.php(335): PDepend\Engine->performParseProcess()
#19 ./vendor/phpmd/phpmd/src/main/php/PHPMD/Parser.php(95): PDepend\Engine->analyze()
#20 ./vendor/phpmd/phpmd/src/main/php/PHPMD/PHPMD.php(240): PHPMD\Parser->parse()
#21 ./vendor/phpmd/phpmd/src/main/php/PHPMD/TextUI/Command.php(136): PHPMD\PHPMD->processFiles()
#22 ./vendor/phpmd/phpmd/src/main/php/PHPMD/TextUI/Command.php(184): PHPMD\TextUI\Command->run()
#23 ./vendor/phpmd/phpmd/src/bin/phpmd(61): PHPMD\TextUI\Command::main()
#24 {main}

It might already be fixed by pdepend/pdepend#534. It was merged to master on May 13th, but it hasn't been tagged in a release yet. Short of depending on 2.x-dev, there's not much we can do here beyond waiting.

For now though, composer require --dev "pdepend/pdepend:2.x-dev" is enough to "fix" it in your own project until next pdepend release.

Works for trailing comma! Thx
Avoid unused params with promoted properties doesn't work tho any idea where that could come from ? @dshoreman

@kylekatarnls
Copy link
Member

You can update to last pdepend/pdepend now to get it fixed, phar version of 2.10.2 that will have it will be released this week too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

6 participants