mirror of
https://github.com/danog/psalm.git
synced 2024-11-30 04:39:00 +01:00
Preserve self refs where possible
This commit is contained in:
parent
7d7d4dd926
commit
2369bac943
@ -1149,7 +1149,9 @@ abstract class FunctionLikeAnalyzer extends SourceAnalyzer implements Statements
|
||||
$this,
|
||||
$param_name_node,
|
||||
$resolved_name,
|
||||
$context->calling_method_id
|
||||
$context->calling_method_id,
|
||||
false,
|
||||
true
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -1181,7 +1183,9 @@ abstract class FunctionLikeAnalyzer extends SourceAnalyzer implements Statements
|
||||
$this,
|
||||
$return_name_node,
|
||||
$resolved_name,
|
||||
$context->calling_method_id
|
||||
$context->calling_method_id,
|
||||
false,
|
||||
true
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -974,7 +974,8 @@ class StaticCallAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expression\
|
||||
$stmt->class,
|
||||
$new_fq_class_name,
|
||||
$context->calling_method_id,
|
||||
strtolower($old_declaring_fq_class_name) !== strtolower($new_fq_class_name)
|
||||
strtolower($old_declaring_fq_class_name) !== strtolower($new_fq_class_name),
|
||||
$stmt->class->parts[0] === 'self'
|
||||
)) {
|
||||
$moved_call = true;
|
||||
}
|
||||
@ -1087,7 +1088,9 @@ class StaticCallAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expression\
|
||||
$statements_analyzer,
|
||||
$stmt->class,
|
||||
$fq_class_name,
|
||||
$context->calling_method_id
|
||||
$context->calling_method_id,
|
||||
false,
|
||||
$stmt->class->parts[0] === 'self'
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -97,7 +97,9 @@ class ClassConstFetchAnalyzer
|
||||
$statements_analyzer,
|
||||
$stmt->class,
|
||||
$fq_class_name,
|
||||
$context->calling_method_id
|
||||
$context->calling_method_id,
|
||||
false,
|
||||
$stmt->class->parts[0] === 'self'
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -1027,7 +1027,8 @@ class ClassLikes
|
||||
PhpParser\Node $class_name_node,
|
||||
string $fq_class_name,
|
||||
?string $calling_method_id,
|
||||
bool $force_change = false
|
||||
bool $force_change = false,
|
||||
bool $was_self = false
|
||||
) : bool {
|
||||
$calling_fq_class_name = $source->getFQCLN();
|
||||
|
||||
@ -1050,7 +1051,8 @@ class ClassLikes
|
||||
$source->getFilePath(),
|
||||
(int) $class_name_node->getAttribute('startFilePos'),
|
||||
(int) $class_name_node->getAttribute('endFilePos') + 1,
|
||||
$class_name_node instanceof PhpParser\Node\Scalar\MagicConst\Class_
|
||||
$class_name_node instanceof PhpParser\Node\Scalar\MagicConst\Class_,
|
||||
$was_self
|
||||
);
|
||||
|
||||
return true;
|
||||
@ -1120,7 +1122,8 @@ class ClassLikes
|
||||
$new_fq_class_name,
|
||||
$source_namespace,
|
||||
$uses_flipped,
|
||||
$migrated_source_fqcln
|
||||
$migrated_source_fqcln,
|
||||
$was_self
|
||||
)
|
||||
. ($class_name_node instanceof PhpParser\Node\Scalar\MagicConst\Class_ ? '::class' : '')
|
||||
);
|
||||
@ -1330,7 +1333,8 @@ class ClassLikes
|
||||
string $source_file_path,
|
||||
int $source_start,
|
||||
int $source_end,
|
||||
bool $add_class_constant = false
|
||||
bool $add_class_constant = false,
|
||||
bool $allow_self = false
|
||||
) : void {
|
||||
$project_analyzer = \Psalm\Internal\Analyzer\ProjectAnalyzer::getInstance();
|
||||
$codebase = $project_analyzer->getCodebase();
|
||||
@ -1350,7 +1354,8 @@ class ClassLikes
|
||||
$fq_class_name,
|
||||
$destination_class_storage->aliases->namespace,
|
||||
$destination_class_storage->aliases->uses_flipped,
|
||||
$destination_class_storage->name
|
||||
$destination_class_storage->name,
|
||||
$allow_self
|
||||
) . ($add_class_constant ? '::class' : '')
|
||||
);
|
||||
|
||||
|
@ -1100,9 +1100,10 @@ abstract class Type
|
||||
string $value,
|
||||
?string $namespace,
|
||||
array $aliased_classes,
|
||||
?string $this_class
|
||||
?string $this_class,
|
||||
bool $allow_self = false
|
||||
) : string {
|
||||
if ($value === $this_class) {
|
||||
if ($allow_self && $value === $this_class) {
|
||||
return 'self';
|
||||
}
|
||||
|
||||
|
@ -77,7 +77,8 @@ class TNamedObject extends Atomic
|
||||
$use_phpdoc_format
|
||||
);
|
||||
|
||||
return Type::getStringFromFQCLN($this->value, $namespace, $aliased_classes, $this_class) . $intersection_types;
|
||||
return Type::getStringFromFQCLN($this->value, $namespace, $aliased_classes, $this_class, true)
|
||||
. $intersection_types;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -219,6 +219,7 @@ class ClassMoveTest extends \Psalm\Tests\TestCase
|
||||
*/
|
||||
public static function foo(self $one, A $two) : void {
|
||||
A::foo($one, $two);
|
||||
self::foo($one, $two);
|
||||
parent::foo($one, $two);
|
||||
static::foo($one, $two);
|
||||
return new static();
|
||||
@ -248,6 +249,7 @@ class ClassMoveTest extends \Psalm\Tests\TestCase
|
||||
* @return static
|
||||
*/
|
||||
public static function foo(self $one, self $two) : void {
|
||||
B::foo($one, $two);
|
||||
self::foo($one, $two);
|
||||
parent::foo($one, $two);
|
||||
static::foo($one, $two);
|
||||
@ -367,6 +369,7 @@ class ClassMoveTest extends \Psalm\Tests\TestCase
|
||||
|
||||
echo \A::class;
|
||||
echo __CLASS__;
|
||||
echo self::class;
|
||||
|
||||
ArrayObject::foo();
|
||||
|
||||
@ -399,7 +402,8 @@ class ClassMoveTest extends \Psalm\Tests\TestCase
|
||||
|
||||
}
|
||||
|
||||
echo self::class;
|
||||
echo B::class;
|
||||
echo B::class;
|
||||
echo self::class;
|
||||
|
||||
\ArrayObject::foo();
|
||||
|
@ -116,8 +116,8 @@ class MethodMoveTest extends \Psalm\Tests\TestCase
|
||||
|
||||
class B {
|
||||
public static function bar() : void {
|
||||
self::Fe();
|
||||
foreach (self::Fe() as $f) {}
|
||||
B::Fe();
|
||||
foreach (B::Fe() as $f) {}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -356,11 +356,11 @@ class MethodMoveTest extends \Psalm\Tests\TestCase
|
||||
echo A::C;
|
||||
A::Bar();
|
||||
A::Bar();
|
||||
echo self::D;
|
||||
echo B::D;
|
||||
new A();
|
||||
/** @var A */
|
||||
$a = new A();
|
||||
new self();
|
||||
new B();
|
||||
|
||||
return $a;
|
||||
}
|
||||
@ -433,12 +433,12 @@ class MethodMoveTest extends \Psalm\Tests\TestCase
|
||||
echo A::C;
|
||||
echo A::C;
|
||||
self::Blacksheep();
|
||||
self::Blacksheep();
|
||||
echo self::D;
|
||||
B::Blacksheep();
|
||||
echo B::D;
|
||||
new A();
|
||||
/** @var A */
|
||||
$a = new A();
|
||||
new self();
|
||||
new B();
|
||||
|
||||
return $a;
|
||||
}
|
||||
@ -515,11 +515,11 @@ class MethodMoveTest extends \Psalm\Tests\TestCase
|
||||
echo A::C;
|
||||
$this->Bar();
|
||||
A::Bar();
|
||||
echo self::D;
|
||||
echo AChild::D;
|
||||
new A();
|
||||
/** @var A */
|
||||
$a = new A();
|
||||
new self();
|
||||
new AChild();
|
||||
|
||||
return $a;
|
||||
}
|
||||
@ -559,7 +559,7 @@ class MethodMoveTest extends \Psalm\Tests\TestCase
|
||||
|
||||
class B {
|
||||
public static function bar() : void {
|
||||
self::Fe();
|
||||
B::Fe();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -598,7 +598,7 @@ class MethodMoveTest extends \Psalm\Tests\TestCase
|
||||
|
||||
class B {
|
||||
public static function bar() : void {
|
||||
self::Fe();
|
||||
B::Fe();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -652,7 +652,7 @@ class MethodMoveTest extends \Psalm\Tests\TestCase
|
||||
namespace Ns2\Ns3 {
|
||||
class B {
|
||||
public static function bar() : void {
|
||||
self::Fe();
|
||||
B::Fe();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -705,7 +705,7 @@ class MethodMoveTest extends \Psalm\Tests\TestCase
|
||||
|
||||
class B {
|
||||
public static function bar() : void {
|
||||
self::Fedcba();
|
||||
B::Fedcba();
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user