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
9 changes: 7 additions & 2 deletions src/Type/Php/LtrimFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@
use PHPStan\Type\TypeCombinator;
use function count;
use function ltrim;
use function preg_match;

#[AutowiredService]
final class LtrimFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{

public function __construct(
private TrimCharactersHelper $trimCharactersHelper,
)
{
}

public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return $functionReflection->getName() === 'ltrim';
Expand Down Expand Up @@ -72,7 +77,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
}
} elseif ($trimConstantString->getValue() === '\\' && $string->isClassString()->yes()) {
$result[] = new ClassStringType();
} elseif (preg_match('/\d/', $trimConstantString->getValue()) === 0 && $string->isNumericString()->yes()) {
} elseif (!$this->trimCharactersHelper->containsDigit($trimConstantString->getValue()) && $string->isNumericString()->yes()) {
$result[] = new IntersectionType([new StringType(), new AccessoryNumericStringType()]);
} else {
return $defaultType;
Expand Down
44 changes: 44 additions & 0 deletions src/Type/Php/TrimCharactersHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Php;

use PHPStan\DependencyInjection\AutowiredService;
use function ord;
use function strlen;

#[AutowiredService]
final class TrimCharactersHelper
{

/**
* Reads the character list like PHP's php_charmask(): "a..z" stands for
* every byte from "a" to "z", so "/..:" contains the digits.
*/
public function containsDigit(string $characters): bool

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this new method should have some unit tests

{
$length = strlen($characters);
for ($i = 0; $i < $length; $i++) {
$byte = ord($characters[$i]);
if (
$i + 3 < $length
&& $characters[$i + 1] === '.'
&& $characters[$i + 2] === '.'
&& ord($characters[$i + 3]) >= $byte
) {
if ($byte <= ord('9') && ord($characters[$i + 3]) >= ord('0')) {
return true;
}

$i += 3;
continue;
}

if ($byte >= ord('0') && $byte <= ord('9')) {
return true;
}
}

return false;
}

}
9 changes: 7 additions & 2 deletions src/Type/Php/TrimFunctionDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@
use PHPStan\Type\TypeCombinator;
use function count;
use function in_array;
use function preg_match;
use function rtrim;
use function trim;

#[AutowiredService]
final class TrimFunctionDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{

public function __construct(
private TrimCharactersHelper $trimCharactersHelper,
)
{
}

public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return in_array($functionReflection->getName(), ['trim', 'rtrim', 'chop'], true);
Expand Down Expand Up @@ -77,7 +82,7 @@ public function getTypeFromFunctionCall(
true,
);
}
} elseif (preg_match('/\d/', $trimConstantString->getValue()) === 0 && $stringType->isNumericString()->yes()) {
} elseif (!$this->trimCharactersHelper->containsDigit($trimConstantString->getValue()) && $stringType->isNumericString()->yes()) {
$result[] = new IntersectionType([new StringType(), new AccessoryNumericStringType()]);
} else {
return $defaultType;
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Analyser/nsrt/numeric-string-trim.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ public function doTrim(string $numericString, string $string): void
assertType('string', trim($numericString, '-0'));
assertType('string', ltrim($numericString, '-0'));
assertType('string', rtrim($numericString, '-0'));

assertType('string', trim($numericString, '/..:'));
assertType('string', ltrim($numericString, '/..:'));
assertType('string', rtrim($numericString, '/..:'));
assertType('string', chop($numericString, '/..:'));
assertType('string', trim($numericString, '!..~'));

assertType('numeric-string', trim($numericString, 'a..z'));
assertType('numeric-string', trim($numericString, '..:'));
assertType('numeric-string', trim($numericString, ':../'));
}

}
Loading