Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infer non-empty-string on substr() comparison with constant string #1259

Merged
merged 5 commits into from May 21, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/Analyser/TypeSpecifier.php
Expand Up @@ -242,6 +242,27 @@ public function specifyTypesInCondition(
}
}
}

if (
$exprNode instanceof FuncCall
&& $exprNode->name instanceof Name
&& strtolower($exprNode->name->toString()) === 'substr'
&& isset($exprNode->getArgs()[0])
&& $constantType instanceof ConstantStringType
&& $constantType->getValue() !== ''
) {
$argType = $scope->getType($exprNode->getArgs()[0]->value);

if ($argType->isString()->yes()) {
return $this->create(
$exprNode->getArgs()[0]->value,
TypeCombinator::intersect($argType, new AccessoryNonEmptyStringType()),
$context,
false,
$scope,
);
}
}
}

$rightType = $scope->getType($expr->right);
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Expand Up @@ -911,6 +911,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/constant-array-type-identical.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/non-empty-string-str-containing-fns.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6609.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/non-empty-string-substr-specifying.php');
}

/**
Expand Down
63 changes: 63 additions & 0 deletions tests/PHPStan/Analyser/data/non-empty-string-substr-specifying.php
@@ -0,0 +1,63 @@
<?php

namespace NonEmptyStringSubstr;

use function PHPStan\Testing\assertType;

class Foo {
public function nonEmptySubstr(string $s, int $offset, int $length): void
{
if (substr($s, 10) === 'hallo') {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (substr($s, -10) === 'hallo') {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (substr($s, 10, 5) === 'hallo') {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (substr($s, 10, -5) === 'hallo') {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (substr($s, $offset) === 'hallo') {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (substr($s, $offset, $length) === 'hallo') {
staabm marked this conversation as resolved.
Show resolved Hide resolved
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (substr($s, $offset, $length) !== 'hallo') {
assertType('string', $s);
}
assertType('string', $s);

if (substr($s, $offset, $length) === '') {
assertType('string', $s);
}
assertType('string', $s);

if (substr($s, $offset, $length) == '') {
assertType('string', $s);
}
assertType('string', $s);

$x = (substr($s, 10) === 'hallo');
assertType('string', $s);
var_dump($x);

$x = (substr($s, 10) !== 'hallo');
assertType('string', $s);
var_dump($x);
}
}