Skip to content

Commit

Permalink
Do not call TypeSpecifier::specifyTypesInCondition with a non-null co…
Browse files Browse the repository at this point in the history
…ntext in case the original context is null
  • Loading branch information
herndlm authored and ondrejmirtes committed May 3, 2022
1 parent 1e95392 commit 6865741
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
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');
}
}

0 comments on commit 6865741

Please sign in to comment.