Skip to content

implement LtrimFunctionReturnTypeExtension to support class-string #1597

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 6, 2022
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
5 changes: 5 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1418,6 +1418,11 @@ services:
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension

-
class: PHPStan\Type\Php\LtrimFunctionReturnTypeExtension
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension

-
class: PHPStan\Type\Php\MbFunctionsReturnTypeExtension
tags:
Expand Down
45 changes: 45 additions & 0 deletions src/Type/Php/LtrimFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Php;

use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\Type;
use function count;
use function ltrim;

class LtrimFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{

public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return $functionReflection->getName() === 'ltrim';
}

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
{
if (count($functionCall->getArgs()) !== 2) {
return null;
}

$string = $scope->getType($functionCall->getArgs()[0]->value);
$trimChars = $scope->getType($functionCall->getArgs()[1]->value);

if ($trimChars instanceof ConstantStringType && $trimChars->getValue() === '\\') {
if ($string instanceof ConstantStringType && $string->isClassString()) {
return new ConstantStringType(ltrim($string->getValue(), $trimChars->getValue()), true);
}

if ($string instanceof ClassStringType) {
return new ClassStringType();
}
}

return null;
}

}
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5223.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7698.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/non-falsy-string.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7483.php');
}

/**
Expand Down
25 changes: 25 additions & 0 deletions tests/PHPStan/Analyser/data/bug-7483.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types=1);

namespace Bug7483;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried supporting things like

function bar() {
	$absolute = '\A';
	$relative = 'A';

	assertType('class-string', ltrim($absolute, '\\'));
	assertType('class-string', ltrim($relative, '\\'));
}

but from the extension point of view these strings seem not to be class-strings...?

Copy link
Member

Choose a reason for hiding this comment

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

ConstantStringType::isClassString() is just a hint whether the string was created from ::class, so that we don't trigger the autoloading for irrelevant strings, which upsets some custom autoloaders.

Copy link
Member

Choose a reason for hiding this comment

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

So you don't need to do anything about it.


use function PHPStan\Testing\assertType;

class A {}

/**
* @param class-string<A> $class
*/
function bar($class): string
{
assertType('class-string', ltrim($class, '\\'));
}

/**
* @param class-string $class
* @return class-string
*/
function foo($class): string
{
assertType('class-string', ltrim($class, '\\'));
assertType("'Bug7483\\\\A'", ltrim(A::class, '\\'));
}