Skip to content

Commit 1ff9e3f

Browse files
committed
Implement TypeSpecifierContext->getReturnType()
1 parent 5878035 commit 1ff9e3f

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -333,21 +333,6 @@ public function specifyTypesInCondition(
333333
}
334334
}
335335

336-
if (
337-
!$context->null()
338-
&& $expr->right instanceof FuncCall
339-
&& count($expr->right->getArgs()) >= 3
340-
&& $expr->right->name instanceof Name
341-
&& in_array(strtolower((string) $expr->right->name), ['preg_match'], true)
342-
&& IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($leftType)->yes()
343-
) {
344-
return $this->specifyTypesInCondition(
345-
$scope,
346-
new Expr\BinaryOp\NotIdentical($expr->right, new ConstFetch(new Name('false'))),
347-
$context,
348-
)->setRootExpr($expr);
349-
}
350-
351336
if (
352337
!$context->null()
353338
&& $expr->right instanceof FuncCall
@@ -468,6 +453,27 @@ public function specifyTypesInCondition(
468453
}
469454
}
470455

456+
if (
457+
!$context->null()
458+
&& $expr->left instanceof Node\Scalar
459+
&& $expr->right instanceof Expr\FuncCall
460+
&& $expr->right->name instanceof Name
461+
&& in_array(strtolower((string) $expr->right->name), ['preg_match'], true)
462+
) {
463+
if (!$scope instanceof MutatingScope) {
464+
throw new ShouldNotHappenException();
465+
}
466+
$newScope = $scope->filterBySpecifiedTypes($result);
467+
$callType = $newScope->getType($expr->right);
468+
$newContext = $context->newWithReturnType($callType);
469+
470+
$result = $result->unionWith($this->specifyTypesInCondition(
471+
$scope,
472+
$expr->right,
473+
$newContext,
474+
)->setRootExpr($expr));
475+
}
476+
471477
return $result;
472478

473479
} elseif ($expr instanceof Node\Expr\BinaryOp\Greater) {

src/Analyser/TypeSpecifierContext.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPStan\Analyser;
44

55
use PHPStan\ShouldNotHappenException;
6+
use PHPStan\Type\Type;
67

78
/**
89
* @api
@@ -21,6 +22,8 @@ final class TypeSpecifierContext
2122
/** @var self[] */
2223
private static array $registry;
2324

25+
private ?Type $returnType = null;
26+
2427
private function __construct(private ?int $value)
2528
{
2629
}
@@ -89,4 +92,16 @@ public function null(): bool
8992
return $this->value === null;
9093
}
9194

95+
public function newWithReturnType(Type $type): self
96+
{
97+
$new = self::create($this->value);
98+
$new->returnType = $type;
99+
return $new;
100+
}
101+
102+
public function getReturnType(): ?Type
103+
{
104+
return $this->returnType;
105+
}
106+
92107
}

0 commit comments

Comments
 (0)