Skip to content

Commit

Permalink
extracted MethodTypeDetector to ease re-use (#63)
Browse files Browse the repository at this point in the history
Co-authored-by: Tomas Votruba <tomas.vot@gmail.com>
  • Loading branch information
staabm and TomasVotruba authored Jun 26, 2023
1 parent 46d3aab commit 1520e42
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 38 deletions.
1 change: 1 addition & 0 deletions config/extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ parameters:

services:
- TomasVotruba\UnusedPublic\PublicClassMethodMatcher
- TomasVotruba\UnusedPublic\MethodTypeDetector
- TomasVotruba\UnusedPublic\ClassTypeDetector
- TomasVotruba\UnusedPublic\ApiDocStmtAnalyzer
- TomasVotruba\UnusedPublic\ClassMethodCallReferenceResolver
Expand Down
44 changes: 6 additions & 38 deletions src/Collectors/PublicClassMethodCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPStan\Reflection\ResolvedMethodReflection;
use TomasVotruba\UnusedPublic\ApiDocStmtAnalyzer;
use TomasVotruba\UnusedPublic\Configuration;
use TomasVotruba\UnusedPublic\MethodTypeDetector;
use TomasVotruba\UnusedPublic\PublicClassMethodMatcher;

/**
Expand All @@ -38,9 +39,10 @@ final class PublicClassMethodCollector implements Collector
];

public function __construct(
private readonly ApiDocStmtAnalyzer $apiDocStmtAnalyzer,
private readonly ApiDocStmtAnalyzer $apiDocStmtAnalyzer,
private readonly PublicClassMethodMatcher $publicClassMethodMatcher,
private readonly Configuration $configuration,
private readonly MethodTypeDetector $methodMatcher,
private readonly Configuration $configuration,
) {
}

Expand All @@ -59,11 +61,11 @@ public function processNode(Node $node, Scope $scope): ?array
return null;
}

if ($this->isTestMethod($node, $scope)) {
if ($this->methodMatcher->isTestMethod($node, $scope)) {
return null;
}

if ($this->isTraitMethod($node, $scope)) {
if ($this->methodMatcher->isTraitMethod($node, $scope)) {
return null;
}

Expand Down Expand Up @@ -105,39 +107,5 @@ public function processNode(Node $node, Scope $scope): ?array
return [$classReflection->getName(), $methodName, $node->getLine()];
}

private function isTestMethod(ClassMethod $classMethod, Scope $scope): bool
{
$classMethodName = $classMethod->name->toString();
if (str_starts_with($classMethodName, 'test')) {
return true;
}

$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return false;
}

$extendedMethodReflection = $classReflection->getMethod($classMethodName, $scope);

if ($extendedMethodReflection->getDocComment() === null) {
return false;
}

return str_contains($extendedMethodReflection->getDocComment(), '@test');
}

private function isTraitMethod(ClassMethod $classMethod, Scope $scope): bool
{
$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return false;
}

$extendedMethodReflection = $classReflection->getMethod($classMethod->name->toString(), $scope);
if ($extendedMethodReflection instanceof PhpMethodReflection || $extendedMethodReflection instanceof ResolvedMethodReflection) {
return $extendedMethodReflection->getDeclaringTrait() instanceof ClassReflection;
}

return false;
}
}
51 changes: 51 additions & 0 deletions src/MethodTypeDetector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace TomasVotruba\UnusedPublic;

use PhpParser\Comment\Doc;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\Php\PhpMethodReflection;
use PHPStan\Reflection\ResolvedMethodReflection;

final class MethodTypeDetector
{
public function isTestMethod(ClassMethod $classMethod, Scope $scope): bool
{
$classMethodName = $classMethod->name->toString();
if (str_starts_with($classMethodName, 'test')) {
return true;
}

$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return false;
}

$extendedMethodReflection = $classReflection->getMethod($classMethodName, $scope);

if ($extendedMethodReflection->getDocComment() === null) {
return false;
}

return str_contains($extendedMethodReflection->getDocComment(), '@test');
}

public function isTraitMethod(ClassMethod $classMethod, Scope $scope): bool
{
$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return false;
}

$extendedMethodReflection = $classReflection->getMethod($classMethod->name->toString(), $scope);
if ($extendedMethodReflection instanceof PhpMethodReflection || $extendedMethodReflection instanceof ResolvedMethodReflection) {
return $extendedMethodReflection->getDeclaringTrait() instanceof ClassReflection;
}

return false;
}
}

0 comments on commit 1520e42

Please sign in to comment.