-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extracted
MethodTypeDetector
to ease re-use (#63)
Co-authored-by: Tomas Votruba <tomas.vot@gmail.com>
- Loading branch information
1 parent
46d3aab
commit 1520e42
Showing
3 changed files
with
58 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |