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

Do not call TypeSpecifier::specifyTypesInCondition with a non-null context in case the original context is null #1275

Merged
merged 1 commit into from May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Analyser/TypeSpecifier.php
Expand Up @@ -175,7 +175,7 @@ public function specifyTypesInCondition(
$exprNode = $expressions[0];
/** @var ConstantScalarType $constantType */
$constantType = $expressions[1];
if ($constantType->getValue() === false) {
if (!$context->null() && $constantType->getValue() === false) {
$types = $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr);
return $types->unionWith($this->specifyTypesInCondition(
$scope,
Expand All @@ -185,7 +185,7 @@ public function specifyTypesInCondition(
));
}

if ($constantType->getValue() === true) {
if (!$context->null() && $constantType->getValue() === true) {
$types = $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr);
return $types->unionWith($this->specifyTypesInCondition(
$scope,
Expand Down Expand Up @@ -339,7 +339,7 @@ public function specifyTypesInCondition(
$exprNode = $expressions[0];
/** @var ConstantScalarType $constantType */
$constantType = $expressions[1];
if ($constantType->getValue() === false || $constantType->getValue() === null) {
if (!$context->null() && ($constantType->getValue() === false || $constantType->getValue() === null)) {
return $this->specifyTypesInCondition(
$scope,
$exprNode,
Expand All @@ -348,7 +348,7 @@ public function specifyTypesInCondition(
);
}

if ($constantType->getValue() === true) {
if (!$context->null() && $constantType->getValue() === true) {
return $this->specifyTypesInCondition(
$scope,
$exprNode,
Expand Down
Expand Up @@ -528,4 +528,10 @@ public function testBug6939(): void
]);
}

public function testBug7166(): void
{
$this->checkAlwaysTrueStrictComparison = true;
$this->analyse([__DIR__ . '/data/bug-7166.php'], []);
}

}
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-7166.php
@@ -0,0 +1,16 @@
<?php declare(strict_types = 1);

namespace Bug7166;

use function PHPStan\dumpType;

class HelloWorld
{
public static function print(
string $value
): void {
$isSingleLine = strpos($value, "\n") === false;
dumpType($value);
$hasLeadingSpace = $value !== '' && ($value[0] === ' ' || $value[0] === '\t');
}
}