Skip to content
Merged
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
70 changes: 35 additions & 35 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -1200,41 +1200,6 @@ private function specifyTypesForConstantStringBinaryExpression(
}
$constantStringValue = $scalarValues[0];

if (
$context->truthy()
&& $exprNode instanceof FuncCall
&& $exprNode->name instanceof Name
&& in_array(strtolower($exprNode->name->toString()), [
'substr', 'strstr', 'stristr', 'strchr', 'strrchr', 'strtolower', 'strtoupper', 'ucfirst', 'lcfirst',
'mb_substr', 'mb_strstr', 'mb_stristr', 'mb_strchr', 'mb_strrchr', 'mb_strtolower', 'mb_strtoupper', 'mb_ucfirst', 'mb_lcfirst',
'ucwords', 'mb_convert_case', 'mb_convert_kana',
], true)
&& isset($exprNode->getArgs()[0])
&& $constantStringValue !== ''
) {
$argType = $scope->getType($exprNode->getArgs()[0]->value);

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

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

if (
$exprNode instanceof FuncCall
&& $exprNode->name instanceof Name
Expand Down Expand Up @@ -2171,6 +2136,41 @@ public function resolveIdentical(Expr\BinaryOp\Identical $expr, Scope $scope, Ty
}
}

if (
$context->truthy()
&& $unwrappedLeftExpr instanceof FuncCall
&& $unwrappedLeftExpr->name instanceof Name
&& in_array(strtolower($unwrappedLeftExpr->name->toString()), [
'substr', 'strstr', 'stristr', 'strchr', 'strrchr', 'strtolower', 'strtoupper', 'ucfirst', 'lcfirst',
'mb_substr', 'mb_strstr', 'mb_stristr', 'mb_strchr', 'mb_strrchr', 'mb_strtolower', 'mb_strtoupper', 'mb_ucfirst', 'mb_lcfirst',
'ucwords', 'mb_convert_case', 'mb_convert_kana',
], true)
&& isset($unwrappedLeftExpr->getArgs()[0])
&& $rightType->isNonEmptyString()->yes()
) {
$argType = $scope->getType($unwrappedLeftExpr->getArgs()[0]->value);

if ($argType->isString()->yes()) {
if ($rightType->isNonFalsyString()->yes()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of if ($constantStringValue !== '0') { we now rely on the type-system

return $this->create(
$unwrappedLeftExpr->getArgs()[0]->value,
TypeCombinator::intersect($argType, new AccessoryNonFalsyStringType()),
$context,
false,
$scope,
);
}

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

if ($rightType->isInteger()->yes() || $rightType->isString()->yes()) {
$types = null;
foreach ($rightType->getFiniteTypes() as $finiteType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use function PHPStan\Testing\assertType;

class Foo {
class Foo
{
public function nonEmptySubstr(string $s, int $offset, int $length): void
{
if (substr($s, 10) === 'hallo') {
Expand Down Expand Up @@ -81,4 +82,19 @@ public function nonEmptySubstr(string $s, int $offset, int $length): void
assertType('\'hallo\'', $x);
}
}

/**
* @param non-empty-string $nonES
* @param non-falsy-string $falsyString
*/
public function stringTypes(string $s, $nonES, $falsyString): void
{
if (substr($s, 10) === $nonES) {
assertType('non-empty-string', $s);
}

if (substr($s, 10) === $falsyString) {
assertType('non-falsy-string', $s);
}
}
}
Loading