Skip to content

Commit

Permalink
Fix methods in IntersectionTypePropertyReflection
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 12, 2023
1 parent d267e51 commit d1c4c6d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 78 deletions.
55 changes: 16 additions & 39 deletions src/Reflection/Type/IntersectionTypePropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,17 @@ public function getDeclaringClass(): ClassReflection

public function isStatic(): bool
{
foreach ($this->properties as $property) {
if ($property->isStatic()) {
return true;
}
}

return false;
return $this->computeResult(static fn (PropertyReflection $property) => $property->isStatic());
}

public function isPrivate(): bool
{
foreach ($this->properties as $property) {
if (!$property->isPrivate()) {
return false;
}
}

return true;
return $this->computeResult(static fn (PropertyReflection $property) => $property->isPrivate());
}

public function isPublic(): bool
{
foreach ($this->properties as $property) {
if ($property->isPublic()) {
return true;
}
}

return false;
return $this->computeResult(static fn (PropertyReflection $property) => $property->isPublic());
}

public function isDeprecated(): TrinaryLogic
Expand Down Expand Up @@ -108,35 +90,30 @@ public function getWritableType(): Type

public function canChangeTypeAfterAssignment(): bool
{
foreach ($this->properties as $property) {
if (!$property->canChangeTypeAfterAssignment()) {
return false;
}
}

return true;
return $this->computeResult(static fn (PropertyReflection $property) => $property->canChangeTypeAfterAssignment());
}

public function isReadable(): bool
{
foreach ($this->properties as $property) {
if (!$property->isReadable()) {
return false;
}
}

return true;
return $this->computeResult(static fn (PropertyReflection $property) => $property->isReadable());
}

public function isWritable(): bool
{
return $this->computeResult(static fn (PropertyReflection $property) => $property->isWritable());
}

/**
* @param callable(PropertyReflection): bool $cb
*/
private function computeResult(callable $cb): bool
{
$result = false;
foreach ($this->properties as $property) {
if (!$property->isWritable()) {
return false;
}
$result = $result || $cb($property);
}

return true;
return $result;
}

}
55 changes: 16 additions & 39 deletions src/Reflection/Type/UnionTypePropertyReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,17 @@ public function getDeclaringClass(): ClassReflection

public function isStatic(): bool
{
foreach ($this->properties as $property) {
if (!$property->isStatic()) {
return false;
}
}

return true;
return $this->computeResult(static fn (PropertyReflection $property) => $property->isStatic());
}

public function isPrivate(): bool
{
foreach ($this->properties as $property) {
if ($property->isPrivate()) {
return true;
}
}

return false;
return $this->computeResult(static fn (PropertyReflection $property) => $property->isPrivate());
}

public function isPublic(): bool
{
foreach ($this->properties as $property) {
if (!$property->isPublic()) {
return false;
}
}

return true;
return $this->computeResult(static fn (PropertyReflection $property) => $property->isPublic());
}

public function isDeprecated(): TrinaryLogic
Expand Down Expand Up @@ -108,35 +90,30 @@ public function getWritableType(): Type

public function canChangeTypeAfterAssignment(): bool
{
foreach ($this->properties as $property) {
if (!$property->canChangeTypeAfterAssignment()) {
return false;
}
}

return true;
return $this->computeResult(static fn (PropertyReflection $property) => $property->canChangeTypeAfterAssignment());
}

public function isReadable(): bool
{
foreach ($this->properties as $property) {
if (!$property->isReadable()) {
return false;
}
}

return true;
return $this->computeResult(static fn (PropertyReflection $property) => $property->isReadable());
}

public function isWritable(): bool
{
return $this->computeResult(static fn (PropertyReflection $property) => $property->isWritable());
}

/**
* @param callable(PropertyReflection): bool $cb
*/
private function computeResult(callable $cb): bool
{
$result = true;
foreach ($this->properties as $property) {
if (!$property->isWritable()) {
return false;
}
$result = $result && $cb($property);
}

return true;
return $result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public function testObjectShapes(): void
'Property object{foo: int, bar?: string}::$foo is not writable.',
18,
],
[
'Property object{foo: int}|stdClass::$foo is not writable.',
42,
],
]);
}

Expand Down
22 changes: 22 additions & 0 deletions tests/PHPStan/Rules/Properties/data/properties-object-shapes.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,26 @@ public function doFoo(object $o): void
$o->baz = 3;
}

/**
* @param object{foo: int}&\stdClass $o
* @return void
*/
public function doIntersection(object $o): void
{
echo $o->foo;

$o->foo = 1;
}

/**
* @param object{foo: int}|\stdClass $o
* @return void
*/
public function doUnion(object $o): void
{
echo $o->foo;

$o->foo = 1;
}

}

0 comments on commit d1c4c6d

Please sign in to comment.