phpnsc
                                
                                 phpnsc copied to clipboard
                                
                                    phpnsc copied to clipboard
                            
                            
                            
                        False positive - Class pending was referenced relatively but not defined
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
As a workaround you can rename the $class variable to $foo. A proper solution is still pending to be released.
Thanks! I will use a different variable name instead of $class to fix this issue.
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