rector/rules/CodingStyle/Reflection/VendorLocationDetector.php
Tomas Votruba c3bc0fc47d Updated Rector to commit d9f47f85729740fe2108d8b0e92e9c97403b6849
d9f47f8572 [DeadCode] Using SideEffectNodeDetector::detectCallExpr() on RemoveOverriddenValuesRector (#554)
2021-07-31 11:34:50 +00:00

53 lines
2.0 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\CodingStyle\Reflection;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\Php\PhpFunctionReflection;
use PHPStan\Reflection\ReflectionWithFilename;
use RectorPrefix20210731\Symplify\SmartFileSystem\Normalizer\PathNormalizer;
final class VendorLocationDetector
{
/**
* @var \Symplify\SmartFileSystem\Normalizer\PathNormalizer
*/
private $pathNormalizer;
public function __construct(\RectorPrefix20210731\Symplify\SmartFileSystem\Normalizer\PathNormalizer $pathNormalizer)
{
$this->pathNormalizer = $pathNormalizer;
}
/**
* @param \PHPStan\Reflection\ReflectionWithFilename|\PHPStan\Reflection\MethodReflection|\PHPStan\Reflection\FunctionReflection $reflection
*/
public function detectFunctionLikeReflection($reflection) : bool
{
$fileName = $this->resolveReflectionFileName($reflection);
// probably internal
if ($fileName === \false) {
return \false;
}
$normalizedFileName = $this->pathNormalizer->normalizePath($fileName);
return \strpos($normalizedFileName, '/vendor/') !== \false;
}
/**
* @param \PHPStan\Reflection\MethodReflection|\PHPStan\Reflection\ReflectionWithFilename|\PHPStan\Reflection\FunctionReflection $reflection
* @return string|bool
*/
private function resolveReflectionFileName($reflection)
{
if ($reflection instanceof \PHPStan\Reflection\ReflectionWithFilename) {
return $reflection->getFileName();
}
if ($reflection instanceof \PHPStan\Reflection\Php\PhpFunctionReflection) {
return $reflection->getFileName();
}
if ($reflection instanceof \PHPStan\Reflection\MethodReflection) {
$declaringClassReflection = $reflection->getDeclaringClass();
return $declaringClassReflection->getFileName();
}
return \false;
}
}