Skip to content
Open
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
28 changes: 24 additions & 4 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\Accessory\AccessoryLowercaseStringType;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
use PHPStan\Type\Accessory\AccessoryUppercaseStringType;
use PHPStan\Type\Accessory\HasOffsetType;
use PHPStan\Type\Accessory\HasPropertyType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
Expand Down Expand Up @@ -2437,21 +2439,39 @@ private function resolveNormalizedIdentical(Expr\BinaryOp\Identical $expr, Scope
$argType = $scope->getType($unwrappedLeftExpr->getArgs()[0]->value);

if ($argType->isString()->yes()) {
$specifiedTypes = new SpecifiedTypes();
if (in_array(strtolower($unwrappedLeftExpr->name->toString()), ['strtolower', 'mb_strtolower'], true)) {
$specifiedTypes = $this->create(
$unwrappedRightExpr,
TypeCombinator::intersect($rightType, new AccessoryLowercaseStringType()),
$context,
$scope,
)->setRootExpr($expr);
}
if (in_array(strtolower($unwrappedLeftExpr->name->toString()), ['strtoupper', 'mb_strtoupper'], true)) {
$specifiedTypes = $this->create(
$unwrappedRightExpr,
TypeCombinator::intersect($rightType, new AccessoryUppercaseStringType()),
$context,
$scope,
)->setRootExpr($expr);
}

if ($rightType->isNonFalsyString()->yes()) {
return $this->create(
return $specifiedTypes->unionWith($this->create(
$unwrappedLeftExpr->getArgs()[0]->value,
TypeCombinator::intersect($argType, new AccessoryNonFalsyStringType()),
$context,
$scope,
)->setRootExpr($expr);
)->setRootExpr($expr));
}

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

Expand Down
69 changes: 69 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14047.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Bug14047;

use function is_numeric;
use function PHPStan\Testing\assertType;

function test_strings(string $a, string $b): void
{
if (strtolower($a) !== $b) {
assertType('string', $a);
assertType('string', $b);
}

if ($a !== '' && strtolower($a) == $b) {
assertType('non-empty-string', $a);
assertType('lowercase-string&non-empty-string', $b);
}

if ($a !== '' && strtolower($a) === $b) {
assertType('non-empty-string', $a);
assertType('lowercase-string&non-empty-string', $b);
}

if ($a !== '' && strtolower($a) == $b && $b !== '0') {
assertType('non-empty-string', $a);
assertType('lowercase-string&non-falsy-string', $b);
}

if ($a !== '' && strtolower($a) === $a) {
assertType('lowercase-string&non-empty-string', $a);
} elseif ($a !== '' && strtoupper($a) === $a) {
assertType('non-empty-string&uppercase-string', $a);
}

if ($a !== '') {
if (strtolower($a) === $a) {
assertType('lowercase-string&non-empty-string', $a);
}
}

if (strtolower($b) === $b && $b !== '') {
assertType('lowercase-string&non-empty-string', $b);
} elseif (strtoupper($b) === $b && $b !== '') {
assertType('non-empty-string&uppercase-string', $b);
}

if ($b !== '' && is_numeric($b)) {
assertType('non-empty-string&numeric-string', $b);
}

if (strtolower($b) === $b && $b !== '' && is_numeric($b)) {
assertType('lowercase-string&non-empty-string&numeric-string', $b);
}

assertType('string', $b);
if (is_numeric($b)) {
assertType('numeric-string', $b);
if (strtolower($b) === $b) {
assertType('lowercase-string&non-empty-string&numeric-string', $b);
if ($b !== '') {
assertType('lowercase-string&non-empty-string&numeric-string', $b);
}
}
if ($b !== '') {
assertType('numeric-string', $b);
}
}
}
Loading