Skip to content

Commit bc000e8

Browse files
authored
infer non-empty-string on literal-string and strlen()
1 parent 153e051 commit bc000e8

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public function specifyTypesInCondition(
235235
$newContext = $newContext->negate();
236236
}
237237
$argType = $scope->getType($exprNode->getArgs()[0]->value);
238-
if ($argType instanceof StringType) {
238+
if ($argType->isString()->yes()) {
239239
$funcTypes = $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr);
240240
$valueTypes = $this->create($exprNode->getArgs()[0]->value, new AccessoryNonEmptyStringType(), $newContext, false, $scope, $rootExpr);
241241
return $funcTypes->unionWith($valueTypes);
@@ -535,7 +535,7 @@ public function specifyTypesInCondition(
535535
|| ($context->falsey() && (new ConstantIntegerType(1 - $offset))->isSuperTypeOf($leftType)->yes())
536536
) {
537537
$argType = $scope->getType($expr->right->getArgs()[0]->value);
538-
if ($argType instanceof StringType) {
538+
if ($argType->isString()->yes()) {
539539
$result = $result->unionWith($this->create($expr->right->getArgs()[0]->value, new AccessoryNonEmptyStringType(), $context, false, $scope, $rootExpr));
540540
}
541541
}

tests/PHPStan/Analyser/data/non-empty-string.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,30 @@ public function doBar5(string $s): void
8989
assertType('\'\'', $s);
9090
}
9191

92+
/**
93+
* @param literal-string $s
94+
*/
95+
public function doBar6($s): void
96+
{
97+
if (1 === strlen($s)) {
98+
assertType('literal-string&non-empty-string', $s);
99+
return;
100+
}
101+
assertType('literal-string', $s);
102+
}
103+
104+
/**
105+
* @param literal-string $s
106+
*/
107+
public function doBar7($s): void
108+
{
109+
if (0 < strlen($s)) {
110+
assertType('literal-string&non-empty-string', $s);
111+
return;
112+
}
113+
assertType("''", $s);
114+
}
115+
92116
public function doFoo3(string $s): void
93117
{
94118
if ($s) {

0 commit comments

Comments
 (0)