2021-07-19 20:57:26 +00:00
|
|
|
<?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;
|
2021-08-25 07:00:32 +00:00
|
|
|
use RectorPrefix20210825\Symplify\SmartFileSystem\Normalizer\PathNormalizer;
|
2021-07-19 20:57:26 +00:00
|
|
|
final class VendorLocationDetector
|
|
|
|
{
|
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @var \Symplify\SmartFileSystem\Normalizer\PathNormalizer
|
2021-07-19 20:57:26 +00:00
|
|
|
*/
|
|
|
|
private $pathNormalizer;
|
2021-08-25 07:00:32 +00:00
|
|
|
public function __construct(\RectorPrefix20210825\Symplify\SmartFileSystem\Normalizer\PathNormalizer $pathNormalizer)
|
2021-07-19 20:57:26 +00:00
|
|
|
{
|
|
|
|
$this->pathNormalizer = $pathNormalizer;
|
|
|
|
}
|
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @param \PHPStan\Reflection\ReflectionWithFilename|\PHPStan\Reflection\MethodReflection|\PHPStan\Reflection\FunctionReflection $reflection
|
2021-07-19 20:57:26 +00:00
|
|
|
*/
|
|
|
|
public function detectFunctionLikeReflection($reflection) : bool
|
|
|
|
{
|
|
|
|
$fileName = $this->resolveReflectionFileName($reflection);
|
|
|
|
// probably internal
|
|
|
|
if ($fileName === \false) {
|
|
|
|
return \false;
|
|
|
|
}
|
2021-08-16 07:19:49 +00:00
|
|
|
$normalizedFileName = $this->pathNormalizer->normalizePath($fileName, '/');
|
2021-07-19 20:57:26 +00:00
|
|
|
return \strpos($normalizedFileName, '/vendor/') !== \false;
|
|
|
|
}
|
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @param \PHPStan\Reflection\MethodReflection|\PHPStan\Reflection\ReflectionWithFilename|\PHPStan\Reflection\FunctionReflection $reflection
|
2021-07-19 20:57:26 +00:00
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
}
|