Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Aug 9, 2024
1 parent 8daf529 commit 34fbb6f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
22 changes: 22 additions & 0 deletions src/Psalm/Internal/BCHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@
*/
final class BCHelper
{
private const CLASS_MAP = [
Node\Expr\ArrayItem::class => Node\ArrayItem::class,
Node\Expr\ClosureUse::class => Node\ClosureUse::class,
Node\Scalar\DNumber::class => Node\Scalar\Float_::class,
Node\Scalar\Encapsed::class => Node\Scalar\InterpolatedString::class,
Node\Scalar\EncapsedStringPart::class => Node\InterpolatedStringPart::class,
Node\Scalar\LNumber::class => Node\Scalar\Int_::class,
Node\Stmt\DeclareDeclare::class => Node\DeclareItem::class,
Node\Stmt\PropertyProperty::class => Node\PropertyItem::class,
Node\Stmt\StaticVar::class => Node\StaticVar::class,
Node\Stmt\UseUse::class => Node\UseItem::class,
];

public static function getPHPParserClassName(string $className): string
{
if (isset(self::CLASS_MAP[$className]) && class_exists(self::CLASS_MAP[$className])) {
return self::CLASS_MAP[$className];
}

return $className;
}

public static function usePHPParserV4(): bool
{
return class_exists('\PhpParser\Node\Stmt\Throw');
Expand Down
16 changes: 11 additions & 5 deletions src/Psalm/Internal/Provider/ClassLikeStorageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use InvalidArgumentException;
use LogicException;
use Psalm\Internal\BCHelper;
use Psalm\Storage\ClassLikeStorage;

use function array_merge;
Expand Down Expand Up @@ -39,7 +40,7 @@ public function __construct(?ClassLikeStorageCacheProvider $cache = null)
*/
public function get(string $fq_classlike_name): ClassLikeStorage
{
$fq_classlike_name_lc = strtolower($fq_classlike_name);
$fq_classlike_name_lc = $this->formatClassName($fq_classlike_name);
/** @psalm-suppress ImpureStaticProperty Used only for caching */
if (!isset(self::$storage[$fq_classlike_name_lc])) {
throw new InvalidArgumentException('Could not get class storage for ' . $fq_classlike_name_lc);
Expand All @@ -49,20 +50,25 @@ public function get(string $fq_classlike_name): ClassLikeStorage
return self::$storage[$fq_classlike_name_lc];
}

private function formatClassName(string $class): string
{
return strtolower(BCHelper::getPHPParserClassName($class));
}

/**
* @psalm-mutation-free
*/
public function has(string $fq_classlike_name): bool
{
$fq_classlike_name_lc = strtolower($fq_classlike_name);
$fq_classlike_name_lc = $this->formatClassName($fq_classlike_name);

/** @psalm-suppress ImpureStaticProperty Used only for caching */
return isset(self::$storage[$fq_classlike_name_lc]);
}

public function exhume(string $fq_classlike_name, string $file_path, string $file_contents): ClassLikeStorage
{
$fq_classlike_name_lc = strtolower($fq_classlike_name);
$fq_classlike_name_lc = $this->formatClassName($fq_classlike_name);

if (isset(self::$storage[$fq_classlike_name_lc])) {
return self::$storage[$fq_classlike_name_lc];
Expand Down Expand Up @@ -112,7 +118,7 @@ public function makeNew(string $fq_classlike_name_lc): void

public function create(string $fq_classlike_name): ClassLikeStorage
{
$fq_classlike_name_lc = strtolower($fq_classlike_name);
$fq_classlike_name_lc = $this->formatClassName($fq_classlike_name);

$storage = new ClassLikeStorage($fq_classlike_name);
self::$storage[$fq_classlike_name_lc] = $storage;
Expand All @@ -123,7 +129,7 @@ public function create(string $fq_classlike_name): ClassLikeStorage

public function remove(string $fq_classlike_name): void
{
$fq_classlike_name_lc = strtolower($fq_classlike_name);
$fq_classlike_name_lc = $this->formatClassName($fq_classlike_name);

unset(self::$storage[$fq_classlike_name_lc]);
}
Expand Down
12 changes: 11 additions & 1 deletion src/Psalm/Internal/Provider/StatementsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,17 @@ public static function parseStatements(
if (!self::$lexer) {
$major_version = Codebase::transformPhpVersionId($analysis_php_version_id, 10_000);
$minor_version = Codebase::transformPhpVersionId($analysis_php_version_id % 10_000, 100);
self::$lexer = new Emulative(PhpVersion::fromComponents($major_version, $minor_version));

if (class_exists(PhpVersion::class)) {
self::$lexer = new Emulative(PhpVersion::fromComponents($major_version, $minor_version));
} else {
self::$lexer = new Emulative([
'usedAttributes' => [
'comments', 'startLine', 'startFilePos', 'endFilePos',
],
'phpVersion' => $major_version . '.' . $minor_version,
]);
}
}

if (!self::$parser) {
Expand Down

0 comments on commit 34fbb6f

Please sign in to comment.