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

Added a rule to check for always true / false result of boolean-xor operations #1502

Closed
wants to merge 5 commits into from

Conversation

mad-briller
Copy link
Contributor

closes phpstan/phpstan#7539

Adapted the rule from the BooleanOrConstantConditionRule

@mad-briller
Copy link
Contributor Author

not sure what's going on with the remaining 2 failed actions there, if there's something i need to do let me know

@ondrejmirtes
Copy link
Member

Oh and I forgot - this new rule has to be registered only in bleedingEdge using the conditionalTags section. (There's plenty of examples for this in conf/).

…uleHelper. Changed BooleanXorConstantConditionRule to be active only in bleeding edge.
@@ -2292,6 +2293,25 @@ static function (?Type $offsetType, Type $valueType, bool $optional) use (&$arra

$this->callNodeCallbackWithExpression($nodeCallback, new BooleanOrNode($expr, $leftResult->getFalseyScope()), $scope, $context);

return new ExpressionResult(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm not confident that i've done the right thing here, just winged it based on what the BooleanOrConstantConditionRule is doing and the node structure it relies on.

i would not be surprised if i have overlooked something in this code

return $left xor $right;
}

function skip(\DateTimeImmutable $date): void
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i could not figure out a way to write an xor statement that had neither the left or right being always true or false, while the full statement itself is a boolean constant result.

i tried this but due to the way phpstan models it's types as possibilities and not as branches it doesn't work :(

$val = (bool) rand(0, 1);
$notVal = !$val;

$res = ($val xor $notVal);

if you have any wisdom let me know :)

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