Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions src/Provider/VendorUsageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Composer\Autoload\ClassLoader;
use ReflectionClass;
use ReflectionClassConstant;
use ReflectionMethod;
use Reflector;
use function array_keys;
use function strlen;
use function strpos;
Expand Down Expand Up @@ -32,24 +34,40 @@ public function shouldMarkMethodAsUsed(ReflectionMethod $method): ?VirtualUsageD
return null;
}

$reflectionClass = $method->getDeclaringClass();
$methodName = $method->getName();
return $this->shouldMarkMemberAsUsed($method);
}

protected function shouldMarkConstantAsUsed(ReflectionClassConstant $constant): ?VirtualUsageData
{
if (!$this->enabled) {
return null;
}

return $this->shouldMarkMemberAsUsed($constant);
}

$usage = VirtualUsageData::withNote('Method overrides vendor one, thus is expected to be used by vendor code');
/**
* @param ReflectionMethod|ReflectionClassConstant $member
*/
private function shouldMarkMemberAsUsed(Reflector $member): ?VirtualUsageData
{
$reflectionClass = $member->getDeclaringClass();
$memberString = $member instanceof ReflectionMethod ? 'Method' : 'Constant';
$usage = VirtualUsageData::withNote($memberString . ' overrides vendor one, thus is expected to be used by vendor code');

do {
if ($this->isForeignMethod($reflectionClass, $methodName)) {
if ($this->isForeignMember($reflectionClass, $member)) {
return $usage;
}

foreach ($reflectionClass->getInterfaces() as $interface) {
if ($this->isForeignMethod($interface, $methodName)) {
if ($this->isForeignMember($interface, $member)) {
return $usage;
}
}

foreach ($reflectionClass->getTraits() as $trait) {
if ($this->isForeignMethod($trait, $methodName)) {
if ($this->isForeignMember($trait, $member)) {
return $usage;
}
}
Expand All @@ -61,14 +79,19 @@ public function shouldMarkMethodAsUsed(ReflectionMethod $method): ?VirtualUsageD
}

/**
* @param ReflectionMethod|ReflectionClassConstant $member
* @param ReflectionClass<object> $reflectionClass
*/
private function isForeignMethod(
private function isForeignMember(
ReflectionClass $reflectionClass,
string $methodName
Reflector $member
): bool
{
if (!$reflectionClass->hasMethod($methodName)) {
if ($member instanceof ReflectionMethod && !$reflectionClass->hasMethod($member->getName())) {
return false;
}

if ($member instanceof ReflectionClassConstant && !$reflectionClass->hasConstant($member->getName())) {
return false;
}

Expand Down
7 changes: 7 additions & 0 deletions tests/Rule/data/providers/vendor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Properties\ReadWritePropertiesExtensionProvider;
use PHPStan\Rules\Rule as RuleFromVendor;

interface IMyRule extends RuleFromVendor
{
public function getNodeType(): string;
}

interface MyPropertiesExtensionProvider extends ReadWritePropertiesExtensionProvider {

public const EXTENSION_TAG = 'overridden-from-vendor';

}

class MyRule implements IMyRule {

public function getNodeType(): string
Expand Down