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

Fatal error: Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: Undefined property #392

Open
cnizzardini opened this issue Apr 3, 2024 · 2 comments

Comments

@cnizzardini
Copy link

cnizzardini commented Apr 3, 2024

Ran phpcbf --colors -p src/ tests/ on my CakePHP 5 project:

Fatal error: Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: Undefined property: PHPStan\PhpDocParser\Ast\PhpDoc\TypelessParamTagValueNode::$type in /srv/app/vendor/cakephp/cakephp-codesniffer/CakePHP/Sniffs/Commenting/TypeHintSniff.php on line 104 in /srv/app/vendor/squizlabs/php_codesniffer/src/Runner.php:623
Stack trace:
#0 /srv/app/vendor/cakephp/cakephp-codesniffer/CakePHP/Sniffs/Commenting/TypeHintSniff.php(104): PHP_CodeSniffer\Runner->handleErrors(2, 'Undefined prope...', '/srv/app/vendor...', 104)
#1 /srv/app/vendor/squizlabs/php_codesniffer/src/Files/File.php(519): CakePHP\Sniffs\Commenting\TypeHintSniff->process(Object(PHP_CodeSniffer\Files\LocalFile), 103)
#2 /srv/app/vendor/squizlabs/php_codesniffer/src/Files/LocalFile.php(92): PHP_CodeSniffer\Files\File->process()
#3 /srv/app/vendor/squizlabs/php_codesniffer/src/Fixer.php(175): PHP_CodeSniffer\Files\LocalFile->process()
#4 /srv/app/vendor/squizlabs/php_codesniffer/src/Reports/Cbf.php(52): PHP_CodeSniffer\Fixer->fixFile()
#5 /srv/app/vendor/squizlabs/php_codesniffer/src/Reporter.php(285): PHP_CodeSniffer\Reports\Cbf->generateFileReport(Array, Object(PHP_CodeSniffer\Files\LocalFile), false, 190)
#6 /srv/app/vendor/squizlabs/php_codesniffer/src/Runner.php(706): PHP_CodeSniffer\Reporter->cacheFileReport(Object(PHP_CodeSniffer\Files\LocalFile))
#7 /srv/app/vendor/squizlabs/php_codesniffer/src/Runner.php(453): PHP_CodeSniffer\Runner->processFile(Object(PHP_CodeSniffer\Files\LocalFile))
#8 /srv/app/vendor/squizlabs/php_codesniffer/src/Runner.php(215): PHP_CodeSniffer\Runner->run()
#9 /srv/app/vendor/squizlabs/php_codesniffer/bin/phpcbf(14): PHP_CodeSniffer\Runner->runPHPCBF()
#10 /srv/app/vendor/bin/phpcbf(119): include('/srv/app/vendor...')
#11 {main}
  thrown in /srv/app/vendor/squizlabs/php_codesniffer/src/Runner.php on line 623

After running in verbose mode I identified it was having issues with this class:

<?php
declare(strict_types=1);

namespace App\Model\Behavior;

use App\Exception\InvalidSearchValueException;
use App\Model\Filter\FilterValidationInterface;
use ArrayObject;
use Cake\Event\EventInterface;
use Cake\ORM\Behavior;
use Cake\ORM\Query;

/**
 * Runs validations of friendsofcake/search when this behavior is applied on the Table class.
 */
class FilterRequestValidatorBehavior extends Behavior
{
    /**
     * Runs validations on FilterCollections implementing FilterValidationInterface
     *
     * @param EventInterface $event
     * @param Query $query
     * @param ArrayObject $options
     * @param $primary
     * @return void
     */
    public function beforeFind(EventInterface $event, Query $query, ArrayObject $options, $primary): void
    {
        $searchParams = $options['search'] ?? null;
        if (!$this->table()->hasBehavior('Search') || !is_array($searchParams)) {
            return;
        }

        /** @var \Search\Model\Behavior\SearchBehavior $searchBehavior */
        $searchBehavior = $this->table()->getBehavior('Search');
        $filterCollection = $searchBehavior->searchManager()->getFilters();
        if (!$filterCollection instanceof FilterValidationInterface) {
            return;
        }

        $result = $filterCollection->getValidator()->validate($searchParams);
        if (!empty($result)) {
            $field = key($result);
            $error = reset($result);
            $type = key($error);
            $msg = $error[$type];
            throw new InvalidSearchValueException("Search parameter value for `$field` is invalid. $msg");
        }
    }
}

Removing the comments above beforeFind seems to have resolved?

Versions:

/srv/app $ composer show -i | grep sniff
You are using the deprecated option "installed". Only installed packages are shown by default now. The --all option can be used to show all packages.
cakephp/cakephp-codesniffer                    5.1.1   CakePHP CodeSniffer Standards
dealerdirect/phpcodesniffer-composer-installer v1.0.0  PHP_CodeSniffer Standards Composer Installer Plugin
slevomat/coding-standard                       8.15.0  Slevomat Coding Standard for PHP_CodeSniffer complements Consistence Coding Standard by providing sniffs with additional checks.
squizlabs/php_codesniffer                      3.9.1   PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.
@dereuromark
Copy link
Member

@param $primary is missing the type

But it would still be nice if the sniffer wouldn't fail that hard in this case.

@dereuromark
Copy link
Member

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

No branches or pull requests

2 participants