-
Notifications
You must be signed in to change notification settings - Fork 2
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
Update TP: 45_static_method_from_variable #35
Comments
@enferas : what do you think? |
Thank you for the suggestion. Yes I agree that the current code will throw an error. The only issue that the suggested solution will mix this testability pattern with static property pattern. But I don't see other solution than creating multiple instances one with static property and one with global variable. |
@enferas Thank you for your answer. Indeed, the pattern 29 (static properties) looks very similar to the proposed change. But when looking at pattern 28 (static method) it is actually the same as the proposed change. So I think I have not yet fully understood, what this pattern 45 should stress. The discovery rules of pattern 28 and 45 are very similar as well. Pattern 45: cpg.call.name(".*INIT_STATIC_METHOD_CALL.*").argument.order(1).code("CV.*|T.*|V.*").location.toJson Pattern 28: cpg.call(".*INIT_STATIC_METHOD_CALL.*").location.toJson So does it mean, that pattern 45 expects an argument for <?php
class A {
static function show_one($a) {
return $a;
}
}
$b = $_GET["p1"]; // source
$a = new A();
$c = $a::show_one($b);
echo $c; // sink But in my opinion, this code could just be an instance of pattern 28. What am I missing? |
@enferas : maybe this pattern is just a duplication and we can remove it? |
First, if I understand correctly we are comparing between patterns 28 and 45. 45 requires type inference to know the object is created from which class to know which static method will be called. 28 doesn't require type inference. 28 discovery rule will look for all the static method calls, while 45 will look for all static method calls when the call from a class instance. Thus, 28 cover 45 and I think we will need to improve the discovery rule for 28 to not cover 45. While, I think they are two different cases and it is better to not merge them. |
Testability pattern
Pattern 45_static_method_from_variable
Problem statement
The original code:
When trying to run this code on my machine with PHP 8.1.2 and Zend Engine v4.1.2 it throws a
Fatal error
.In my opinion that is correct. The instance code does not work, because
$this
is accessed in thestatic function show_one()
which should not be possible as$this
refers to a specific instance of a class, while thestatic function show_one()
refers to the class itself.Proposed changes
In my understanding, this pattern targets, that a static function can be called from an instance of the class.
If that is the right understanding, changing the variable
$one
frompublic
tostatic
should fix the problem.The changed code could look like this:
The text was updated successfully, but these errors were encountered: