Skip to content

Commit f3dc81e

Browse files
VincentLangletondrejmirtes
authored andcommitted
Do not instantiate Accessory without string type
1 parent 097a155 commit f3dc81e

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

src/Type/Php/LtrimFunctionReturnTypeExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
7272
} elseif ($trimConstantString->getValue() === '\\' && $string->isClassString()->yes()) {
7373
$result[] = new ClassStringType();
7474
} elseif (preg_match('/\d/', $trimConstantString->getValue()) === 0 && $string->isNumericString()->yes()) {
75-
$result[] = new AccessoryNumericStringType();
75+
$result[] = new IntersectionType([new StringType(), new AccessoryNumericStringType()]);
7676
} else {
7777
return $defaultType;
7878
}

src/Type/Php/TrimFunctionDynamicReturnTypeExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function getTypeFromFunctionCall(
7878
);
7979
}
8080
} elseif (preg_match('/\d/', $trimConstantString->getValue()) === 0 && $stringType->isNumericString()->yes()) {
81-
$result[] = new AccessoryNumericStringType();
81+
$result[] = new IntersectionType([new StringType(), new AccessoryNumericStringType()]);
8282
} else {
8383
return $defaultType;
8484
}

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2380,6 +2380,11 @@ public function testBug11863(): void
23802380
$this->analyse([__DIR__ . '/data/bug-11863.php'], []);
23812381
}
23822382

2383+
public function testBug13784(): void
2384+
{
2385+
$this->analyse([__DIR__ . '/data/bug-13784.php'], []);
2386+
}
2387+
23832388
public function testBug13556(): void
23842389
{
23852390
$this->checkExplicitMixed = true;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug13784;
4+
5+
function ok(): int {
6+
return strlen(number_format(15, 0, '', ''));
7+
}
8+
9+
/**
10+
* @param numeric-string $str
11+
*/
12+
function ok2(string $str): int {
13+
return strlen($str);
14+
}
15+
16+
function fail(): int {
17+
return strlen(ltrim(number_format(15, 0, '', ''), '-'));
18+
}
19+
20+
function fail2(): int {
21+
return strlen(trim(number_format(15, 0, '', ''), '-'));
22+
}

0 commit comments

Comments
 (0)