DeveloperKid
DeveloperKid

Reputation: 112

How to detect non existing class in use statement

How can I detect than class NonExistingClass does not exist?

I'm currently checking out features from PhpStan, Phan, Composer but nothing so far.

A developer removes code that calls NonExistingClass in class A and removes whole class NonExistingClass but does not remove use statement in class A:

<?php

namespace App\Service;

use App\Model\NonExistingClass;

class A
{
}

How to detect this scenario during static analysis?

Upvotes: 0

Views: 946

Answers (1)

Ondřej Mirtes
Ondřej Mirtes

Reputation: 5616

You can't detect nonexistent class in use statement because it might be a namespace.

What you can do is to detect unused uses in a file which is actually the case in your code example. Check out SlevomatCodingStandard.Namespaces.UnusedUses sniff for PHP_CodeSniffer in slevomat/coding-standard.

If the nonexistent class in a use statement is actually used in the file, PHPStan will tell you. See an example here: https://phpstan.org/r/1f05520e-88fe-4a61-9d05-018b466e640d

Upvotes: 4

Related Questions