Comments (4)
Are you sure this is a bug? I think that actually it's a correct warning. See the section titled "When calling static methods, the function call is stronger than the static property operator" in this PHP doc. I actually had to fix this behavior in #27.
In your example, return new self::$defaultClass();
will first evaluate $defaultClass
and then look for a static property on self
which matches that name. However, $defaultClass
is not defined.
Here's a simplified example which you can run to see the failure:
<?php
class Foobar {
private static $name = 'doSomething';
public static function doSomething() {
return 'hi';
}
public static function proxy() {
return self::$name();
}
}
echo Foobar::proxy();
Or view it here: https://repl.it/repls/DefiantCrowdedTabs
If I'm misunderstanding or I'm wrong (always possible!) please feel free to re-open the issue.
from phpcs-variable-analysis.
Hi @sirbrillig and thanks for response, I was trying to change the code after your comment and I discovered this...
Why does my previous code throw a warning and this does not?
<?php
class MyClass extends MyForms
{
private static $defaultClass = 'OtherClass';
final public static function getForm($class = false)
{
$defaultClass = self::$defaultClass;
if ($class !== false) {
return new $class();
}
return new self::$defaultClass();
}
}
from phpcs-variable-analysis.
That example is not an issue, I think, because in that case, $defaultClass
is defined by the time it's used. PHP is weird this way.
In the second example, you're creating a local variable called $defaultClass
, which you're assigning with the value of self::$defaultClass
, which evaluates to the static property and thus has the value of 'OtherClass'
. Then you're returning self::$defaultClass()
. Because the function call is stronger than the static reference, essentially that means that those parenthesis evaluate first, before the self::
part. So self::$defaultClass()
first evaluates $defaultClass()
. In the original code, that was causing an error (and also, the sniff was warning you about it) since $defaultClass
didn't exist, but in the second example, it does exist, so it will be evaluated into OtherClass()
. That function does not exist (it's a class, not a function), so you'll probably get a PHP error, but it's a different error and one that this sniff cannot detect because it only looks to make sure variables exist, not functions.
I understand what you're trying to do: dynamically access the static property and then use that as a class name. This is tricky to do in PHP, though. I'm not sure if it will work, but maybe try this:
<?php
class MyClass extends MyForms
{
private static $defaultClass = 'OtherClass';
final public static function getForm($class = false)
{
if ($class !== false) {
return new $class();
}
return new (self::$defaultClass)();
}
}
from phpcs-variable-analysis.
Thank u so much @sirbrillig, we will review it!
from phpcs-variable-analysis.
Related Issues (20)
- using extract on trusted data will show undefined HOT 2
- Add validUndefinedVariableNamesInFileScope HOT 1
- Improve handling of initial values in initializers HOT 1
- UndefinedVariable first assigned in for loop, used in loop expression HOT 1
- Inline if-clause with short-list false positive HOT 5
- Support of array desctructing HOT 2
- Incorrect warning for arguments to static methods that return "new static()" HOT 3
- Incorrect warning for arguments are thrown on static arrow functions HOT 1
- False-positive `UndefinedVariable` for static variable inside an anonymous function HOT 4
- Incorrect warning about redeclaration of function parameter as static variable HOT 2
- Variable $this is undefined in enum HOT 1
- Support constructor promotion with readonly properties (UnusedVariable) HOT 5
- method call called 'enum' causes an "Cannot find enum start at position" error HOT 2
- Unused function parameter in lambda function HOT 4
- Ternary operator inside arrow function HOT 3
- Incorrect warning Variable $var is undefined. HOT 6
- Undefined variable error introduced in v2.11.15 HOT 2
- Feature Request: don't report on unused parameters for deprecated functions
- Variable reported as unused on an assignment by reference HOT 2
- Assignment of an array element counts as a read
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from phpcs-variable-analysis.