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

Fix tests

This commit is contained in:
Matthew Brown 2020-01-05 21:58:18 -05:00
parent 867511d7fd
commit 4e85967184
5 changed files with 88 additions and 20 deletions

View File

@ -341,6 +341,8 @@
<xs:element name="UndefinedFunction" type="FunctionIssueHandlerType" minOccurs="0" />
<xs:element name="UndefinedInterface" type="ClassIssueHandlerType" minOccurs="0" />
<xs:element name="UndefinedInterfaceMethod" type="MethodIssueHandlerType" minOccurs="0" />
<xs:element name="UndefinedMagicPropertyAssignment" type="PropertyIssueHandlerType" minOccurs="0" />
<xs:element name="UndefinedMagicPropertyFetch" type="PropertyIssueHandlerType" minOccurs="0" />
<xs:element name="UndefinedMethod" type="MethodIssueHandlerType" minOccurs="0" />
<xs:element name="UndefinedPropertyAssignment" type="PropertyIssueHandlerType" minOccurs="0" />
<xs:element name="UndefinedPropertyFetch" type="PropertyIssueHandlerType" minOccurs="0" />

View File

@ -2304,6 +2304,39 @@ class C {}
interface I extends C {}
```
### UndefinedMagicPropertyAssignment
Emitted when assigning a property on an object that doesnt have that magic property defined
```php
/**
* @property string $bar
*/
class A {
/** @param mixed $value */
public function __set(string $name, $value) {}
}
$a = new A();
$a->foo = "bar";
```
### UndefinedMagicPropertyFetch
Emitted when getting a property on an object that doesnt have that magic property defined
```php
/**
* @property string $bar
*/
class A {
public function __get(string $name) {
return "cool";
}
}
$a = new A();
echo $a->foo;
```
### UndefinedMethod
Emitted when calling a method that doesnt exist

View File

@ -341,6 +341,8 @@ class PropertyAssignmentAnalyzer
$property_id = $fq_class_name . '::$' . $prop_name;
$property_ids[] = $property_id;
$has_magic_setter = false;
if ($codebase->methodExists($fq_class_name . '::__set')
&& (!$codebase->properties->propertyExists($property_id, false, $statements_analyzer, $context)
|| ($lhs_var_id !== '$this'
@ -355,6 +357,7 @@ class PropertyAssignmentAnalyzer
) !== true)
)
) {
$has_magic_setter = true;
$class_storage = $codebase->classlike_storage_provider->get($fq_class_name);
if ($var_id) {
@ -495,15 +498,28 @@ class PropertyAssignmentAnalyzer
// fall through
}
} else {
if (IssueBuffer::accepts(
new UndefinedPropertyAssignment(
'Instance property ' . $property_id . ' is not defined',
new CodeLocation($statements_analyzer->getSource(), $stmt),
$property_id
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
if ($has_magic_setter) {
if (IssueBuffer::accepts(
new UndefinedMagicPropertyAssignment(
'Magic instance property ' . $property_id . ' is not defined',
new CodeLocation($statements_analyzer->getSource(), $stmt),
$property_id
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
} else {
if (IssueBuffer::accepts(
new UndefinedPropertyAssignment(
'Instance property ' . $property_id . ' is not defined',
new CodeLocation($statements_analyzer->getSource(), $stmt),
$property_id
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
}
}

View File

@ -413,6 +413,8 @@ class PropertyFetchAnalyzer
$override_property_visibility = false;
$has_magic_getter = false;
$class_exists = false;
$interface_exists = false;
@ -498,6 +500,8 @@ class PropertyFetchAnalyzer
) !== true)
)
) {
$has_magic_getter = true;
$class_storage = $codebase->classlike_storage_provider->get($fq_class_name);
if (isset($class_storage->pseudo_property_get_types['$' . $prop_name])) {
@ -636,15 +640,28 @@ class PropertyFetchAnalyzer
// fall through
}
} else {
if (IssueBuffer::accepts(
new UndefinedPropertyFetch(
'Instance property ' . $property_id . ' is not defined',
new CodeLocation($statements_analyzer->getSource(), $stmt),
$property_id
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
if ($has_magic_getter) {
if (IssueBuffer::accepts(
new UndefinedMagicPropertyFetch(
'Magic instance property ' . $property_id . ' is not defined',
new CodeLocation($statements_analyzer->getSource(), $stmt),
$property_id
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
} else {
if (IssueBuffer::accepts(
new UndefinedPropertyFetch(
'Instance property ' . $property_id . ' is not defined',
new CodeLocation($statements_analyzer->getSource(), $stmt),
$property_id
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
}
}

View File

@ -653,7 +653,7 @@ class MagicPropertyTest extends TestCase
$a = new A();
$a->bar = 5;',
'error_message' => 'UndefinedPropertyAssignment',
'error_message' => 'UndefinedMagicPropertyAssignment',
],
'propertySealedDocblockDefinedPropertyAssignment' => [
'<?php
@ -719,7 +719,7 @@ class MagicPropertyTest extends TestCase
$a = new A();
echo $a->bar;',
'error_message' => 'UndefinedPropertyFetch',
'error_message' => 'UndefinedMagicPropertyFetch',
],
/**
* The property $foo is not defined on the object, but accessed with the magic setter.