Skip to content

Commit 22f0cab

Browse files
Fix trim issue
1 parent e99bf4f commit 22f0cab

File tree

3 files changed

+55
-15
lines changed

3 files changed

+55
-15
lines changed

conf/config.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,6 +1774,11 @@ services:
17741774
tags:
17751775
- phpstan.broker.dynamicFunctionReturnTypeExtension
17761776

1777+
-
1778+
class: PHPStan\Type\Php\TrimFunctionDynamicReturnTypeExtension
1779+
tags:
1780+
- phpstan.broker.dynamicFunctionReturnTypeExtension
1781+
17771782
-
17781783
class: PHPStan\Type\Php\VersionCompareFunctionDynamicReturnTypeExtension
17791784
tags:
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Php;
4+
5+
use PhpParser\Node\Expr\FuncCall;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Reflection\FunctionReflection;
8+
use PHPStan\Type\Accessory\AccessoryLowercaseStringType;
9+
use PHPStan\Type\Accessory\AccessoryUppercaseStringType;
10+
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
11+
use PHPStan\Type\IntersectionType;
12+
use PHPStan\Type\StringType;
13+
use PHPStan\Type\Type;
14+
use function count;
15+
16+
final class TrimFunctionDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
17+
{
18+
19+
public function isFunctionSupported(FunctionReflection $functionReflection): bool
20+
{
21+
return \in_array($functionReflection->getName(), ['trim', 'rtrim', 'ltrim'], true);
22+
}
23+
24+
public function getTypeFromFunctionCall(
25+
FunctionReflection $functionReflection,
26+
FuncCall $functionCall,
27+
Scope $scope,
28+
): ?Type
29+
{
30+
$args = $functionCall->getArgs();
31+
if (count($args) < 1) {
32+
return null;
33+
}
34+
35+
$stringType = $scope->getType($args[0]->value);
36+
$accessory = [];
37+
if ($stringType->isLowercaseString()->yes()) {
38+
$accessory[] = new AccessoryLowercaseStringType();
39+
}
40+
if ($stringType->isUppercaseString()->yes()) {
41+
$accessory[] = new AccessoryUppercaseStringType();
42+
}
43+
if (count($accessory) > 0) {
44+
return new IntersectionType([new StringType(), ...$accessory]);
45+
}
46+
47+
return new StringType();
48+
}
49+
50+
}

stubs/core.stub

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -322,18 +322,3 @@ function is_callable(mixed $value, bool $syntax_only = false, ?string &$callable
322322
* @return ($num is float ? float : $num is int ? non-negative-int : float|non-negative-int)
323323
*/
324324
function abs($num) {}
325-
326-
/**
327-
* @return ($string is lowercase-string&uppercase-string ? lowercase-string&uppercase-string : ($string is lowercase-string ? lowercase-string : ($string is uppercase-string ? uppercase-string : string)))
328-
*/
329-
function trim(string $string, string $characters = " \n\r\t\v\x00"): string {}
330-
331-
/**
332-
* @return ($string is lowercase-string&uppercase-string ? lowercase-string&uppercase-string : ($string is lowercase-string ? lowercase-string : ($string is uppercase-string ? uppercase-string : string)))
333-
*/
334-
function ltrim(string $string, string $characters = " \n\r\t\v\x00"): string {}
335-
336-
/**
337-
* @return ($string is lowercase-string&uppercase-string ? lowercase-string&uppercase-string : ($string is lowercase-string ? lowercase-string : ($string is uppercase-string ? uppercase-string : string)))
338-
*/
339-
function rtrim(string $string, string $characters = " \n\r\t\v\x00"): string {}

0 commit comments

Comments
 (0)