Skip to content

Commit

Permalink
infer non-empty-string on literal-string and strlen()
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Apr 29, 2022
1 parent 153e051 commit bc000e8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Analyser/TypeSpecifier.php
Expand Up @@ -235,7 +235,7 @@ public function specifyTypesInCondition(
$newContext = $newContext->negate();
}
$argType = $scope->getType($exprNode->getArgs()[0]->value);
if ($argType instanceof StringType) {
if ($argType->isString()->yes()) {
$funcTypes = $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr);
$valueTypes = $this->create($exprNode->getArgs()[0]->value, new AccessoryNonEmptyStringType(), $newContext, false, $scope, $rootExpr);
return $funcTypes->unionWith($valueTypes);
Expand Down Expand Up @@ -535,7 +535,7 @@ public function specifyTypesInCondition(
|| ($context->falsey() && (new ConstantIntegerType(1 - $offset))->isSuperTypeOf($leftType)->yes())
) {
$argType = $scope->getType($expr->right->getArgs()[0]->value);
if ($argType instanceof StringType) {
if ($argType->isString()->yes()) {
$result = $result->unionWith($this->create($expr->right->getArgs()[0]->value, new AccessoryNonEmptyStringType(), $context, false, $scope, $rootExpr));
}
}
Expand Down
24 changes: 24 additions & 0 deletions tests/PHPStan/Analyser/data/non-empty-string.php
Expand Up @@ -89,6 +89,30 @@ public function doBar5(string $s): void
assertType('\'\'', $s);
}

/**
* @param literal-string $s
*/
public function doBar6($s): void
{
if (1 === strlen($s)) {
assertType('literal-string&non-empty-string', $s);
return;
}
assertType('literal-string', $s);
}

/**
* @param literal-string $s
*/
public function doBar7($s): void
{
if (0 < strlen($s)) {
assertType('literal-string&non-empty-string', $s);
return;
}
assertType("''", $s);
}

public function doFoo3(string $s): void
{
if ($s) {
Expand Down

0 comments on commit bc000e8

Please sign in to comment.