1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00
psalm/examples/plugins/ClassUnqualifier.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

58 lines
2.0 KiB
PHP

<?php
namespace Psalm\Example\Plugin;
use Psalm\FileManipulation;
use Psalm\Internal\Type\TypeTokenizer;
use Psalm\Plugin\EventHandler\AfterClassLikeExistenceCheckInterface;
use Psalm\Plugin\EventHandler\Event\AfterClassLikeExistenceCheckEvent;
use function strpos;
use function strtolower;
use function implode;
use function array_map;
class ClassUnqualifier implements AfterClassLikeExistenceCheckInterface
{
public static function afterClassLikeExistenceCheck(
AfterClassLikeExistenceCheckEvent $event
): void {
$fq_class_name = $event->getFqClassName();
$code_location = $event->getCodeLocation();
$statements_source = $event->getStatementsSource();
$file_replacements = $event->getFileReplacements();
$candidate_type = $code_location->getSelectedText();
$aliases = $statements_source->getAliasedClassesFlipped();
if ($statements_source->getFilePath() !== $code_location->file_path) {
return;
}
if (strpos($candidate_type, '\\' . $fq_class_name) !== false) {
$type_tokens = TypeTokenizer::tokenize($candidate_type, false);
foreach ($type_tokens as &$type_token) {
if ($type_token[0] === ('\\' . $fq_class_name)
&& isset($aliases[strtolower($fq_class_name)])
) {
$type_token[0] = $aliases[strtolower($fq_class_name)];
}
}
unset($type_token);
$new_candidate_type = implode(
'',
array_map(
fn($f) => $f[0],
$type_tokens
)
);
if ($new_candidate_type !== $candidate_type) {
$bounds = $code_location->getSelectionBounds();
$file_replacements[] = new FileManipulation($bounds[0], $bounds[1], $new_candidate_type);
}
$event->setFileReplacements($file_replacements);
}
}
}