Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/Analyser/ExprHandler/BooleanAndHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\NodeScopeResolver;
use PHPStan\Analyser\NoopNodeCallback;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Node\BooleanAndNode;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function array_merge;

/**
Expand All @@ -34,6 +38,7 @@ final class BooleanAndHandler implements ExprHandler

public function __construct(
private NodeScopeResolver $nodeScopeResolver,
private TypeSpecifier $typeSpecifier,
)
{
}
Expand Down Expand Up @@ -99,15 +104,42 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex

$nodeScopeResolver->callNodeCallbackWithExpression($nodeCallback, new BooleanAndNode($expr, $leftTruthyScope), $scope, $storage, $context);

$leftTruthySpecifiedTypes = $this->typeSpecifier->specifyTypesInCondition($scope, $expr->left, TypeSpecifierContext::createTruthy());

return new ExpressionResult(
$leftMergedWithRightScope,
hasYield: $leftResult->hasYield() || $rightResult->hasYield(),
isAlwaysTerminating: $leftResult->isAlwaysTerminating(),
throwPoints: array_merge($leftResult->getThrowPoints(), $rightResult->getThrowPoints()),
impurePoints: array_merge($leftResult->getImpurePoints(), $rightResult->getImpurePoints()),
truthyScopeCallback: static fn (): MutatingScope => $rightResult->getScope()->filterByTruthyValue($expr->right),
truthyScopeCallback: static fn (): MutatingScope => self::computeTruthyScope($rightResult->getScope(), $expr->right, $leftTruthySpecifiedTypes, $leftTruthyScope),
falseyScopeCallback: static fn (): MutatingScope => $leftMergedWithRightScope->filterByFalseyValue($expr),
);
}

private static function computeTruthyScope(
MutatingScope $rightScope,
Expr $rightExpr,
SpecifiedTypes $leftTruthySpecifiedTypes,
MutatingScope $leftTruthyScope,
): MutatingScope
{
$scope = $rightScope->filterByTruthyValue($rightExpr);

foreach ($leftTruthySpecifiedTypes->getSureNotTypes() as [$exprNode, $sureNotType]) {
if (!$leftTruthyScope->getType($exprNode)->equals($rightScope->getType($exprNode))) {
continue;
}

$exprType = $scope->getType($exprNode);
if (TypeCombinator::remove($exprType, $sureNotType) === $exprType) {
continue;
}

$scope = $scope->removeTypeFromExpression($exprNode, $sureNotType);
}

return $scope;
}

}
34 changes: 33 additions & 1 deletion src/Analyser/ExprHandler/BooleanOrHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\NodeScopeResolver;
use PHPStan\Analyser\NoopNodeCallback;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Node\BooleanOrNode;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function array_merge;

/**
Expand All @@ -32,6 +36,7 @@ final class BooleanOrHandler implements ExprHandler

public function __construct(
private NodeScopeResolver $nodeScopeResolver,
private TypeSpecifier $typeSpecifier,
)
{
}
Expand Down Expand Up @@ -83,15 +88,42 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex

$nodeScopeResolver->callNodeCallbackWithExpression($nodeCallback, new BooleanOrNode($expr, $leftFalseyScope), $scope, $storage, $context);

$leftFalseySpecifiedTypes = $this->typeSpecifier->specifyTypesInCondition($scope, $expr->left, TypeSpecifierContext::createFalsey());

return new ExpressionResult(
$leftMergedWithRightScope,
hasYield: $leftResult->hasYield() || $rightResult->hasYield(),
isAlwaysTerminating: $leftResult->isAlwaysTerminating(),
throwPoints: array_merge($leftResult->getThrowPoints(), $rightResult->getThrowPoints()),
impurePoints: array_merge($leftResult->getImpurePoints(), $rightResult->getImpurePoints()),
truthyScopeCallback: static fn (): MutatingScope => $leftMergedWithRightScope->filterByTruthyValue($expr),
falseyScopeCallback: static fn (): MutatingScope => $rightResult->getScope()->filterByFalseyValue($expr->right),
falseyScopeCallback: static fn (): MutatingScope => self::computeFalseyScope($rightResult->getScope(), $expr->right, $leftFalseySpecifiedTypes, $leftFalseyScope),
);
}

private static function computeFalseyScope(
MutatingScope $rightScope,
Expr $rightExpr,
SpecifiedTypes $leftFalseySpecifiedTypes,
MutatingScope $leftFalseyScope,
): MutatingScope
{
$scope = $rightScope->filterByFalseyValue($rightExpr);

foreach ($leftFalseySpecifiedTypes->getSureNotTypes() as [$exprNode, $sureNotType]) {
if (!$leftFalseyScope->getType($exprNode)->equals($rightScope->getType($exprNode))) {
continue;
}

$exprType = $scope->getType($exprNode);
if (TypeCombinator::remove($exprType, $sureNotType) === $exprType) {
continue;
}

$scope = $scope->removeTypeFromExpression($exprNode, $sureNotType);
}

return $scope;
}

}
16 changes: 14 additions & 2 deletions src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,20 @@ public static function addNull(Type $type): Type
public static function remove(Type $fromType, Type $typeToRemove): Type
{
if ($typeToRemove instanceof UnionType) {
foreach ($typeToRemove->getTypes() as $unionTypeToRemove) {
$fromType = self::remove($fromType, $unionTypeToRemove);
$typesToRemove = $typeToRemove->getTypes();
$changed = true;
while ($changed && count($typesToRemove) > 0) {
$changed = false;
foreach ($typesToRemove as $key => $unionTypeToRemove) {
$newFromType = self::remove($fromType, $unionTypeToRemove);
if ($newFromType === $fromType) {
continue;
}

$fromType = $newFromType;
unset($typesToRemove[$key]);
$changed = true;
}
}
return $fromType;
}
Expand Down
47 changes: 47 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-9114.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php declare(strict_types = 1);

namespace Bug9114;

use function PHPStan\Testing\assertType;

function foo(string $foo): void
{
if ('' === $foo || '0' === $foo) {
throw new \Exception();
}
assertType('non-falsy-string', $foo);
}

function bar(string $foo): void
{
if ('0' === $foo || '' === $foo) {
throw new \Exception();
}
assertType('non-falsy-string', $foo);
}

function baz(string $foo): void
{
if ('0' === $foo) {
throw new \Exception();
}
assertType('string', $foo);
if ('' === $foo) {
throw new \Exception();
}
assertType('non-empty-string', $foo);
}

function qux(string $foo): void
{
if ('' !== $foo && '0' !== $foo) {
assertType('non-falsy-string', $foo);
}
}

function quux(string $foo): void
{
if ('0' !== $foo && '' !== $foo) {
assertType('non-falsy-string', $foo);
}
}
Loading