Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FR] Detect use of deprecated class/object methods #1300

Open
sarfrazeteam opened this issue Sep 12, 2021 · 4 comments
Open

[FR] Detect use of deprecated class/object methods #1300

sarfrazeteam opened this issue Sep 12, 2021 · 4 comments

Comments

@sarfrazeteam
Copy link

sarfrazeteam commented Sep 12, 2021

I am trying to check for errors via phpcs and PHPCompatibility standard on a laravel project, here is the command:

phpcs -psvw app --extensions=php --standard=PHPCompatibility --runtime-set testVersion 8.0

The command works fine. However, I am supposed to receive following error but I am not:

Deprecated: Method ReflectionParameter::getClass() is deprecated in somefile.php on line 838

The actual code where PHP8 fails but PHPCS does not inform:

$results[] = is_null($dependency->getClass()) ? $this->resolvePrimitive($dependency) : $this->resolveClass($dependency);

The reason PHPCompatibility standard should tell me about this error is that when I try to run my app in PHP 8 I get above error, however PHPCS with PHPCompatibility does not report above error.

Any idea how can I make PHPCompatibility report above error ?

@jrfnl
Copy link
Member

jrfnl commented Sep 12, 2021

Any idea how can I make PHPCompatibility report above error ?

@sarfrazeteam Well, the simple answer is: by writing a sniff to detect deprecated and removed class methods.
Such a sniff does not currently exist in PHPCompatibility.

The reason such a sniff does not exist is that for accurate detection of methods on classes, we'd need a way to resolve an object variable to its class and as the code is tokenized, not run, that's nowhere near as easy as it sounds.

Some code samples of things such a sniff would need to be able to handle:

$obj = new ReflectionParameter();
$obj->getClass();

class Foo extends ReflectionParameter {
    function bar() {
        $this->getClass();
    }
}

function foo(ReflectionParameter $obj) {
    $obj->getClass();
}

Note: these code samples are still the relatively "easy" ones, i.e. the cases which may be detectable...

@sarfrazeteam
Copy link
Author

So that simply means we cannot rely completely on PHPCompatibility as of now when checking things before upgrading ?

@jrfnl
Copy link
Member

jrfnl commented Sep 12, 2021

So that simply means we cannot rely completely on PHPCompatibility as of now when checking things before upgrading ?

@sarfrazeteam You never could and never will be able to in the future either.
You can rely on PHPCompatibility to detect what it can 1) within the limitation of it being static analysis and 2) based on the available sniffs.
You can see the list of available sniffs via phpcs --standard=PHPCompatibility -e. You can also have a look at the issue list - particularly the PHP version "meta" tickets - to see what things we currently don't or can't detect.

Static analysis tools, no matter how good, are never a replacement for having unit tests. At the same time, unit tests are only as reliable as the tests are good and complete (which in most cases means: not that good, nor complete).

Using both - static analysis AND tests - will give you a good starting point to assess your code's readiness for a new PHP version.

@sarfrazeteam
Copy link
Author

Right, makes sense, thanks for the explanation

@jrfnl jrfnl changed the title phpcs --standard=PHPCompatibility not detecting error [FR] Detect use of deprecated class/object methods May 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants