phpmd icon indicating copy to clipboard operation
phpmd copied to clipboard

PHP 8 `new` with arbitrary expressions leads to a syntax issue

Open Geolim4 opened this issue 3 years ago • 2 comments

  • PHPMD version: 2.12.0
  • PHP Version: 8.0.2
  • Installation type: composer
  • Operating System / Distribution & Version: Not relevant

Current Behavior

This (valid) code:

$myObject = new (SomeArbitraryCode::class . 'loremIpsum' . trim(' Hello '))($arg1, $arg2);

Leads to the following error:

Unexpected token: (, line: xx, col: xx, file: xxxxxxxxxxx.php.
#0 xxxxxxxxxxx\vendor\pdepend\pdepend\src\main\php\PDepend\Source\Language\PHP\AbstractPHPParser.php(7844): PDepend\Source\Parser\UnexpectedTokenException->__construct(Object(PDepend\Source\Tokenizer\Token), Object(PDepend\Source\AST\ASTCompilationUnit)
)

Expected Behavior

No error, dynamic class instantiation has been introduced as of PHP 8: https://www.php.net/manual/en/language.oop5.basic.php

Steps To Reproduce:

Just analyses the piece of code above.

Checks before submitting

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

Geolim4 avatar Apr 25 '22 21:04 Geolim4

@Geolim4 Thanks for your report!

If I check the error this is in fact an error in PDepend. That is why I have created a new issue: https://github.com/pdepend/pdepend/issues/601

tvbeek avatar Apr 26 '22 07:04 tvbeek

Thank you @tvbeek !

Geolim4 avatar Apr 26 '22 08:04 Geolim4