phpstan: False positives when using `method_exists()` with a static method

Summary of a problem or a feature request

When using method_exists() with a static method, it is giving a false positive error. The PHP documentation clearly highlights this as a valid use-case.

Code snippet that reproduces the problem

https://phpstan.org/r/ddb2e9099834cf325a7917bcd50b42e2

Actual output

Call to an undefined static method Greetings::waveGoodbye().

Expected output

Nothing. Should have no errors

Similar to #323

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 2
  • Comments: 27 (21 by maintainers)

Most upvoted comments

@josephzidell What does “bump” mean? Either send a pull request with a fix, or wait. Issue is urgent when a lot of people have the same problem, not just because one guy says “bump”.

Yes, please keep this open, a bug is still a bug.

I’m calling this from the parent class, and checking if the child class defined the method.

class Model
{
	public function checkForExcitingFunctionality()
	{
		if (method_exists(static::class, 'exciting')) {
			static::exciting();
		}
	}
}

// boring class
class ProductModel extends Model
{
}

// exciting class
class ImageModel extends Model
{
	public static function exciting()
	{
		echo 'Ooh hoo!!';
	}
}

@keichinger Your problem is unrelated, please open a separate issue.

I may have found another example where PhpStan is complaining that a certain method doesn’t exist, though my example may be a bit extreme 😅

https://phpstan.org/r/eed4b23c-b6e7-424f-87fa-9f88ac7150e3

In this particular case, I’ve got a very generic Trait that may or may not be used within a class-inheritance. The actual code works flawlessly, it’s just that PhpStan is (rightfully) getting confused here 😄

Without knowing the internals of PhpStan, I can imagine that fixing this particular case may be an extreme case of „This code is making sure that it’s safe to call said method though parent:: doesn’t know about it” 😅

send a pull request with a fix, or wait

Hard to decide!