Skip to content

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

@phpdave11

Description

@phpdave11

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions