pdepend / pdepend

PHP_Depend is an adaptation of the established Java development tool JDepend. This tool shows you the quality of your design in terms of extensibility, reusability and maintainability.
https://pdepend.org/
BSD 3-Clause "New" or "Revised" License
924 stars 131 forks source link

Enum with DEFAULT or NEW case result in error. #635

Closed tvbeek closed 1 year ago

tvbeek commented 2 years ago

Current Behavior

When an enum contains a value named DEFAULT or NEW, pdepend crashes.

Expected Behavior

Expected behavior is no error, just a successful analysis.

Steps To Reproduce:

Create a class with the enum and run PDepend on it

Checks before submitting

This is first reported in: https://github.com/phpmd/phpmd/issues/985

guvra commented 2 years ago

Note: it happens with any PHP keyword (e.g. "CLASS", "INTERFACE"...) and is case insensitive (e.g. "CLASS", "Class", "class" all result in a fatal error).

Example test file (test.php):

<?php

enum Test
{
    case CLASS;
}

Running the following command:

phpmd test.php ansi cleancode

Results in:

ERROR while parsing 
--------------------
Unexpected token: use, line: 5, col: 10, file: /var/www/html/test.php.
#0 /var/www/html/vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(7966): PDepend\Source\Parser\UnexpectedTokenException->__construct(Object(PDepend\Source\Tokenizer\Token), Object(PDepend\Source\AST\ASTCompilationUnit))
#1 /var/www/html/vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(8116): PDepend\Source\Language\PHP\AbstractPHPParser->getUnexpectedTokenException()
#2 /var/www/html/vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(1099): PDepend\Source\Language\PHP\AbstractPHPParser->parseEnumCase()
#3 /var/www/html/vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/PHPParserVersion81.php(164): PDepend\Source\Language\PHP\AbstractPHPParser->parseTypeBody(Object(PDepend\Source\AST\ASTEnum))
#4 /var/www/html/vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(6578): PDepend\Source\Language\PHP\PHPParserVersion81->parseEnumDeclaration()
#5 /var/www/html/vendor/pdepend/pdepend/src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php(516): PDepend\Source\Language\PHP\AbstractPHPParser->parseOptionalStatement()
#6 /var/www/html/vendor/pdepend/pdepend/src/main/php/PDepend/Engine.php(606): PDepend\Source\Language\PHP\AbstractPHPParser->parse()
#7 /var/www/html/vendor/pdepend/pdepend/src/main/php/PDepend/Engine.php(351): PDepend\Engine->performParseProcess()
#8 /var/www/html/vendor/phpmd/phpmd/src/main/php/PHPMD/Parser.php(99): PDepend\Engine->analyze()
#9 /var/www/html/vendor/phpmd/phpmd/src/main/php/PHPMD/PHPMD.php(240): PHPMD\Parser->parse(Object(PHPMD\Report))
#10 /var/www/html/vendor/phpmd/phpmd/src/main/php/PHPMD/TextUI/Command.php(136): PHPMD\PHPMD->processFiles('test.php', 'phpmd.xml.dist', Array, Object(PHPMD\RuleSetFactory), Object(PHPMD\Report))
#11 /var/www/html/vendor/phpmd/phpmd/src/main/php/PHPMD/TextUI/Command.php(184): PHPMD\TextUI\Command->run(Object(PHPMD\TextUI\CommandLineOptions), Object(PHPMD\RuleSetFactory))
#12 /var/www/html/vendor/phpmd/phpmd/src/bin/phpmd(61): PHPMD\TextUI\Command::main(Array)
#13 /var/www/html/vendor/bin/phpmd(120): include('/var/www/html/v...')
#14 {main}
ericfortmeyer commented 2 years ago

The error also occurs when the enum's member is named Null.

tvbeek commented 1 year ago

Note: it happens with any PHP keyword (e.g. "CLASS", "INTERFACE"...) and is case insensitive (e.g. "CLASS", "Class", "class" all result in a fatal error).

Example test file (test.php):

<?php

enum Test
{
    case CLASS;
}
php -l test.php
Result in: PHP Fatal error:  A class constant must not be called 'class'; it is reserved for class name fetching in test.php on line 4
Error parsing test.php

That means class isn't allowed.

This are valid names for constants but not yet handled by PDepend.

florianajir commented 1 year ago

I got the same error for enum name CONTINUE

natanfelles commented 1 year ago

Same for parent.