Skip to content

Commit 9be81c3

Browse files
committed
Revert "Implement property name as an expression in TypesAssignedToPropertiesRule"
This reverts commit fd66714ba06f9df099cb4bf2a182726fd1315c87.
1 parent c1e85c2 commit 9be81c3

File tree

5 files changed

+4
-164
lines changed

5 files changed

+4
-164
lines changed

src/Rules/Properties/FoundPropertyReflection.php

-9
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,17 @@ class FoundPropertyReflection implements PropertyReflection
1414

1515
private PropertyReflection $originalPropertyReflection;
1616

17-
private string $propertyName;
18-
1917
private Type $readableType;
2018

2119
private Type $writableType;
2220

2321
public function __construct(
2422
PropertyReflection $originalPropertyReflection,
25-
string $propertyName,
2623
Type $readableType,
2724
Type $writableType
2825
)
2926
{
3027
$this->originalPropertyReflection = $originalPropertyReflection;
31-
$this->propertyName = $propertyName;
3228
$this->readableType = $readableType;
3329
$this->writableType = $writableType;
3430
}
@@ -38,11 +34,6 @@ public function getDeclaringClass(): ClassReflection
3834
return $this->originalPropertyReflection->getDeclaringClass();
3935
}
4036

41-
public function getName(): string
42-
{
43-
return $this->propertyName;
44-
}
45-
4637
public function isStatic(): bool
4738
{
4839
return $this->originalPropertyReflection->isStatic();

src/Rules/Properties/PropertyDescriptor.php

-9
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,6 @@
77
class PropertyDescriptor
88
{
99

10-
public function describePropertyByName(PropertyReflection $property, string $propertyName): string
11-
{
12-
if (!$property->isStatic()) {
13-
return sprintf('Property %s::$%s', $property->getDeclaringClass()->getDisplayName(), $propertyName);
14-
}
15-
16-
return sprintf('Static property %s::$%s', $property->getDeclaringClass()->getDisplayName(), $propertyName);
17-
}
18-
1910
/**
2011
* @param \PHPStan\Reflection\PropertyReflection $property
2112
* @param \PhpParser\Node\Expr\PropertyFetch|\PhpParser\Node\Expr\StaticPropertyFetch $propertyFetch

src/Rules/Properties/PropertyReflectionFinder.php

-64
Original file line numberDiff line numberDiff line change
@@ -2,79 +2,16 @@
22

33
namespace PHPStan\Rules\Properties;
44

5-
use PhpParser\Node\VarLikeIdentifier;
65
use PHPStan\Analyser\Scope;
7-
use PHPStan\Type\Constant\ConstantStringType;
86
use PHPStan\Type\ObjectType;
97
use PHPStan\Type\StaticType;
108
use PHPStan\Type\ThisType;
119
use PHPStan\Type\Type;
1210
use PHPStan\Type\TypeTraverser;
13-
use PHPStan\Type\TypeUtils;
1411

1512
class PropertyReflectionFinder
1613
{
1714

18-
/**
19-
* @param \PhpParser\Node\Expr\PropertyFetch|\PhpParser\Node\Expr\StaticPropertyFetch $propertyFetch
20-
* @param \PHPStan\Analyser\Scope $scope
21-
* @return FoundPropertyReflection[]
22-
*/
23-
public function findPropertyReflectionsFromNode($propertyFetch, Scope $scope): array
24-
{
25-
if ($propertyFetch instanceof \PhpParser\Node\Expr\PropertyFetch) {
26-
if ($propertyFetch->name instanceof \PhpParser\Node\Identifier) {
27-
$names = [$propertyFetch->name->name];
28-
} else {
29-
$names = array_map(static function (ConstantStringType $name): string {
30-
return $name->getValue();
31-
}, TypeUtils::getConstantStrings($scope->getType($propertyFetch->name)));
32-
}
33-
34-
$reflections = [];
35-
$propertyHolderType = $scope->getType($propertyFetch->var);
36-
$fetchedOnThis = $propertyHolderType instanceof ThisType && $scope->isInClass();
37-
foreach ($names as $name) {
38-
$reflection = $this->findPropertyReflection($propertyHolderType, $name, $scope, $fetchedOnThis);
39-
if ($reflection === null) {
40-
continue;
41-
}
42-
43-
$reflections[] = $reflection;
44-
}
45-
46-
return $reflections;
47-
}
48-
49-
if ($propertyFetch->class instanceof \PhpParser\Node\Name) {
50-
$propertyHolderType = new ObjectType($scope->resolveName($propertyFetch->class));
51-
} else {
52-
$propertyHolderType = $scope->getType($propertyFetch->class);
53-
}
54-
55-
$fetchedOnThis = $propertyHolderType instanceof ThisType && $scope->isInClass();
56-
57-
if ($propertyFetch->name instanceof VarLikeIdentifier) {
58-
$names = [$propertyFetch->name->name];
59-
} else {
60-
$names = array_map(static function (ConstantStringType $name): string {
61-
return $name->getValue();
62-
}, TypeUtils::getConstantStrings($scope->getType($propertyFetch->name)));
63-
}
64-
65-
$reflections = [];
66-
foreach ($names as $name) {
67-
$reflection = $this->findPropertyReflection($propertyHolderType, $name, $scope, $fetchedOnThis);
68-
if ($reflection === null) {
69-
continue;
70-
}
71-
72-
$reflections[] = $reflection;
73-
}
74-
75-
return $reflections;
76-
}
77-
7815
/**
7916
* @param \PhpParser\Node\Expr\PropertyFetch|\PhpParser\Node\Expr\StaticPropertyFetch $propertyFetch
8017
* @param \PHPStan\Analyser\Scope $scope
@@ -131,7 +68,6 @@ private function findPropertyReflection(Type $propertyHolderType, string $proper
13168

13269
return new FoundPropertyReflection(
13370
$originalProperty,
134-
$propertyName,
13571
$readableType,
13672
$writableType
13773
);

src/Rules/Properties/TypesAssignedToPropertiesRule.php

+4-26
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
7-
use PHPStan\Rules\RuleError;
87
use PHPStan\Rules\RuleErrorBuilder;
98
use PHPStan\Rules\RuleLevelHelper;
109
use PHPStan\Type\VerbosityLevel;
@@ -55,32 +54,11 @@ public function processNode(Node $node, Scope $scope): array
5554

5655
/** @var \PhpParser\Node\Expr\PropertyFetch|\PhpParser\Node\Expr\StaticPropertyFetch $propertyFetch */
5756
$propertyFetch = $node->var;
58-
$propertyReflections = $this->propertyReflectionFinder->findPropertyReflectionsFromNode($propertyFetch, $scope);
59-
60-
$errors = [];
61-
foreach ($propertyReflections as $propertyReflection) {
62-
$errors = array_merge($errors, $this->processSingleProperty(
63-
$scope,
64-
$propertyReflection,
65-
$node
66-
));
57+
$propertyReflection = $this->propertyReflectionFinder->findPropertyReflectionFromNode($propertyFetch, $scope);
58+
if ($propertyReflection === null) {
59+
return [];
6760
}
6861

69-
return $errors;
70-
}
71-
72-
/**
73-
* @param Scope $scope
74-
* @param FoundPropertyReflection $propertyReflection
75-
* @param Node\Expr $node
76-
* @return RuleError[]
77-
*/
78-
private function processSingleProperty(
79-
Scope $scope,
80-
FoundPropertyReflection $propertyReflection,
81-
Node\Expr $node
82-
): array
83-
{
8462
$propertyType = $propertyReflection->getWritableType();
8563

8664
if ($node instanceof Node\Expr\Assign) {
@@ -89,7 +67,7 @@ private function processSingleProperty(
8967
$assignedValueType = $scope->getType($node);
9068
}
9169
if (!$this->ruleLevelHelper->accepts($propertyType, $assignedValueType, $scope->isDeclareStrictTypes())) {
92-
$propertyDescription = $this->propertyDescriptor->describePropertyByName($propertyReflection, $propertyReflection->getName());
70+
$propertyDescription = $this->propertyDescriptor->describeProperty($propertyReflection, $propertyFetch);
9371
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($propertyType);
9472

9573
return [

tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php

-56
Original file line numberDiff line numberDiff line change
@@ -91,60 +91,4 @@ public function testBug1216(): void
9191
]);
9292
}
9393

94-
public function testTypesAssignedToPropertiesExpressionNames(): void
95-
{
96-
$this->analyse([__DIR__ . '/data/properties-from-array-into-object.php'], [
97-
[
98-
'Property PropertiesFromArrayIntoObject\Foo::$lall (int) does not accept string.',
99-
42,
100-
],
101-
[
102-
'Property PropertiesFromArrayIntoObject\Foo::$lall (int) does not accept string.',
103-
54,
104-
],
105-
[
106-
'Property PropertiesFromArrayIntoObject\Foo::$test (int|null) does not accept stdClass.',
107-
66,
108-
],
109-
[
110-
'Property PropertiesFromArrayIntoObject\Foo::$lall (int) does not accept string.',
111-
69,
112-
],
113-
[
114-
'Property PropertiesFromArrayIntoObject\Foo::$foo (string) does not accept int.',
115-
73,
116-
],
117-
[
118-
'Property PropertiesFromArrayIntoObject\Foo::$foo (string) does not accept float.',
119-
83,
120-
],
121-
[
122-
'Property PropertiesFromArrayIntoObject\Foo::$lall (int) does not accept string.',
123-
110,
124-
],
125-
[
126-
'Property PropertiesFromArrayIntoObject\FooBar::$foo (string) does not accept float.',
127-
147,
128-
],
129-
]);
130-
}
131-
132-
public function testTypesAssignedToStaticPropertiesExpressionNames(): void
133-
{
134-
$this->analyse([__DIR__ . '/data/properties-from-array-into-static-object.php'], [
135-
[
136-
'Static property PropertiesFromArrayIntoStaticObject\Foo::$lall (stdClass|null) does not accept string.',
137-
29,
138-
],
139-
[
140-
'Static property PropertiesFromArrayIntoStaticObject\Foo::$foo (string) does not accept float.',
141-
36,
142-
],
143-
[
144-
'Static property PropertiesFromArrayIntoStaticObject\FooBar::$foo (string) does not accept float.',
145-
72,
146-
],
147-
]);
148-
}
149-
15094
}

0 commit comments

Comments
 (0)