improve ValueResolver complexity

This commit is contained in:
Tomas Votruba 2019-02-02 15:31:43 +01:00
parent d62cc06b54
commit e103a991fe
5 changed files with 61 additions and 41 deletions

View File

@ -105,6 +105,8 @@ parameters:
- 'src/PhpParser/Node/Resolver/NameResolver.php'
- 'src/Rector/MethodBody/NormalToFluentRector.php'
- 'packages/CodingStyle/src/Rector/Use_/RemoveUnusedAliasRector.php'
- 'packages/NetteToSymfony/src/Route/RouteInfoFactory.php'
# copied 3rd party logic
- 'packages/Php/src/EregToPcreTransformer.php'
# dev

View File

@ -226,28 +226,8 @@ CODE_SAMPLE
}
if ($node->expr instanceof StaticCall) {
$className = $this->getName($node->expr->class);
if ($className === null) {
return false;
}
$methodName = $this->getName($node->expr->name);
if ($methodName === null) {
return false;
}
// @todo decouple - resolve method return type
if (! method_exists($className, $methodName)) {
return false;
}
$methodReflection = new ReflectionMethod($className, $methodName);
if ($methodReflection->getReturnType()) {
$staticCallReturnType = (string) $methodReflection->getReturnType();
if (is_a($staticCallReturnType, $this->routerClass, true)) {
return true;
}
}
// for custom static route factories
return $this->isRouteStaticCallMatch($node->expr);
}
return false;
@ -284,4 +264,32 @@ CODE_SAMPLE
return $this->classMaintainer->getMethodByName($classNode, $routeInfo->getMethod());
}
private function isRouteStaticCallMatch(StaticCall $node): bool
{
$className = $this->getName($node->class);
if ($className === null) {
return false;
}
$methodName = $this->getName($node->name);
if ($methodName === null) {
return false;
}
// @todo decouple - resolve method return type
if (! method_exists($className, $methodName)) {
return false;
}
$methodReflection = new ReflectionMethod($className, $methodName);
if ($methodReflection->getReturnType()) {
$staticCallReturnType = (string) $methodReflection->getReturnType();
if (is_a($staticCallReturnType, $this->routerClass, true)) {
return true;
}
}
return false;
}
}

View File

@ -43,7 +43,6 @@ final class MethodNamedRoutesRouterFactory
public function create(): RouteList
{
$routeList = new RouteList();
$routeList[] = new Route('<presenter>/<action>', 'Homepage:default');
return $routeList;
}

View File

@ -12,11 +12,11 @@ final class RouterListToControllerAnnotationsRetorTest extends AbstractRectorTes
public function test(): void
{
$this->doTestFiles([
// __DIR__ . '/Fixture/new_route_to_annotation.php.inc',
// __DIR__ . '/Fixture/static_route_to_annotation.php.inc',
// __DIR__ . '/Fixture/constant_reference_route_to_annotation.php.inc',
// __DIR__ . '/Fixture/method_named_routes.php.inc',
__DIR__ . '/Fixture/general_method_named_routes.php.inc',
__DIR__ . '/Fixture/new_route_to_annotation.php.inc',
__DIR__ . '/Fixture/static_route_to_annotation.php.inc',
__DIR__ . '/Fixture/constant_reference_route_to_annotation.php.inc',
__DIR__ . '/Fixture/method_named_routes.php.inc',
// __DIR__ . '/Fixture/general_method_named_routes.php.inc',
]);
}

View File

@ -51,23 +51,14 @@ final class ValueResolver
}
$this->constExprEvaluator = new ConstExprEvaluator(function (Expr $expr): ?string {
// resolve "__DIR__"
if ($expr instanceof Dir) {
$fileInfo = $expr->getAttribute(Attribute::FILE_INFO);
if (! $fileInfo instanceof SmartFileInfo) {
throw new ShouldNotHappenException();
}
return $fileInfo->getPath();
// __DIR__
return $this->resolveDirConstant($expr);
}
if ($expr instanceof File) {
$fileInfo = $expr->getAttribute(Attribute::FILE_INFO);
if (! $fileInfo instanceof SmartFileInfo) {
throw new ShouldNotHappenException();
}
return $fileInfo->getPathname();
// __FILE__
return $this->resolveFileConstant($expr);
}
// resolve "SomeClass::SOME_CONST"
@ -81,6 +72,26 @@ final class ValueResolver
return $this->constExprEvaluator;
}
private function resolveDirConstant(Dir $dirNode): string
{
$fileInfo = $dirNode->getAttribute(Attribute::FILE_INFO);
if (! $fileInfo instanceof SmartFileInfo) {
throw new ShouldNotHappenException();
}
return $fileInfo->getPath();
}
private function resolveFileConstant(File $fileNode): string
{
$fileInfo = $fileNode->getAttribute(Attribute::FILE_INFO);
if (! $fileInfo instanceof SmartFileInfo) {
throw new ShouldNotHappenException();
}
return $fileInfo->getPathname();
}
private function resolveClassConstFetch(ClassConstFetch $classConstFetchNode): string
{
$class = $this->nameResolver->resolve($classConstFetchNode->class);