rector/rules/CodingStyle/Reflection/VendorLocationDetector.php
Tomas Votruba 4d052b08f9 Updated Rector to commit 6cdfebe40c879a70173ff34e147553cf285db883
6cdfebe40c [Windows] Add platform agnostic asserts (#461)
2021-08-16 07:19:49 +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 RectorPrefix20210816\Symplify\SmartFileSystem\Normalizer\PathNormalizer;
final class VendorLocationDetector
{
/**
* @var \Symplify\SmartFileSystem\Normalizer\PathNormalizer
*/
private $pathNormalizer;
public function __construct(\RectorPrefix20210816\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;
}
}