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

phpdoc_to_comment false positive #5119

Closed
fluffycondor opened this issue Aug 20, 2020 · 1 comment
Closed

phpdoc_to_comment false positive #5119

fluffycondor opened this issue Aug 20, 2020 · 1 comment

Comments

@fluffycondor
Copy link

Bug report

Rule phpdoc_to_comment turns phpdoc @var on return statement to two-slash comment.

PHP 7.3.21, PHP CS Fixer 2.16.4 Yellow Bird

The command:
/usr/local/bin/php-cs-fixer fix --verbose --diff --config "$PHP_CS_CONFIG" $CHANGED_FILES;

Config:

<?php

$finder = PhpCsFixer\Finder::create()
    ->in(__DIR__ . DIRECTORY_SEPARATOR . 'src')
    ->notPath('Kernel.php')
;

return PhpCsFixer\Config::create()
    ->setRiskyAllowed(true)
    ->setRules([
        '@PSR2' => true,
        '@Symfony' => true,
        '@PhpCsFixer' => true,
        'declare_strict_types' => true,
        'concat_space' => ['spacing' => 'one'],
        'ordered_class_elements' => false,
        'phpdoc_add_missing_param_annotation' => false,
        'binary_operator_spaces' => [
            'default' => 'single_space',
            'operators' => [
                '=>' => null,
                '=' => null,
            ],
        ],
    ])
    ->setFinder($finder)
;

Code snippet that reproduces the problem

public function findDeleted($id, $lockMode = null, $lockVersion = null): ?Project
{
   /** @var null|Project */ <-- this comment is required to specify concrete object from parent::find,
                           otherwise PHPStan throws an error "should return Project|null but returns object|null"
  return parent::find($id, $lockMode, $lockVersion);
}

PHP-CS-Fixer turns it to

public function findDeleted($id, $lockMode = null, $lockVersion = null): ?Project
{
   // @var null|Project
  return parent::find($id, $lockMode, $lockVersion);
}

If I try to avoid it like this:

public function findDeleted($id, $lockMode = null, $lockVersion = null): ?Project
{
   /** @var null|Project */
   $project = parent::find($id, $lockMode, $lockVersion);

   return $project;
}

The rule return_assignment turns it to

public function findDeleted($id, $lockMode = null, $lockVersion = null): ?Project
{
   /** @var null|Project */
  return parent::find($id, $lockMode, $lockVersion);
}

And on the next run the rule phpdoc_to_comment is coming again:

public function findDeleted($id, $lockMode = null, $lockVersion = null): ?Project
{
   // @var null|Project
  return parent::find($id, $lockMode, $lockVersion);
}

And this kind of comment can't be inspected by PHPStan.

@julienfalque
Copy link
Member

Duplicate of #5109/#4649.

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

No branches or pull requests

2 participants