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
False positives when using method_exists()
with a static method
#1267
Comments
I've been thinking about this and I can't think of any use case for this - why isn't the method defined on |
It's a simple use-case where the method is optional, and when defined,
provides additional functionality.
On Jul 22, 2018 1:58 PM, "Ondřej Mirtes" <notifications@github.com> wrote:
I've been thinking about this and I can't think of any use case for this -
why isn't the method defined on Greetings? I don't think you can define it
dynamically.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#1267 (comment)>,
or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ABun28WJRY2raPZL-4K4Kja8DkKX2sIIks5uJL1SgaJpZM4VZ75s>
.
|
How can you optionally define a static method?
On Sun, 22 Jul 2018 at 20:04, Joseph Zidell <notifications@github.com>
wrote:
It's a simple use-case where the method is optional, and when defined,
provides additional functionality.
On Jul 22, 2018 1:58 PM, "Ondřej Mirtes" ***@***.***> wrote:
I've been thinking about this and I can't think of any use case for this -
why isn't the method defined on Greetings? I don't think you can define it
dynamically.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#1267 (comment)>,
or mute
the thread
<
https://github.com/notifications/unsubscribe-auth/ABun28WJRY2raPZL-4K4Kja8DkKX2sIIks5uJL1SgaJpZM4VZ75s
>
.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#1267 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAGZuALqdzQWlyybCYPL4eKzsblt7Mqrks5uJL6XgaJpZM4VZ75s>
.
--
Ondřej Mirtes
|
Models in an MVC framework. A model can define this method, allowing special functionality. Any design pattern, really. |
Can you show me some example code? I don' see how you can optionally define a static method. The method either exists or it doesn't |
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!!';
}
} |
I see. The example with |
Any update on this? |
This could also happen in a trait. if (method_exists(__CLASS__, 'getConstants')) {
$statuses = self::getConstants('STATUS_');
} |
Any update? protected function assertInitializeExists(string $widgetClassName): void
{
if (!method_exists($widgetClassName, 'initialize')) { ... }
} |
@dereuromark Can you post full example to phpstan.org? This one does not yield any errors. |
Maybe because of missing doc block? I will try to provide one later. |
/**
* @param string $widgetClassName
*
* @return void
*/
protected function assertInitializeExists(string $widgetClassName)
{
if (!method_exists($widgetClassName, 'initialize')) {
throw new \Exception(sprintf(
'Widget %s needs to define and implement custom initialize() method with its custom widget input parameters.',
$widgetClassName
));
}
} is the exact method throwing that error
And PHP docs state it can be object or fqcn class name string, and this code works fine :) |
bump |
@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". |
Hard to decide! |
Similar issue here: // Call to function method_exists() with string and '__invoke' will always evaluate to false.
if (\method_exists($this->name, '__invoke')) {
} |
There has been a fix of the But PHPStan will still complain about |
I've moved on from PHP and am not following this anymore. @ondrejmirtes, should we keep this open for others? |
Yes, please keep this open, a bug is still a bug. |
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 :D 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 |
@keichinger Your problem is unrelated, please open a separate issue. |
Exact same use case here. Ive got an abstract class /**
* Class - ObjectType
*/
abstract class ObjectType {
...
protected static function get_type_config() : array {
$config = parent::get_type_config();
$config['fields'] = static::get_fields();
if ( method_exists( static::class, 'get_connections' ) ) { // children that implement TypeWithConnections.php
$config['connections'] = static::get_connections();
}
if ( method_exists( static::class, 'get_interfaces' ) ) { // children that implement TypeWithInterfaces.php
$config['interfaces'] = static::get_interfaces();
}
return $config;
}
}
/**
* Class - Post
*/
class Post extends ObjectType implements TypeWithConnections {
...
public static function get_interfaces() : array {
return [ 'some config array' ];
}
} |
I've also a similar problem related to a BC layer needed in our library to support multiple versions of symfony. Although it's not related to static methods in this case |
@acrobat This makes sense: |
Hmm good point actually, nevermind 😅 |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
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
The text was updated successfully, but these errors were encountered: