researchgate / phpnsc

Small tool to check for missing use statements when using PHP namespaces
MIT License
43 stars 4 forks source link

False positive - Class pending was referenced relatively but not defined #12

Open phpdave11 opened 5 years ago

phpdave11 commented 5 years ago

I ran into an issue where phpnsc incorrectly detects a string as being a class.

Code:

<?php

namespace App\Service;

use App\Entity\Order;
use RuntimeException;

class TestService
{
    public function updateOrderStatus(Order $order)
    {
        $class = '\\App\Importer\\' . ucfirst($order->getVendor()->getName());
        if ($order->getStatus() != 'new') {
            throw new RuntimeException("Cannot update order");
        }
        $order->setClass($class);
        $order->setStatus('pending');
    }
}

Here's my phpnsc.json:

{
   "vendor" : "App",
   "folders" : {
       "root"    : "./src/",
       "include" : [""],
       "exclude" : []
   },
   "filetypes" : {
       "include" : [".php"],
       "exclude" : [".config.php"]
   },
   "output" : [{
       "class": "rg\\tools\\phpnsc\\CheckstyleOutput",
       "parameter": "/tmp/phpnsc.xml"
   },{
       "class": "rg\\tools\\phpnsc\\ConsoleOutput",
       "parameter": ""
   }]
}

Result:

$ vendor/rg/phpnsc/phpnsc run phpnsc.json 
Got 62 files
Collect entities...
............................................................ 60/62
.. 62/62
Got 60 defined entities
Check namespaces...
............................................................ 60/62
.. 62/62
Errors found:

/home/dave-barnes/test-project/src/Service/TestService.php (17): Class pending was referenced relatively but not defined
Time: 76 ms, Memory: 8.00MB
edlerd commented 5 years ago

As a workaround you can rename the $class variable to $foo. A proper solution is still pending to be released.

phpdave11 commented 5 years ago

Thanks! I will use a different variable name instead of $class to fix this issue.

smilesrg commented 5 years ago

I have the same troubles with the code:

$message = $this->trans('That password reset link is incorrect. Please request a new password reset link.');

I got an error

Class password was referenced relatively but not defined

UPD: it seems it is caused by #1