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
18 changes: 14 additions & 4 deletions src/Type/Php/ReplaceFunctionsDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Accessory\AccessoryLowercaseStringType;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
Expand Down Expand Up @@ -82,17 +83,26 @@ private function getPreliminarilyResolvedTypeFromFunctionCall(
return TypeUtils::toBenevolentUnion($defaultReturnType);
}

if ($subjectArgumentType->isNonEmptyString()->yes() && array_key_exists($functionReflection->getName(), self::FUNCTIONS_REPLACE_POSITION)) {
if (array_key_exists($functionReflection->getName(), self::FUNCTIONS_REPLACE_POSITION)) {
$replaceArgumentPosition = self::FUNCTIONS_REPLACE_POSITION[$functionReflection->getName()];

if (count($functionCall->getArgs()) > $replaceArgumentPosition) {
$replaceArgumentType = $scope->getType($functionCall->getArgs()[$replaceArgumentPosition]->value);

$accessories = [];
if ($subjectArgumentType->isNonFalsyString()->yes() && $replaceArgumentType->isNonFalsyString()->yes()) {
return new IntersectionType([new StringType(), new AccessoryNonFalsyStringType()]);
$accessories[] = new AccessoryNonFalsyStringType();
} elseif ($subjectArgumentType->isNonEmptyString()->yes() && $replaceArgumentType->isNonEmptyString()->yes()) {
$accessories[] = new AccessoryNonEmptyStringType();
}
if ($replaceArgumentType->isNonEmptyString()->yes()) {
return new IntersectionType([new StringType(), new AccessoryNonEmptyStringType()]);

if ($subjectArgumentType->isLowercaseString()->yes() && $replaceArgumentType->isLowercaseString()->yes()) {
$accessories[] = new AccessoryLowercaseStringType();
}

if (count($accessories) > 0) {
$accessories[] = new StringType();
return new IntersectionType($accessories);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7414,15 +7414,15 @@ public function dataReplaceFunctions(): array
{
return [
[
'non-falsy-string',
'lowercase-string&non-falsy-string',
'$expectedString',
],
[
'string|null',
'$expectedString2',
],
[
'non-falsy-string|null',
'(lowercase-string&non-falsy-string)|null',
'$anotherExpectedString',
],
[
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/isset-coalesce-empty-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ function coalesce()

assertType('int<0, max>', rand() ?? false);

assertType('0|string', preg_replace('', '', '') ?? 0);
assertType('0|lowercase-string', preg_replace('', '', '') ?? 0);

$foo = new FooCoalesce();

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

namespace LowercaseStringReplace;

use function PHPStan\Testing\assertType;

class ReplaceStrings
{

/**
* @param string $s
* @param lowercase-string $ls
*/
public function doFoo(string $s, string $ls): void
{
assertType('lowercase-string', str_replace($s, $ls, $ls));
assertType('string', str_replace($s, $s, $ls));
assertType('string', str_replace($s, $ls, $s));
assertType('lowercase-string', str_replace($ls, $ls, $ls));
assertType('string', str_replace($ls, $s, $ls));
assertType('string', str_replace($ls, $ls, $s));

assertType('lowercase-string', str_ireplace($s, $ls, $ls));
assertType('string', str_ireplace($s, $s, $ls));
assertType('string', str_ireplace($s, $ls, $s));
assertType('lowercase-string', str_ireplace($ls, $ls, $ls));
assertType('string', str_ireplace($ls, $s, $ls));
assertType('string', str_ireplace($ls, $ls, $s));

assertType('lowercase-string|null', preg_replace($s, $ls, $ls));
assertType('string|null', preg_replace($s, $s, $ls));
assertType('string|null', preg_replace($s, $ls, $s));
assertType('lowercase-string|null', preg_replace($ls, $ls, $ls));
assertType('string|null', preg_replace($ls, $s, $ls));
assertType('string|null', preg_replace($ls, $ls, $s));

assertType('lowercase-string', substr_replace($ls, $ls, 1));
assertType('string', substr_replace($s, $ls, 1));
assertType('string', substr_replace($ls, $s, 1));
assertType('string', substr_replace($s, $s, 1));
}
}
Loading