Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create a return type provider for mb_internal_encoding
- Loading branch information
Showing
2 changed files
with
118 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
116 changes: 116 additions & 0 deletions
116
src/Psalm/Internal/Provider/ReturnTypeProvider/MbInternalEncodingReturnTypeProvider.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,116 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psalm\Internal\Provider\ReturnTypeProvider; | ||
|
||
use Psalm\Plugin\EventHandler\Event\FunctionReturnTypeProviderEvent; | ||
use Psalm\Plugin\EventHandler\FunctionReturnTypeProviderInterface; | ||
use Psalm\Type; | ||
use Psalm\Type\Atomic\TBool; | ||
use Psalm\Type\Atomic\TFalse; | ||
use Psalm\Type\Atomic\TNever; | ||
use Psalm\Type\Atomic\TNull; | ||
use Psalm\Type\Atomic\TString; | ||
use Psalm\Type\Atomic\TTrue; | ||
use Psalm\Type\Union; | ||
|
||
use function assert; | ||
use function count; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class MbInternalEncodingReturnTypeProvider implements FunctionReturnTypeProviderInterface | ||
{ | ||
/** | ||
* @return array<lowercase-string> | ||
*/ | ||
public static function getFunctionIds(): array | ||
{ | ||
return ['mb_internal_encoding']; | ||
} | ||
|
||
public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event): ?Union | ||
{ | ||
$call_args = $event->getCallArgs(); | ||
if (count($call_args) === 0) { | ||
return Type::getString(); | ||
} | ||
|
||
$statements_source = $event->getStatementsSource(); | ||
$nodeTypeProvider = $statements_source->getNodeTypeProvider(); | ||
$codebase = $statements_source->getCodebase(); | ||
|
||
$first_arg_type = $nodeTypeProvider->getType($call_args[0]->value); | ||
if ($first_arg_type === null) { | ||
if ($codebase->analysis_php_version_id >= 8_00_00) { | ||
return new Union([new TString(), new TTrue()]); | ||
} else { | ||
return new Union([new TString(), new TBool()]); | ||
} | ||
} | ||
|
||
$has_stringable = false; | ||
$has_tostring = false; | ||
$has_string = false; | ||
$has_null = false; | ||
$has_unknown = false; | ||
|
||
foreach ($first_arg_type->getAtomicTypes() as $atomic_type) { | ||
if ($atomic_type instanceof Type\Atomic\TNamedObject | ||
&& $codebase->classlikes->classImplements($atomic_type->value, 'Stringable') | ||
) { | ||
$has_stringable = true; | ||
continue; | ||
} | ||
|
||
if ($atomic_type instanceof Type\Atomic\TObjectWithProperties | ||
&& isset($atomic_type->methods['__toString']) | ||
) { | ||
$has_tostring = true; | ||
continue; | ||
} | ||
|
||
if ($atomic_type instanceof TString) { | ||
$has_string = true; | ||
continue; | ||
} | ||
|
||
if ($atomic_type instanceof TNull) { | ||
$has_null = true; | ||
continue; | ||
} | ||
|
||
$has_unknown = true; | ||
} | ||
|
||
$list_return_atomics = []; | ||
if ($has_string || $has_stringable || $has_tostring) { | ||
if ($codebase->analysis_php_version_id >= 8_00_00) { | ||
$list_return_atomics[] = new TTrue(); | ||
} else { | ||
$list_return_atomics[] = new TBool(); | ||
} | ||
} | ||
|
||
if ($has_null) { | ||
if ($codebase->analysis_php_version_id >= 8_00_00) { | ||
$list_return_atomics[] = new TString(); | ||
} else { | ||
$list_return_atomics[] = new TFalse(); | ||
} | ||
} | ||
|
||
if ($has_unknown) { | ||
if ($codebase->analysis_php_version_id >= 8_00_00) { | ||
$list_return_atomics[] = new TNever(); | ||
} else { | ||
$list_return_atomics[] = new TNull(); | ||
} | ||
} | ||
|
||
assert($list_return_atomics !== []); | ||
return new Union($list_return_atomics); | ||
} | ||
} |