adamwojs / php-cs-fixer-phpdoc-force-fqcn

php-cs-fixer rule to force using FQCN in DocBlock comments

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error when running the fixer on non-namespaced files

svenluijten opened this issue · comments

I'm running into an issue when using this fixer. This doesn't happen on all files, an example file where this happens is included at the bottom of the issue. The error I'm getting:

[TypeError]
Argument 1 passed to AdamWojs\PhpCsFixerPhpdocForceFQCN\Fixer\Phpdoc\ForceFQCNFixer::fixAnnotation() must 
be an instance of AdamWojs\PhpCsFixerPhpdocForceFQCN\Analyzer\NamespaceInfo, null given, 
called in <path>/vendor/adamwojs/php-cs-fixer-phpdoc-force-fqcn/src/Fixer/Phpdoc/ForceFQCNFixer.php on line 85

      AdamWojs\PhpCsFixerPhpdocForceFQCN\Fixer\Phpdoc\ForceFQCNFixer->fixAnnotation()
        in <path>/vendor/adamwojs/php-cs-fixer-phpdoc-force-fqcn/src/Fixer/Phpdoc/ForceFQCNFixer.php at line 85
      AdamWojs\PhpCsFixerPhpdocForceFQCN\Fixer\Phpdoc\ForceFQCNFixer->fix()
        in <path>/vendor/friendsofphp/php-cs-fixer/src/Runner/Runner.php at line 190
      PhpCsFixer\Runner\Runner->fixFile()
        in <path>/vendor/friendsofphp/php-cs-fixer/src/Runner/Runner.php at line 132
      PhpCsFixer\Runner\Runner->fix()
        in <path>/vendor/friendsofphp/php-cs-fixer/src/Console/Command/FixCommand.php at line 220
      PhpCsFixer\Console\Command\FixCommand->execute()
        in <path>/vendor/symfony/console/Command/Command.php at line 255
      [ ... ]

I've looked around and this is due to the fact that the $currentNamespace variable can be null here. Changing that foreach to include a null-check on that $currentNamespace variable seems to fix my issue:

foreach ($annotations as $annotation) {
    if ($currentNamespace === null) {
        continue;
    }

    $this->fixAnnotation($currentNamespace, $annotation);
}

I've included this fix in #4. However, I'm not very familiar with the inner workings of this package (or custom fixers for PHP-CS-Fixer in general), so I'll let you decide if you want to fix it that way 🙂

Here is the file & config that's causing the issue for me.

Edit: This seems to only be an issue with files that don't have a namespace declaration at the top of the file!

Thanks for taking a look!

Hi @svenluijten! Thank you for a detailed bug report and PR. I will take a look on that ;-)

#4 is a valid fix for the case when there is no namespace declaration and no use statements in the file.

However, there is the second case that needs to take into account: no namespace declaration but with some use statements. The code snippet is worth more than 1000 words, so let me illustrate it:

For the following snippet:

<?php

use App\Foo;

/**
 * @param Foo $foo
 */
function bar(Foo $foo): void 
{
    // do something
}

as a user, I expect the following results:

<?php

use App\Foo;

/**
 * @param \App\Foo $foo
 */
function bar(Foo $foo): void 
{
    // do something
}

So the patch will more or less look like https://github.com/adamwojs/php-cs-fixer-phpdoc-force-fqcn/compare/issue_3 (It seems to work as expected but I need to test it on real-life projects). Stay tuned! 😉

Nice, that does look like it solves the issues I was seeing!