Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Like dirname return type provider of #8611
- Loading branch information
Showing
3 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/Psalm/Internal/Provider/ReturnTypeProvider/BasenameReturnTypeProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 count; | ||
use function basename; | ||
|
||
/** | ||
* @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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' => [ | ||
'$dir===' => "'c'", | ||
], | ||
]; | ||
|
||
yield 'basenameOfStringPathReturnsString' => [ | ||
'code' => '<?php | ||
$dir = basename(implode("", range("a", "c"))); | ||
', | ||
'assertions' => [ | ||
'$dir===' => 'string', | ||
], | ||
]; | ||
} | ||
} |