1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Merge pull request #8620 from kkmuffme/basename-return-type-provider

add basename return type provider
This commit is contained in:
orklah 2022-10-26 22:01:32 +02:00 committed by GitHub
commit 26c2539827
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 92 additions and 0 deletions

View File

@ -21,6 +21,7 @@ use Psalm\Internal\Provider\ReturnTypeProvider\ArrayReverseReturnTypeProvider;
use Psalm\Internal\Provider\ReturnTypeProvider\ArraySliceReturnTypeProvider;
use Psalm\Internal\Provider\ReturnTypeProvider\ArraySpliceReturnTypeProvider;
use Psalm\Internal\Provider\ReturnTypeProvider\ArrayUniqueReturnTypeProvider;
use Psalm\Internal\Provider\ReturnTypeProvider\BasenameReturnTypeProvider;
use Psalm\Internal\Provider\ReturnTypeProvider\DirnameReturnTypeProvider;
use Psalm\Internal\Provider\ReturnTypeProvider\ExplodeReturnTypeProvider;
use Psalm\Internal\Provider\ReturnTypeProvider\FilterVarReturnTypeProvider;
@ -87,6 +88,7 @@ class FunctionReturnTypeProvider
$this->registerClass(StrTrReturnTypeProvider::class);
$this->registerClass(VersionCompareReturnTypeProvider::class);
$this->registerClass(MktimeReturnTypeProvider::class);
$this->registerClass(BasenameReturnTypeProvider::class);
$this->registerClass(DirnameReturnTypeProvider::class);
$this->registerClass(ExplodeReturnTypeProvider::class);
$this->registerClass(GetObjectVarsReturnTypeProvider::class);

View File

@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
namespace Psalm\Internal\Provider\ReturnTypeProvider;
use Psalm\Internal\Analyzer\Statements\Expression\IncludeAnalyzer;
use Psalm\Plugin\EventHandler\Event\FunctionReturnTypeProviderEvent;
use Psalm\Plugin\EventHandler\FunctionReturnTypeProviderInterface;
use Psalm\Type;
use Psalm\Type\Union;
use function basename;
use function count;
/**
* @internal
*/
class BasenameReturnTypeProvider implements FunctionReturnTypeProviderInterface
{
/**
* @return array<lowercase-string>
*/
public static function getFunctionIds(): array
{
return ['basename'];
}
public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event): ?Union
{
$call_args = $event->getCallArgs();
if (count($call_args) === 0) {
return null;
}
$statements_source = $event->getStatementsSource();
$evaled_path = IncludeAnalyzer::getPathTo(
$call_args[0]->value,
null,
null,
$statements_source->getFileName(),
$statements_source->getCodebase()->config
);
if ($evaled_path === null) {
return Type::getString();
}
$basename = basename($evaled_path);
return Type::getString($basename);
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace Psalm\Tests\ReturnTypeProvider;
use Psalm\Tests\TestCase;
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
use const DIRECTORY_SEPARATOR;
class BasenameTest extends TestCase
{
use ValidCodeAnalysisTestTrait;
public function providerValidCodeParse(): iterable
{
$input = 'a' . DIRECTORY_SEPARATOR . 'b' . DIRECTORY_SEPARATOR . 'c';
yield 'basenameOfLiteralStringPathReturnsLiteralString' => [
'code' => '<?php
$base = basename("' . $input . '");
',
'assertions' => [
'$base===' => "'c'",
],
];
yield 'basenameOfStringPathReturnsString' => [
'code' => '<?php
$base = basename(implode("", range("a", "c")));
',
'assertions' => [
'$base===' => 'string',
],
];
}
}