1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-10 23:18:40 +01:00
psalm/src/Psalm/Internal/Analyzer/CanAlias.php
Daniil Gentili d0be59e16e
Immutable unions (#8627)
* Immutable CodeLocation

* Remove excess clones

* Remove external clones

* Remove leftover clones

* Fix final clone issue

* Immutable storages

* Refactoring

* Fixes

* Fixes

* Fix

* Fix

* Fixes

* Simplify

* Fixes

* Fix

* Fixes

* Update

* Fix

* Cache global types

* Fix

* Update

* Update

* Fixes

* Fixes

* Refactor

* Fixes

* Fix

* Fix

* More caching

* Fix

* Fix

* Update

* Update

* Fix

* Fixes

* Update

* Refactor

* Update

* Fixes

* Break one more test

* Fix

* FIx

* Fix

* Fix

* Fix

* Fix

* Improve performance and readability

* Equivalent logic

* Fixes

* Revert

* Revert "Revert"

This reverts commit f9175100c8452c80559234200663fd4c4f4dd889.

* Fix

* Fix reference bug

* Make default TypeVisitor immutable

* Bugfix

* Remove clones

* Partial refactoring

* Refactoring

* Fixes

* Fix

* Fixes

* Fixes

* cs-fix

* Fix final bugs

* Add test

* Misc fixes

* Update

* Fixes

* Experiment with removing different property

* revert "Experiment with removing different property"

This reverts commit ac1156e077fc4ea633530d51096d27b6e88bfdf9.

* Uniform naming

* Uniform naming

* Hack hotfix

* Clean up $_FILES ref #8621

* Undo hack, try fixing properly

* Helper method

* Remove redundant call

* Partially fix bugs

* Cleanup

* Change defaults

* Fix bug

* Fix (?, hope this doesn't break anything else)

* cs-fix

* Review fixes

* Bugfix

* Bugfix

* Improve logic

* Update
2022-11-04 19:04:23 +01:00

166 lines
5.4 KiB
PHP

<?php
namespace Psalm\Internal\Analyzer;
use PhpParser;
use Psalm\Aliases;
use Psalm\CodeLocation;
use Psalm\FileManipulation;
use Psalm\Internal\FileManipulation\FileManipulationBuffer;
use function implode;
use function strtolower;
trait CanAlias
{
/**
* @var array<lowercase-string, string>
*/
private $aliased_classes = [];
/**
* @var array<lowercase-string, CodeLocation>
*/
private $aliased_class_locations = [];
/**
* @var array<lowercase-string, string>
*/
private $aliased_classes_flipped = [];
/**
* @var array<lowercase-string, string>
*/
private $aliased_classes_flipped_replaceable = [];
/**
* @var array<lowercase-string, non-empty-string>
*/
private $aliased_functions = [];
/**
* @var array<string, string>
*/
private $aliased_constants = [];
public function visitUse(PhpParser\Node\Stmt\Use_ $stmt): void
{
$codebase = $this->getCodebase();
foreach ($stmt->uses as $use) {
$use_path = implode('\\', $use->name->parts);
$use_path_lc = strtolower($use_path);
$use_alias = $use->alias->name ?? $use->name->getLast();
$use_alias_lc = strtolower($use_alias);
switch ($use->type !== PhpParser\Node\Stmt\Use_::TYPE_UNKNOWN ? $use->type : $stmt->type) {
case PhpParser\Node\Stmt\Use_::TYPE_FUNCTION:
$this->aliased_functions[$use_alias_lc] = $use_path;
break;
case PhpParser\Node\Stmt\Use_::TYPE_CONSTANT:
$this->aliased_constants[$use_alias] = $use_path;
break;
case PhpParser\Node\Stmt\Use_::TYPE_NORMAL:
$codebase->analyzer->addOffsetReference(
$this->getFilePath(),
(int) $use->getAttribute('startFilePos'),
(int) $use->getAttribute('endFilePos'),
$use_path
);
if ($codebase->collect_locations) {
// register the path
$codebase->use_referencing_locations[$use_path_lc][] =
new CodeLocation($this, $use);
}
if ($codebase->alter_code) {
if (isset($codebase->class_transforms[$use_path_lc])) {
$new_fq_class_name = $codebase->class_transforms[$use_path_lc];
$file_manipulations = [];
$file_manipulations[] = new FileManipulation(
(int) $use->getAttribute('startFilePos'),
(int) $use->getAttribute('endFilePos') + 1,
$new_fq_class_name . ($use->alias ? ' as ' . $use_alias : '')
);
FileManipulationBuffer::add($this->getFilePath(), $file_manipulations);
}
$this->aliased_classes_flipped_replaceable[$use_path_lc] = $use_alias;
}
$this->aliased_classes[$use_alias_lc] = $use_path;
$this->aliased_class_locations[$use_alias_lc] = new CodeLocation($this, $stmt);
$this->aliased_classes_flipped[$use_path_lc] = $use_alias;
break;
}
}
}
public function visitGroupUse(PhpParser\Node\Stmt\GroupUse $stmt): void
{
$use_prefix = implode('\\', $stmt->prefix->parts);
$codebase = $this->getCodebase();
foreach ($stmt->uses as $use) {
$use_path = $use_prefix . '\\' . implode('\\', $use->name->parts);
$use_alias = $use->alias->name ?? $use->name->getLast();
switch ($use->type !== PhpParser\Node\Stmt\Use_::TYPE_UNKNOWN ? $use->type : $stmt->type) {
case PhpParser\Node\Stmt\Use_::TYPE_FUNCTION:
$this->aliased_functions[strtolower($use_alias)] = $use_path;
break;
case PhpParser\Node\Stmt\Use_::TYPE_CONSTANT:
$this->aliased_constants[$use_alias] = $use_path;
break;
case PhpParser\Node\Stmt\Use_::TYPE_NORMAL:
if ($codebase->collect_locations) {
// register the path
$codebase->use_referencing_locations[strtolower($use_path)][] =
new CodeLocation($this, $use);
}
$this->aliased_classes[strtolower($use_alias)] = $use_path;
$this->aliased_classes_flipped[strtolower($use_path)] = $use_alias;
break;
}
}
}
/**
* @psalm-mutation-free
* @return array<lowercase-string, string>
*/
public function getAliasedClassesFlipped(): array
{
return $this->aliased_classes_flipped;
}
/**
* @psalm-mutation-free
* @return array<string, string>
*/
public function getAliasedClassesFlippedReplaceable(): array
{
return $this->aliased_classes_flipped_replaceable;
}
/** @psalm-mutation-free */
public function getAliases(): Aliases
{
return new Aliases(
$this->getNamespace(),
$this->aliased_classes,
$this->aliased_functions,
$this->aliased_constants
);
}
}