squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.67k stars 1.48k forks source link

Fatal error when a method has "@param $code" in DocBlock for Doctrine phpcs standard #3646

Closed kirilcvetkov closed 2 years ago

kirilcvetkov commented 2 years ago

Describe the bug Fatal error when a method has a line in DocBlock "@param $code" (Doctrine standard).

Fatal error: Uncaught TypeError: SlevomatCodingStandard\Helpers\Annotation\ParameterAnnotation::__construct(): Argument #5 ($contentNode) must be of type ?PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode, PHPStan\PhpDocParser\Ast\PhpDoc\TypelessParamTagValueNode given, called in /System/Volumes/Data/Users/kirilcvetkov/.composer/vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/AnnotationHelper.php on line 357 and defined in /System/Volumes/Data/Users/kirilcvetkov/.composer/vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/Annotation/ParameterAnnotation.php:31 Stack trace:

0 /System/Volumes/Data/Users/kirilcvetkov/.composer/vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/AnnotationHelper.php(357): SlevomatCodingStandard\Helpers\Annotation\ParameterAnnotation->__construct('@param', 14, 16, '$code', Object(PHPStan\PhpDocParser\Ast\PhpDoc\TypelessParamTagValueNode))

1 /System/Volumes/Data/Users/kirilcvetkov/.composer/vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/SniffLocalCache.php(42): SlevomatCodingStandard\Helpers\AnnotationHelper::SlevomatCodingStandard\Helpers{closure}()

2 /System/Volumes/Data/Users/kirilcvetkov/.composer/vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/SniffLocalCache.php(31): SlevomatCodingStandard\Helpers\SniffLocalCache::setIfNotCached(0, '/Users/kirilcve...', Object(Closure))

3 /System/Volumes/Data/Users/kirilcvetkov/.composer/vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/AnnotationHelper.php(372): SlevomatCodingStandard\Helpers\SniffLocalCache::getAndSetIfNotCached(Object(PHP_CodeSniffer\Files\LocalFile), 'annotations-9', Object(Closure))

4 /Users/kirilcvetkov/.composer/vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesOnlySniff.php(680): SlevomatCodingStandard\Helpers\AnnotationHelper::getAnnotations(Object(PHP_CodeSniffer\Files\LocalFile), 9)

5 /Users/kirilcvetkov/.composer/vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Namespaces/ReferenceUsedNamesOnlySniff.php(145): SlevomatCodingStandard\Sniffs\Namespaces\ReferenceUsedNamesOnlySniff->getReferences(Object(PHP_CodeSniffer\Files\LocalFile), 0)

6 /System/Volumes/Data/Users/kirilcvetkov/.composer/vendor/squizlabs/php_codesniffer/src/Files/File.php(498): SlevomatCodingStandard\Sniffs\Namespaces\ReferenceUsedNamesOnlySniff->process(Object(PHP_CodeSniffer\Files\LocalFile), 0)

7 /System/Volumes/Data/Users/kirilcvetkov/.composer/vendor/squizlabs/php_codesniffer/src/Files/LocalFile.php(92): PHP_CodeSniffer\Files\File->process()

8 /System/Volumes/Data/Users/kirilcvetkov/.composer/vendor/squizlabs/php_codesniffer/src/Runner.php(628): PHP_CodeSniffer\Files\LocalFile->process()

9 /System/Volumes/Data/Users/kirilcvetkov/.composer/vendor/squizlabs/php_codesniffer/src/Runner.php(434): PHP_CodeSniffer\Runner->processFile(Object(PHP_CodeSniffer\Files\LocalFile))

10 /System/Volumes/Data/Users/kirilcvetkov/.composer/vendor/squizlabs/php_codesniffer/src/Runner.php(114): PHP_CodeSniffer\Runner->run()

11 /System/Volumes/Data/Users/kirilcvetkov/.composer/vendor/squizlabs/php_codesniffer/bin/phpcs(18): PHP_CodeSniffer\Runner->runPHPCS()

12 /System/Volumes/Data/Users/kirilcvetkov/.composer/vendor/bin/phpcs(120): include('/System/Volumes...')

13 {main}

thrown in /System/Volumes/Data/Users/kirilcvetkov/.composer/vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/Annotation/ParameterAnnotation.php on line 31

Code sample

        /**
         * @param $code
         *
         * @return bool
         */
        private function interpretStatus($code)
        {
            switch ($code) {
                case '1':
                case '2':
                case '3':
                case '4':
                case '6':
                    return '1';
                case '7':
                case '9':
                    return '0';
            }

            return false;
        }

Custom ruleset

To reproduce Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs --standard=Doctrine test.php ...
  3. See error message displayed
    PHPCS output here

Expected behavior I'd expect to receive feedback on the sniffed file.

Versions (please complete the following information):

Additional context Add any other context about the problem here.

jrfnl commented 2 years ago

@kirilcvetkov Based on the backtrace, this is an issue in the SlevomatCodingStandard, not in PHPCS. Please report it to the Slevomat repo.

kirilcvetkov commented 2 years ago

Ah, I see. Thank you.