1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Fix #1090 - re-fix support for magic get properties

This commit is contained in:
Matthew Brown 2018-11-21 16:57:54 -05:00
parent 3ccd9f4c01
commit 116cbfd349
4 changed files with 32 additions and 26 deletions

View File

@ -256,12 +256,7 @@ class PropertyAssignmentAnalyzer
$property_id = $lhs_type_part->value . '::$' . $prop_name;
$property_ids[] = $property_id;
$statements_analyzer_source = $statements_analyzer->getSource();
if ($codebase->methodExists($lhs_type_part->value . '::__set')
&& (!$statements_analyzer_source instanceof FunctionLikeAnalyzer
|| $statements_analyzer_source->getMethodId() !== $lhs_type_part->value . '::__set')
&& (!$context->self || !$codebase->classExtends($context->self, $lhs_type_part->value))
&& (!$codebase->properties->propertyExists($property_id)
|| ($lhs_var_id !== '$this'
&& $lhs_type_part->value !== $context->self

View File

@ -291,12 +291,7 @@ class PropertyFetchAnalyzer
);
}
$statements_analyzer_source = $statements_analyzer->getSource();
if ($codebase->methodExists($lhs_type_part->value . '::__get')
&& (!$statements_analyzer_source instanceof FunctionLikeAnalyzer
|| $statements_analyzer_source->getMethodId() !== $lhs_type_part->value . '::__get')
&& (!$context->self || !$codebase->classExtends($context->self, $lhs_type_part->value))
&& (!$codebase->properties->propertyExists($property_id)
|| ($stmt_var_id !== '$this'
&& $lhs_type_part->value !== $context->self

View File

@ -323,6 +323,22 @@ class MagicPropertyTest extends TestCase
'assertions' => [],
'error_level' => ['MixedArgument'],
],
'accessInMagicGet' => [
'<?php
class X {
public function __get(string $name) : string {
switch ($name) {
case "a":
return $this->other;
case "other":
return "foo";
}
return "default";
}
}',
'assertions' => [],
'error_level' => ['MixedReturnStatement', 'MixedInferredReturnType'],
],
];
}
@ -612,22 +628,6 @@ class MagicPropertyTest extends TestCase
'error_message' => 'MixedTypeCoercion',
'error_levels' => ['MixedAssignment'],
],
'misnamedPropertyByVariable' => [
'<?php
class B {
/** @var string|null */
public $foo;
public function __get(string $var_name) : ?string {
if ($var_name === "bar") {
return $this->$var_name;
}
return null;
}
}',
'error_message' => 'UndefinedThisPropertyFetch',
],
];
}
}

View File

@ -1809,6 +1809,22 @@ class PropertyTypeTest extends TestCase
}',
'error_message' => 'UndefinedMethod',
],
'misnamedPropertyByVariable' => [
'<?php
class B {
/** @var string|null */
public $foo;
public function bar(string $var_name) : ?string {
if ($var_name === "bar") {
return $this->$var_name;
}
return null;
}
}',
'error_message' => 'UndefinedThisPropertyFetch',
],
];
}
}