mirror of
https://github.com/danog/psalm.git
synced 2025-01-21 21:31:13 +01:00
Rewrite use statements
This commit is contained in:
parent
c6063d1378
commit
4fc5d1d5c8
@ -4,6 +4,7 @@ namespace Psalm\Internal\Analyzer;
|
||||
use PhpParser;
|
||||
use Psalm\Aliases;
|
||||
use Psalm\CodeLocation;
|
||||
use Psalm\Internal\FileManipulation\FileManipulationBuffer;
|
||||
|
||||
trait CanAlias
|
||||
{
|
||||
@ -22,6 +23,11 @@ trait CanAlias
|
||||
*/
|
||||
private $aliased_classes_flipped = [];
|
||||
|
||||
/**
|
||||
* @var array<string, string>
|
||||
*/
|
||||
private $aliased_classes_flipped_replaceable = [];
|
||||
|
||||
/**
|
||||
* @var array<string, string>
|
||||
*/
|
||||
@ -39,6 +45,8 @@ trait CanAlias
|
||||
*/
|
||||
public function visitUse(PhpParser\Node\Stmt\Use_ $stmt)
|
||||
{
|
||||
$codebase = $this->getCodebase();
|
||||
|
||||
foreach ($stmt->uses as $use) {
|
||||
$use_path = implode('\\', $use->name->parts);
|
||||
$use_alias = $use->alias ? $use->alias->name : $use->name->getLast();
|
||||
@ -53,16 +61,32 @@ trait CanAlias
|
||||
break;
|
||||
|
||||
case PhpParser\Node\Stmt\Use_::TYPE_NORMAL:
|
||||
if ($this->getCodebase()->collect_locations) {
|
||||
if ($codebase->collect_locations) {
|
||||
// register the path
|
||||
$codebase = $this->getCodebase();
|
||||
|
||||
$codebase->use_referencing_locations[strtolower($use_path)][] =
|
||||
new \Psalm\CodeLocation($this, $use);
|
||||
|
||||
$codebase->use_referencing_files[$this->getFilePath()][strtolower($use_path)] = true;
|
||||
}
|
||||
|
||||
if ($codebase->alter_code) {
|
||||
if (isset($codebase->class_transforms[strtolower($use_path)])) {
|
||||
$new_fq_class_name = $codebase->class_transforms[strtolower($use_path)];
|
||||
|
||||
$file_manipulations = [];
|
||||
|
||||
$file_manipulations[] = new \Psalm\FileManipulation(
|
||||
(int) $use->getAttribute('startFilePos'),
|
||||
(int) $use->getAttribute('endFilePos') + 1,
|
||||
$new_fq_class_name
|
||||
);
|
||||
|
||||
FileManipulationBuffer::add($this->getFilePath(), $file_manipulations);
|
||||
}
|
||||
|
||||
$this->aliased_classes_flipped_replaceable[strtolower($use_path)] = $use_alias;
|
||||
}
|
||||
|
||||
$this->aliased_classes[strtolower($use_alias)] = $use_path;
|
||||
$this->aliased_class_locations[strtolower($use_alias)] = new CodeLocation($this, $stmt);
|
||||
$this->aliased_classes_flipped[strtolower($use_path)] = $use_alias;
|
||||
@ -80,6 +104,8 @@ trait CanAlias
|
||||
{
|
||||
$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 ? $use->alias->name : $use->name->getLast();
|
||||
@ -94,10 +120,8 @@ trait CanAlias
|
||||
break;
|
||||
|
||||
case PhpParser\Node\Stmt\Use_::TYPE_NORMAL:
|
||||
if ($this->getCodebase()->collect_locations) {
|
||||
if ($codebase->collect_locations) {
|
||||
// register the path
|
||||
$codebase = $this->getCodebase();
|
||||
|
||||
$codebase->use_referencing_locations[strtolower($use_path)][] =
|
||||
new \Psalm\CodeLocation($this, $use);
|
||||
}
|
||||
@ -117,6 +141,14 @@ trait CanAlias
|
||||
return $this->aliased_classes_flipped;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function getAliasedClassesFlippedReplaceable()
|
||||
{
|
||||
return $this->aliased_classes_flipped_replaceable;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Aliases
|
||||
*/
|
||||
|
@ -418,6 +418,18 @@ abstract class ClassLikeAnalyzer extends SourceAnalyzer implements StatementsSou
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function getAliasedClassesFlippedReplaceable()
|
||||
{
|
||||
if ($this->source instanceof NamespaceAnalyzer || $this->source instanceof FileAnalyzer) {
|
||||
return $this->source->getAliasedClassesFlippedReplaceable();
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
|
@ -64,6 +64,11 @@ class FileAnalyzer extends SourceAnalyzer implements StatementsSource
|
||||
*/
|
||||
private $namespace_aliased_classes_flipped = [];
|
||||
|
||||
/**
|
||||
* @var array<string, array<string, string>>
|
||||
*/
|
||||
private $namespace_aliased_classes_flipped_replaceable = [];
|
||||
|
||||
/**
|
||||
* @var array<string, InterfaceAnalyzer>
|
||||
*/
|
||||
@ -232,6 +237,8 @@ class FileAnalyzer extends SourceAnalyzer implements StatementsSource
|
||||
$this->namespace_aliased_classes[$namespace_name] = $namespace_analyzer->getAliases()->uses;
|
||||
$this->namespace_aliased_classes_flipped[$namespace_name] =
|
||||
$namespace_analyzer->getAliasedClassesFlipped();
|
||||
$this->namespace_aliased_classes_flipped_replaceable[$namespace_name] =
|
||||
$namespace_analyzer->getAliasedClassesFlippedReplaceable();
|
||||
} elseif ($stmt instanceof PhpParser\Node\Stmt\Use_) {
|
||||
$this->visitUse($stmt);
|
||||
} elseif ($stmt instanceof PhpParser\Node\Stmt\GroupUse) {
|
||||
@ -389,6 +396,20 @@ class FileAnalyzer extends SourceAnalyzer implements StatementsSource
|
||||
return $this->aliased_classes_flipped;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $namespace_name
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function getAliasedClassesFlippedReplaceable($namespace_name = null)
|
||||
{
|
||||
if ($namespace_name && isset($this->namespace_aliased_classes_flipped_replaceable[$namespace_name])) {
|
||||
return $this->namespace_aliased_classes_flipped_replaceable[$namespace_name];
|
||||
}
|
||||
|
||||
return $this->aliased_classes_flipped_replaceable;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
|
@ -1283,6 +1283,21 @@ abstract class FunctionLikeAnalyzer extends SourceAnalyzer implements Statements
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function getAliasedClassesFlippedReplaceable()
|
||||
{
|
||||
if ($this->source instanceof NamespaceAnalyzer ||
|
||||
$this->source instanceof FileAnalyzer ||
|
||||
$this->source instanceof ClassLikeAnalyzer
|
||||
) {
|
||||
return $this->source->getAliasedClassesFlippedReplaceable();
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
|
@ -33,6 +33,14 @@ abstract class SourceAnalyzer implements StatementsSource
|
||||
return $this->source->getAliasedClassesFlipped();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function getAliasedClassesFlippedReplaceable()
|
||||
{
|
||||
return $this->source->getAliasedClassesFlippedReplaceable();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
|
@ -56,4 +56,12 @@ class TraitAnalyzer extends ClassLikeAnalyzer
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function getAliasedClassesFlippedReplaceable()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
@ -1029,25 +1029,34 @@ class ClassLikes
|
||||
}
|
||||
|
||||
// if we're outside a moved class, but we're changing all references to a class
|
||||
foreach ($codebase->class_transforms as $old_fq_class_name => $new_fq_class_name) {
|
||||
if (strtolower($fq_class_name) === $old_fq_class_name) {
|
||||
$file_manipulations = [];
|
||||
if (isset($codebase->class_transforms[strtolower($fq_class_name)])) {
|
||||
$new_fq_class_name = $codebase->class_transforms[strtolower($fq_class_name)];
|
||||
$file_manipulations = [];
|
||||
|
||||
$file_manipulations[] = new \Psalm\FileManipulation(
|
||||
(int) $class_name_node->getAttribute('startFilePos'),
|
||||
(int) $class_name_node->getAttribute('endFilePos') + 1,
|
||||
Type::getStringFromFQCLN(
|
||||
$new_fq_class_name,
|
||||
$source->getNamespace(),
|
||||
$source->getAliasedClassesFlipped(),
|
||||
$calling_fq_class_name
|
||||
)
|
||||
);
|
||||
$uses_flipped = $source->getAliasedClassesFlipped();
|
||||
$uses_flipped_replaceable = $source->getAliasedClassesFlippedReplaceable();
|
||||
|
||||
FileManipulationBuffer::add($source->getFilePath(), $file_manipulations);
|
||||
|
||||
return true;
|
||||
if (isset($uses_flipped_replaceable[strtolower($fq_class_name)])) {
|
||||
unset($uses_flipped_replaceable[strtolower($fq_class_name)]);
|
||||
$new_class_name_parts = explode('\\', $new_fq_class_name);
|
||||
$class_name = end($new_class_name_parts);
|
||||
$uses_flipped[strtolower($new_fq_class_name)] = $class_name;
|
||||
}
|
||||
|
||||
$file_manipulations[] = new \Psalm\FileManipulation(
|
||||
(int) $class_name_node->getAttribute('startFilePos'),
|
||||
(int) $class_name_node->getAttribute('endFilePos') + 1,
|
||||
Type::getStringFromFQCLN(
|
||||
$new_fq_class_name,
|
||||
$source->getNamespace(),
|
||||
$uses_flipped,
|
||||
$calling_fq_class_name
|
||||
)
|
||||
);
|
||||
|
||||
FileManipulationBuffer::add($source->getFilePath(), $file_manipulations);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
@ -1125,12 +1134,22 @@ class ClassLikes
|
||||
|
||||
$file_manipulations = [];
|
||||
|
||||
$uses_flipped = $source->getAliasedClassesFlipped();
|
||||
$uses_flipped_replaceable = $source->getAliasedClassesFlippedReplaceable();
|
||||
|
||||
if (isset($uses_flipped_replaceable[$old_fq_class_name])) {
|
||||
unset($uses_flipped_replaceable[$old_fq_class_name]);
|
||||
$new_class_name_parts = explode('\\', $new_fq_class_name);
|
||||
$class_name = end($new_class_name_parts);
|
||||
$uses_flipped[strtolower($new_fq_class_name)] = $class_name;
|
||||
}
|
||||
|
||||
$file_manipulations[] = new \Psalm\FileManipulation(
|
||||
$bounds[0],
|
||||
$bounds[1],
|
||||
$type->toNamespacedString(
|
||||
$source->getNamespace(),
|
||||
$source->getAliasedClassesFlipped(),
|
||||
$uses_flipped,
|
||||
null,
|
||||
false
|
||||
)
|
||||
|
@ -13,6 +13,11 @@ interface StatementsSource extends FileSource
|
||||
*/
|
||||
public function getAliasedClassesFlipped();
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function getAliasedClassesFlippedReplaceable();
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
|
@ -331,55 +331,62 @@ class ClassMoveTest extends \Psalm\Tests\TestCase
|
||||
'A' => 'Foo\Bar\Baz\B',
|
||||
]
|
||||
],
|
||||
'moveClassDeeperIntoNamespace' => [
|
||||
'moveClassDeeperIntoNamespaceAdjustUse' => [
|
||||
'<?php
|
||||
namespace Foo;
|
||||
|
||||
use Exception;
|
||||
use ArrayObject;
|
||||
|
||||
class A {
|
||||
/** @var ?Exception */
|
||||
public $x;
|
||||
namespace Foo {
|
||||
use Bar\Bat;
|
||||
|
||||
/**
|
||||
* @param ArrayObject<int, A> $a
|
||||
* @param Bat $b
|
||||
*/
|
||||
public function foo(ArrayObject $a) : Exception {
|
||||
foreach ($a as $b) {
|
||||
$b->bar();
|
||||
}
|
||||
|
||||
return new Exception("bad");
|
||||
}
|
||||
|
||||
public function bar() : void {}
|
||||
function doSomething(Bat $b) : void {}
|
||||
}
|
||||
namespace Bar {
|
||||
class Bat {}
|
||||
}',
|
||||
'<?php
|
||||
namespace Foo\Bar\Baz;
|
||||
|
||||
use Exception;
|
||||
use ArrayObject;
|
||||
|
||||
class B {
|
||||
/** @var null|Exception */
|
||||
public $x;
|
||||
namespace Foo {
|
||||
use Bar\Baz\Bahh;
|
||||
|
||||
/**
|
||||
* @param ArrayObject<int, self> $a
|
||||
* @param Bahh $b
|
||||
*/
|
||||
public function foo(ArrayObject $a) : Exception {
|
||||
foreach ($a as $b) {
|
||||
$b->bar();
|
||||
}
|
||||
|
||||
return new Exception("bad");
|
||||
}
|
||||
|
||||
public function bar() : void {}
|
||||
function doSomething(Bahh $b) : void {}
|
||||
}
|
||||
namespace Bar\Baz {
|
||||
class Bahh {}
|
||||
}',
|
||||
[
|
||||
'Foo\A' => 'Foo\Bar\Baz\B',
|
||||
'Bar\Bat' => 'Bar\Baz\Bahh',
|
||||
]
|
||||
],
|
||||
'moveClassDeeperIntoNamespaceDontAdjustGroupUse' => [
|
||||
'<?php
|
||||
namespace Foo {
|
||||
use Bar\{Bat};
|
||||
|
||||
/**
|
||||
* @param Bat $b
|
||||
*/
|
||||
function doSomething(Bat $b) : void {}
|
||||
}
|
||||
namespace Bar {
|
||||
class Bat {}
|
||||
}',
|
||||
'<?php
|
||||
namespace Foo {
|
||||
use Bar\{Bat};
|
||||
|
||||
/**
|
||||
* @param \Bar\Baz\Bahh $b
|
||||
*/
|
||||
function doSomething(\Bar\Baz\Bahh $b) : void {}
|
||||
}
|
||||
namespace Bar\Baz {
|
||||
class Bahh {}
|
||||
}',
|
||||
[
|
||||
'Bar\Bat' => 'Bar\Baz\Bahh',
|
||||
]
|
||||
],
|
||||
];
|
||||
|
Loading…
x
Reference in New Issue
Block a user