mirror of
https://github.com/danog/psalm.git
synced 2024-11-30 04:39:00 +01:00
Clean up immutable fix
This commit is contained in:
parent
4d0b91fcd2
commit
06a9ae9c6c
@ -696,7 +696,6 @@ class InstancePropertyAssignmentAnalyzer
|
||||
$property_id,
|
||||
$property_storage,
|
||||
$declaring_class_storage,
|
||||
$assignment_value_type,
|
||||
$context
|
||||
);
|
||||
|
||||
@ -1069,7 +1068,6 @@ class InstancePropertyAssignmentAnalyzer
|
||||
string $property_id,
|
||||
\Psalm\Storage\PropertyStorage $property_storage,
|
||||
\Psalm\Storage\ClassLikeStorage $declaring_class_storage,
|
||||
?Type\Union $assignment_value_type,
|
||||
Context $context
|
||||
): void {
|
||||
$codebase = $statements_analyzer->getCodebase();
|
||||
|
@ -942,7 +942,6 @@ class InstancePropertyFetchAnalyzer
|
||||
$property_id,
|
||||
$property_storage,
|
||||
$declaring_class_storage,
|
||||
null,
|
||||
$context
|
||||
);
|
||||
}
|
||||
|
@ -1,66 +0,0 @@
|
||||
<?php
|
||||
namespace Psalm\Internal\TypeVisitor;
|
||||
|
||||
use PhpParser;
|
||||
use Psalm\CodeLocation;
|
||||
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
||||
use Psalm\IssueBuffer;
|
||||
use Psalm\Issue\ImpurePropertyAssignment;
|
||||
use Psalm\Type\NodeVisitor;
|
||||
use Psalm\Type\Union;
|
||||
use Psalm\Type\Atomic\TClassString;
|
||||
use Psalm\Type\Atomic\TNamedObject;
|
||||
use Psalm\Type\TypeNode;
|
||||
|
||||
class ImmutablePropertyAssignmentVisitor extends NodeVisitor
|
||||
{
|
||||
private $statements_analyzer;
|
||||
private $stmt;
|
||||
|
||||
/** @var bool */
|
||||
public $has_mutation = false;
|
||||
|
||||
public function __construct(
|
||||
StatementsAnalyzer $statements_analyzer,
|
||||
PhpParser\Node\Expr\PropertyFetch $stmt
|
||||
) {
|
||||
$this->statements_analyzer = $statements_analyzer;
|
||||
$this->stmt = $stmt;
|
||||
}
|
||||
|
||||
public function enterNode(TypeNode $type) : ?int
|
||||
{
|
||||
if ($type instanceof Union && $type->reference_free) {
|
||||
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
|
||||
}
|
||||
|
||||
if ($type instanceof TClassString) {
|
||||
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
|
||||
}
|
||||
|
||||
if ($type instanceof TNamedObject) {
|
||||
$codebase = $this->statements_analyzer->getCodebase();
|
||||
|
||||
$object_storage = $codebase->classlike_storage_provider->get(
|
||||
$type->value
|
||||
);
|
||||
|
||||
if (!$object_storage->mutation_free) {
|
||||
if (IssueBuffer::accepts(
|
||||
new ImpurePropertyAssignment(
|
||||
'Cannot store a reference to an externally-mutable object'
|
||||
. ' inside an immutable object – consider using __clone',
|
||||
new CodeLocation($this->statements_analyzer, $this->stmt)
|
||||
),
|
||||
$this->statements_analyzer->getSuppressedIssues()
|
||||
)) {
|
||||
// fall through
|
||||
}
|
||||
|
||||
$this->has_mutation = true;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
@ -115,7 +115,7 @@ class ImmutableAnnotationAdditionTest extends FileManipulationTest
|
||||
['MissingImmutableAnnotation'],
|
||||
true,
|
||||
],
|
||||
'dontAddPureAnnotationWhenClassCanHoldMutableData' => [
|
||||
'addPureAnnotationWhenClassCanHoldMutableData' => [
|
||||
'<?php
|
||||
class B {
|
||||
public int $i = 5;
|
||||
@ -147,6 +147,9 @@ class ImmutableAnnotationAdditionTest extends FileManipulationTest
|
||||
public int $i = 5;
|
||||
}
|
||||
|
||||
/**
|
||||
* @psalm-immutable
|
||||
*/
|
||||
class A {
|
||||
public B $b;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user