Skip to content

Commit d32efcb

Browse files
authored
Support chop() function as an alias of rtrim() (#5157)
1 parent a81a132 commit d32efcb

File tree

5 files changed

+27
-2
lines changed

5 files changed

+27
-2
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1506,7 +1506,7 @@ private function specifyTypesForConstantStringBinaryExpression(
15061506
&& $exprNode instanceof FuncCall
15071507
&& $exprNode->name instanceof Name
15081508
&& in_array(strtolower((string) $exprNode->name), [
1509-
'trim', 'ltrim', 'rtrim',
1509+
'trim', 'ltrim', 'rtrim', 'chop',
15101510
'mb_trim', 'mb_ltrim', 'mb_rtrim',
15111511
], true)
15121512
&& isset($exprNode->getArgs()[0])

src/Type/Php/TrimFunctionDynamicReturnTypeExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ final class TrimFunctionDynamicReturnTypeExtension implements DynamicFunctionRet
2727

2828
public function isFunctionSupported(FunctionReflection $functionReflection): bool
2929
{
30-
return in_array($functionReflection->getName(), ['trim', 'rtrim'], true);
30+
return in_array($functionReflection->getName(), ['trim', 'rtrim', 'chop'], true);
3131
}
3232

3333
public function getTypeFromFunctionCall(

tests/PHPStan/Analyser/nsrt/bug-12973.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,20 @@ function rtrimTypes(string $value): void
7777
}
7878
assertType('string', $value);
7979
}
80+
81+
function chopTypes(string $value): void
82+
{
83+
if (chop($value) === '') {
84+
assertType('string', $value);
85+
} else {
86+
assertType('non-empty-string', $value);
87+
}
88+
assertType('string', $value);
89+
90+
if (chop($value) !== '') {
91+
assertType('non-empty-string', $value);
92+
} else {
93+
assertType('string', $value);
94+
}
95+
assertType('string', $value);
96+
}

tests/PHPStan/Analyser/nsrt/lowercase-string-trim.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,19 @@ public function doTrim(string $lowercase, string $string): void
1515
assertType('lowercase-string', trim($lowercase));
1616
assertType('lowercase-string', ltrim($lowercase));
1717
assertType('lowercase-string', rtrim($lowercase));
18+
assertType('lowercase-string', chop($lowercase));
1819
assertType('lowercase-string', trim($lowercase, $string));
1920
assertType('lowercase-string', ltrim($lowercase, $string));
2021
assertType('lowercase-string', rtrim($lowercase, $string));
22+
assertType('lowercase-string', chop($lowercase));
2123
assertType('string', trim($string));
2224
assertType('string', ltrim($string));
2325
assertType('string', rtrim($string));
26+
assertType('string', chop($string));
2427
assertType('string', trim($string, $lowercase));
2528
assertType('string', ltrim($string, $lowercase));
2629
assertType('string', rtrim($string, $lowercase));
30+
assertType('string', chop($string, $lowercase));
2731
}
2832

2933
}

tests/PHPStan/Analyser/nsrt/uppercase-string-trim.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,19 @@ public function doTrim(string $uppercase, string $string): void
1515
assertType('uppercase-string', trim($uppercase));
1616
assertType('uppercase-string', ltrim($uppercase));
1717
assertType('uppercase-string', rtrim($uppercase));
18+
assertType('uppercase-string', chop($uppercase));
1819
assertType('uppercase-string', trim($uppercase, $string));
1920
assertType('uppercase-string', ltrim($uppercase, $string));
2021
assertType('uppercase-string', rtrim($uppercase, $string));
22+
assertType('uppercase-string', chop($uppercase, $string));
2123
assertType('string', trim($string));
2224
assertType('string', ltrim($string));
2325
assertType('string', rtrim($string));
26+
assertType('string', chop($string));
2427
assertType('string', trim($string, $uppercase));
2528
assertType('string', ltrim($string, $uppercase));
2629
assertType('string', rtrim($string, $uppercase));
30+
assertType('string', chop($string, $uppercase));
2731
}
2832

2933
}

0 commit comments

Comments
 (0)