Skip to content

Commit 4445a52

Browse files
More
1 parent b1a67bd commit 4445a52

File tree

7 files changed

+68
-77
lines changed

7 files changed

+68
-77
lines changed

src/Reflection/RequireExtension/RequireExtendsPropertiesClassReflectionExtension.php

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,8 @@ public function hasInstanceProperty(ClassReflection $classReflection, string $pr
4444
return $this->findProperty(
4545
$classReflection,
4646
$propertyName,
47-
// TODO: Use hasInstanceProperty
48-
static fn (Type $type, string $propertyName): TrinaryLogic => $type->hasProperty($propertyName),
49-
// TODO: Use getInstanceProperty
50-
static fn (Type $type, string $propertyName): ExtendedPropertyReflection => $type->getProperty($propertyName, new OutOfClassScope())
47+
static fn (Type $type, string $propertyName): TrinaryLogic => $type->hasInstanceProperty($propertyName),
48+
static fn (Type $type, string $propertyName): ExtendedPropertyReflection => $type->getInstanceProperty($propertyName, new OutOfClassScope())
5149
) !== null;
5250
}
5351

@@ -56,10 +54,8 @@ public function getInstanceProperty(ClassReflection $classReflection, string $pr
5654
$property = $this->findProperty(
5755
$classReflection,
5856
$propertyName,
59-
// TODO: Use hasInstanceProperty
60-
static fn (Type $type, string $propertyName): TrinaryLogic => $type->hasProperty($propertyName),
61-
// TODO: Use getInstanceProperty
62-
static fn (Type $type, string $propertyName): ExtendedPropertyReflection => $type->getProperty($propertyName, new OutOfClassScope())
57+
static fn (Type $type, string $propertyName): TrinaryLogic => $type->hasInstanceProperty($propertyName),
58+
static fn (Type $type, string $propertyName): ExtendedPropertyReflection => $type->getInstanceProperty($propertyName, new OutOfClassScope())
6359
);
6460
if ($property === null) {
6561
throw new ShouldNotHappenException();
@@ -73,10 +69,8 @@ public function hasStaticProperty(ClassReflection $classReflection, string $prop
7369
return $this->findProperty(
7470
$classReflection,
7571
$propertyName,
76-
// TODO: Use hasStaticProperty
77-
static fn (Type $type, string $propertyName): TrinaryLogic => $type->hasProperty($propertyName),
78-
// TODO: Use getStaticProperty
79-
static fn (Type $type, string $propertyName): ExtendedPropertyReflection => $type->getProperty($propertyName, new OutOfClassScope())
72+
static fn (Type $type, string $propertyName): TrinaryLogic => $type->hasStaticProperty($propertyName),
73+
static fn (Type $type, string $propertyName): ExtendedPropertyReflection => $type->getStaticProperty($propertyName, new OutOfClassScope())
8074
) !== null;
8175
}
8276

@@ -85,10 +79,8 @@ public function getStaticProperty(ClassReflection $classReflection, string $prop
8579
$property = $this->findProperty(
8680
$classReflection,
8781
$propertyName,
88-
// TODO: Use hasStaticProperty
89-
static fn (Type $type, string $propertyName): TrinaryLogic => $type->hasProperty($propertyName),
90-
// TODO: Use getStaticProperty
91-
static fn (Type $type, string $propertyName): ExtendedPropertyReflection => $type->getProperty($propertyName, new OutOfClassScope())
82+
static fn (Type $type, string $propertyName): TrinaryLogic => $type->hasStaticProperty($propertyName),
83+
static fn (Type $type, string $propertyName): ExtendedPropertyReflection => $type->getStaticProperty($propertyName, new OutOfClassScope())
9284
);
9385
if ($property === null) {
9486
throw new ShouldNotHappenException();

src/Rules/Properties/AccessPropertiesCheck.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ private function processSingleProperty(Scope $scope, PropertyFetch $node, string
9191
$scope,
9292
NullsafeOperatorHelper::getNullsafeShortcircuitedExprRespectingScope($scope, $node->var),
9393
sprintf('Access to property $%s on an unknown class %%s.', SprintfHelper::escapeFormatString($name)),
94-
// TODO use hasInstanceProperty
95-
static fn (Type $type): bool => $type->canAccessProperties()->yes() && $type->hasProperty($name)->yes(),
94+
static fn (Type $type): bool => $type->canAccessProperties()->yes() && $type->hasInstanceProperty($name)->yes(),
9695
);
9796
$type = $typeResult->getType();
9897
if ($type instanceof ErrorType) {
@@ -118,8 +117,7 @@ private function processSingleProperty(Scope $scope, PropertyFetch $node, string
118117
];
119118
}
120119

121-
// TODO use hasInstanceProperty
122-
$has = $type->hasProperty($name);
120+
$has = $type->hasInstanceProperty($name);
123121
if (!$has->no() && $this->canAccessUndefinedProperties($scope, $node)) {
124122
return [];
125123
}
@@ -183,6 +181,16 @@ private function processSingleProperty(Scope $scope, PropertyFetch $node, string
183181
}
184182
}
185183

184+
if ($type->hasStaticProperty($name)->yes()) {
185+
return [
186+
RuleErrorBuilder::message(sprintf(
187+
'Non-static access to static property %s::$%s.',
188+
$type->getStaticProperty($name, $scope)->getDeclaringClass()->getDisplayName(),
189+
$name,
190+
))->identifier('staticProperty.nonStaticAccess')->build(),
191+
];
192+
}
193+
186194
$ruleErrorBuilder = RuleErrorBuilder::message(sprintf(
187195
'Access to an undefined property %s::$%s.',
188196
$typeForDescribe->describe(VerbosityLevel::typeOnly()),
@@ -199,18 +207,7 @@ private function processSingleProperty(Scope $scope, PropertyFetch $node, string
199207
];
200208
}
201209

202-
// TODO use getInstanceProperty
203-
$propertyReflection = $type->getProperty($name, $scope);
204-
if ($propertyReflection->isStatic()) {
205-
return [
206-
RuleErrorBuilder::message(sprintf(
207-
'Non-static access to static property %s::$%s.',
208-
$propertyReflection->getDeclaringClass()->getDisplayName(),
209-
$name,
210-
))->identifier('staticProperty.nonStaticAccess')->build(),
211-
];
212-
}
213-
210+
$propertyReflection = $type->getInstanceProperty($name, $scope);
214211
if ($write) {
215212
if ($scope->canWriteProperty($propertyReflection)) {
216213
return [];

src/Rules/Properties/AccessStaticPropertiesRule.php

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,7 @@ private function processSingleProperty(Scope $scope, StaticPropertyFetch $node,
155155
$scope,
156156
NullsafeOperatorHelper::getNullsafeShortcircuitedExprRespectingScope($scope, $node->class),
157157
sprintf('Access to static property $%s on an unknown class %%s.', SprintfHelper::escapeFormatString($name)),
158-
// TODO Use hasStaticProperty
159-
static fn (Type $type): bool => $type->canAccessProperties()->yes() && $type->hasProperty($name)->yes(),
158+
static fn (Type $type): bool => $type->canAccessProperties()->yes() && $type->hasStaticProperty($name)->yes(),
160159
);
161160
$classType = $classTypeResult->getType();
162161
if ($classType instanceof ErrorType) {
@@ -188,8 +187,7 @@ private function processSingleProperty(Scope $scope, StaticPropertyFetch $node,
188187
]);
189188
}
190189

191-
// TODO Use hasStaticProperty
192-
$has = $classType->hasProperty($name);
190+
$has = $classType->hasStaticProperty($name);
193191
if (!$has->no() && $scope->isUndefinedExpressionAllowed($node)) {
194192
return [];
195193
}
@@ -222,6 +220,23 @@ private function processSingleProperty(Scope $scope, StaticPropertyFetch $node,
222220
}
223221
}
224222

223+
if ($classType->hasInstanceProperty($name)->yes()) {
224+
$hasPropertyTypes = TypeUtils::getHasPropertyTypes($classType);
225+
foreach ($hasPropertyTypes as $hasPropertyType) {
226+
if ($hasPropertyType->getPropertyName() === $name) {
227+
return [];
228+
}
229+
}
230+
231+
return array_merge($messages, [
232+
RuleErrorBuilder::message(sprintf(
233+
'Static access to instance property %s::$%s.',
234+
$classType->getInstanceProperty($name, $scope)->getDeclaringClass()->getDisplayName(),
235+
$name,
236+
))->identifier('property.staticAccess')->build(),
237+
]);
238+
}
239+
225240
return array_merge($messages, [
226241
RuleErrorBuilder::message(sprintf(
227242
'Access to an undefined static property %s::$%s.',
@@ -231,25 +246,7 @@ private function processSingleProperty(Scope $scope, StaticPropertyFetch $node,
231246
]);
232247
}
233248

234-
// TODO Use getStaticProperty and update the if
235249
$property = $classType->getProperty($name, $scope);
236-
if (!$property->isStatic()) {
237-
$hasPropertyTypes = TypeUtils::getHasPropertyTypes($classType);
238-
foreach ($hasPropertyTypes as $hasPropertyType) {
239-
if ($hasPropertyType->getPropertyName() === $name) {
240-
return [];
241-
}
242-
}
243-
244-
return array_merge($messages, [
245-
RuleErrorBuilder::message(sprintf(
246-
'Static access to instance property %s::$%s.',
247-
$property->getDeclaringClass()->getDisplayName(),
248-
$name,
249-
))->identifier('property.staticAccess')->build(),
250-
]);
251-
}
252-
253250
if (!$scope->canReadProperty($property)) {
254251
return array_merge($messages, [
255252
RuleErrorBuilder::message(sprintf(

src/Type/Accessory/HasPropertyType.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public function accepts(Type $type, bool $strictTypes): AcceptsResult
7272

7373
public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
7474
{
75+
// TODO
7576
return new IsSuperTypeOfResult($type->hasProperty($this->propertyName), []);
7677
}
7778

@@ -87,6 +88,7 @@ public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
8788
$limit = IsSuperTypeOfResult::createMaybe();
8889
}
8990

91+
// TODO
9092
return $limit->and(new IsSuperTypeOfResult($otherType->hasProperty($this->propertyName), []));
9193
}
9294

@@ -120,6 +122,26 @@ public function hasProperty(string $propertyName): TrinaryLogic
120122
return TrinaryLogic::createMaybe();
121123
}
122124

125+
// TODO
126+
public function hasInstanceProperty(string $propertyName): TrinaryLogic
127+
{
128+
if ($this->propertyName === $propertyName) {
129+
return TrinaryLogic::createYes();
130+
}
131+
132+
return TrinaryLogic::createMaybe();
133+
}
134+
135+
// TODO
136+
public function hasStaticProperty(string $propertyName): TrinaryLogic
137+
{
138+
if ($this->propertyName === $propertyName) {
139+
return TrinaryLogic::createYes();
140+
}
141+
142+
return TrinaryLogic::createMaybe();
143+
}
144+
123145
public function getCallableParametersAcceptors(ClassMemberAccessAnswerer $scope): array
124146
{
125147
return [new TrivialParametersAcceptor()];

src/Type/ObjectShapeType.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -137,23 +137,6 @@ public function getUnresolvedInstancePropertyPrototype(string $propertyName, Cla
137137
);
138138
}
139139

140-
public function hasStaticProperty(string $propertyName): TrinaryLogic
141-
{
142-
// TODO Change the implementation
143-
return $this->hasInstanceProperty($propertyName);
144-
}
145-
146-
public function getStaticProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection
147-
{
148-
return $this->getUnresolvedStaticPropertyPrototype($propertyName, $scope)->getTransformedProperty();
149-
}
150-
151-
public function getUnresolvedStaticPropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection
152-
{
153-
// TODO Change the implementation
154-
return $this->getUnresolvedInstancePropertyPrototype($propertyName, $scope);
155-
}
156-
157140
public function accepts(Type $type, bool $strictTypes): AcceptsResult
158141
{
159142
if ($type instanceof CompoundType) {

src/Type/Traits/ObjectTypeTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function hasInstanceProperty(string $propertyName): TrinaryLogic
8080

8181
public function getInstanceProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection
8282
{
83-
return $this->getUnresolvedPropertyPrototype($propertyName, $scope)->getTransformedProperty();
83+
return $this->getUnresolvedInstancePropertyPrototype($propertyName, $scope)->getTransformedProperty();
8484
}
8585

8686
public function getUnresolvedInstancePropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection
@@ -101,7 +101,7 @@ public function hasStaticProperty(string $propertyName): TrinaryLogic
101101

102102
public function getStaticProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection
103103
{
104-
return $this->getUnresolvedPropertyPrototype($propertyName, $scope)->getTransformedProperty();
104+
return $this->getUnresolvedStaticPropertyPrototype($propertyName, $scope)->getTransformedProperty();
105105
}
106106

107107
public function getUnresolvedStaticPropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection

tests/PHPStan/Type/BenevolentUnionTypeTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function testCanAccessProperties(BenevolentUnionType $type, TrinaryLogic
4949
);
5050
}
5151

52-
public static function dataHasProperty(): Iterator
52+
public static function dataHasInstanceProperty(): Iterator
5353
{
5454
yield [
5555
new BenevolentUnionType([
@@ -76,10 +76,10 @@ public static function dataHasProperty(): Iterator
7676
];
7777
}
7878

79-
#[DataProvider('dataHasProperty')]
80-
public function testHasProperty(BenevolentUnionType $type, string $propertyName, TrinaryLogic $expectedResult): void
79+
#[DataProvider('dataHasInstanceProperty')]
80+
public function testHasInstanceProperty(BenevolentUnionType $type, string $propertyName, TrinaryLogic $expectedResult): void
8181
{
82-
$actualResult = $type->hasProperty($propertyName);
82+
$actualResult = $type->hasInstanceProperty($propertyName);
8383
$this->assertSame(
8484
$expectedResult->describe(),
8585
$actualResult->describe(),

0 commit comments

Comments
 (0)