Skip to content

Commit

Permalink
Fix one more false positive about unused private property
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 7, 2023
1 parent 35ce48c commit 6d61d3d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ public function hasTraitUse(string $traitName): bool
private function getTraitNames(): array
{
$class = $this->reflection;
$traitNames = $class->getTraitNames();
$traitNames = array_map(static fn (ReflectionClass $class) => $class->getName(), $this->collectTraits($class));
while ($class->getParentClass() !== false) {
$traitNames = array_values(array_unique(array_merge($traitNames, $class->getParentClass()->getTraitNames())));
$class = $class->getParentClass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,11 @@ public function testBug8204(): void
$this->analyse([__DIR__ . '/data/bug-8204.php'], []);
}

public function testBug8850(): void
{
$this->alwaysWrittenTags = [];
$this->alwaysReadTags = [];
$this->analyse([__DIR__ . '/data/bug-8850.php'], []);
}

}
50 changes: 50 additions & 0 deletions tests/PHPStan/Rules/DeadCode/data/bug-8850.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Bug8850;

class Security
{
public function getUser(): string
{
return 'foo';
}
}

final class UserInSessionInRoleEndpointExtension
{
use QueryBuilderHelperTrait;

/** @var Security */
private $security;

public function __construct(
Security $security
) {
$this->security = $security;
}
}

trait QueryBuilderHelperTrait
{
use OrganisationExtensionHelperTrait;
}

trait OrganisationExtensionHelperTrait
{
use UserHelperTrait;

public function getOrganisationIds(): void
{
$user = $this->getUser();
}
}

trait UserHelperTrait
{
public function getUser(): string
{
$user = $this->security->getUser();

return 'foo';
}
}
7 changes: 7 additions & 0 deletions tests/PHPStan/Type/ObjectTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use ArrayAccess;
use ArrayObject;
use Bug8850\UserHelperTrait;
use Bug8850\UserInSessionInRoleEndpointExtension;
use Closure;
use Countable;
use DateInterval;
Expand Down Expand Up @@ -432,6 +434,11 @@ public function dataIsSuperTypeOf(): array
new ObjectType(DateTime::class),
TrinaryLogic::createNo(),
],
61 => [
new ObjectType(UserInSessionInRoleEndpointExtension::class),
new ThisType($reflectionProvider->getClass(UserInSessionInRoleEndpointExtension::class), null, $reflectionProvider->getClass(UserHelperTrait::class)),
TrinaryLogic::createMaybe(),
],
];
}

Expand Down

0 comments on commit 6d61d3d

Please sign in to comment.