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

[CodeQuality] Handle negative and positive int/float on InlineConstructorDefaultToPropertyRector #5159

Merged
merged 4 commits into from Oct 13, 2023

Conversation

samsonasik
Copy link
Member

@samsonasik
Copy link
Member Author

Fixed 🎉 /cc @JulianCBC

@samsonasik samsonasik changed the title [CodeQuality] Handle negative and positive int on InlineConstructorDefaultToPropertyRector [CodeQuality] Handle negative and positive int/float on InlineConstructorDefaultToPropertyRector Oct 13, 2023
@samsonasik
Copy link
Member Author

All checks have passed 🎉 @TomasVotruba let's merge it ;)

@samsonasik samsonasik enabled auto-merge (squash) October 13, 2023 09:55
@samsonasik samsonasik merged commit 1f04504 into main Oct 13, 2023
41 checks passed
@samsonasik samsonasik deleted the neg-pos branch October 13, 2023 09:56
@JulianCBC
Copy link
Contributor

Is this actually enough to fully handle UnaryMinus and UnaryPlus? Am I right that, for the purposes of determining whether an expression is dynamic, the unary plus and unary minus don't matter? I.e. should something like:

class Test {
    public $thing;
    public function __construct() {
        $this->thing = -CONSTANT;
    }
}

should also get moved?

@samsonasik
Copy link
Member Author

Accessing constant on property default value can be dangerous that cause compile time error, see https://3v4l.org/W8Epd

I think exact int and float value is the safest :)

@JulianCBC
Copy link
Contributor

I'm not disagreeing that it's the safest.

I think that something like this would cover almost all possible scenarios:

            if ($expr instanceof UnaryPlus || $expr instanceof UnaryMinus) {
                return $this->isDynamicExpr($expr->expr);
            }

@samsonasik
Copy link
Member Author

samsonasik commented Oct 15, 2023

I see, yes, it seems can be covered with:

             if ($expr instanceof UnaryPlus || $expr instanceof UnaryMinus) {
-                return ! $expr->expr instanceof LNumber && ! $expr->expr instanceof DNumber;
+                return $this->isDynamicExpr($expr->expr);
+            }

@JulianCBC could you create a PR improvement for it with add fixture for possible constants ? Thank you.

@JulianCBC
Copy link
Contributor

Can do! PR incoming...

@JulianCBC
Copy link
Contributor

Done: #5177

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants