From 5795b9b640e0af78c2fa5dd61efd88fc34b67065 Mon Sep 17 00:00:00 2001 From: ProklUng Date: Sat, 24 Jul 2021 21:27:00 +0300 Subject: [PATCH] Psalm fixes --- .../SymfonyRouterExtension.php | 2 + Services/Agnostic/RoutesLoader.php | 15 +- .../Annotations/SearchAnnotatedClasses.php | 6 +- Services/Router/InitRouter.php | 1 + Services/Utils/BitrixRouteConvertor.php | 20 ++- Services/Utils/RouteCheckerExist.php | 4 +- Tests/Cases/BitrixRouteConvertorTest.php | 154 ++++++++++++++++++ psalm.xml | 5 + 8 files changed, 197 insertions(+), 10 deletions(-) create mode 100644 Tests/Cases/BitrixRouteConvertorTest.php diff --git a/DependencyInjection/SymfonyRouterExtension.php b/DependencyInjection/SymfonyRouterExtension.php index b0f3a1f..02e0c6d 100644 --- a/DependencyInjection/SymfonyRouterExtension.php +++ b/DependencyInjection/SymfonyRouterExtension.php @@ -95,7 +95,9 @@ private function registerRouterConfiguration( } if ($config['default_uri'] === null) { + /** @psalm-suppress PossiblyInvalidCast */ $host = (string)$container->getParameter('kernel.http.host'); + /** @psalm-suppress PossiblyInvalidCast */ $schema = (string)$container->getParameter('kernel.schema'); $config['default_uri'] = $schema . '://' . $host . '/'; } diff --git a/Services/Agnostic/RoutesLoader.php b/Services/Agnostic/RoutesLoader.php index 8356054..de9121c 100644 --- a/Services/Agnostic/RoutesLoader.php +++ b/Services/Agnostic/RoutesLoader.php @@ -43,7 +43,7 @@ class RoutesLoader private $checker; /** - * @var ResourceCheckerConfigCache $cacheFreshChecker + * @var ResourceCheckerConfigCache | null $cacheFreshChecker */ private $cacheFreshChecker; @@ -98,7 +98,7 @@ public function __construct( if ($cacheDir) { $this->cacheFreshChecker = new ResourceCheckerConfigCache( - $this->cacheDir . '/url_generating_routes.php', + (string)$this->cacheDir . '/url_generating_routes.php', [$this->checker] ); @@ -139,6 +139,10 @@ public function getRoutes() : RouteCollection */ public function purgeCache() : void { + if (!$this->cacheDir) { + return; + } + $filesystem = new Filesystem(); if (!$filesystem->exists($this->cacheDir)) { @@ -155,9 +159,14 @@ public function purgeCache() : void */ private function warmUpCache() : void { + if (!$this->cacheDir) { + return; + } + + /** @psalm-suppress UndefinedInterfaceMethod */ $this->router->setConfigCacheFactory($this->cacheFactory); - if (!$this->cacheFreshChecker->isFresh()) { + if ($this->cacheFreshChecker !== null && !$this->cacheFreshChecker->isFresh()) { if (!@file_exists($this->cacheDir)) { @mkdir($this->cacheDir, 0777); } diff --git a/Services/Router/Annotations/SearchAnnotatedClasses.php b/Services/Router/Annotations/SearchAnnotatedClasses.php index 30b5a68..0c675eb 100644 --- a/Services/Router/Annotations/SearchAnnotatedClasses.php +++ b/Services/Router/Annotations/SearchAnnotatedClasses.php @@ -15,7 +15,7 @@ class SearchAnnotatedClasses { /** - * @var array $paths Пути, где искать классы. + * @var array|null $paths Пути, где искать классы. */ private $paths; @@ -37,7 +37,7 @@ class SearchAnnotatedClasses */ public function __construct( string $documentRoot, - array $paths = null + ?array $paths = null ) { $this->paths = $paths; $this->documentRoot = $documentRoot; @@ -50,7 +50,7 @@ public function __construct( */ public function collect() : array { - if (count($this->paths) === 0) { + if ($this->paths === null || count($this->paths) === 0) { return []; } diff --git a/Services/Router/InitRouter.php b/Services/Router/InitRouter.php index 46c0c3f..3250ec0 100644 --- a/Services/Router/InitRouter.php +++ b/Services/Router/InitRouter.php @@ -127,6 +127,7 @@ public function __construct( // Роуты бандлов. $this->mixRoutesBundles(); + /** @psalm-suppress UndefinedInterfaceMethod */ $matcher = $this->router->getMatcher(); $matcher->setContext($requestContext); diff --git a/Services/Utils/BitrixRouteConvertor.php b/Services/Utils/BitrixRouteConvertor.php index 663c7be..55b0d61 100644 --- a/Services/Utils/BitrixRouteConvertor.php +++ b/Services/Utils/BitrixRouteConvertor.php @@ -132,13 +132,13 @@ public function convertRoute(string $name, Route $route, RoutingConfigurator $ro * @param string $name Название роута. * @param array|string $controller Контроллер. * - * @return array + * @return string[] * @throws LogicException */ private function parseControllerString(string $name, $controller) : array { $argument = $controller; - if (is_string($controller) && $controller) { + if (is_string($controller) && $controller !== '') { if (strpos($controller, '::') !== false) { $controller = explode('::', $controller, 2); if (strpos($controller[1], 'Action') === false) { @@ -162,6 +162,22 @@ private function parseControllerString(string $name, $controller) : array } } + if (is_array($controller)) { + if (strpos($controller[1], 'Action') === false) { + // В методе контроллера обязательно должно содержаться Action + // (особенность битриксовых контроллеров) + throw new LogicException( + sprintf( + 'Route %s. Action %s name of controller must contain Action.', + $name, + (string)$controller[0] + ) + ); + } + + return $controller; + } + throw new LogicException( sprintf('Route %s. Invalid _controller param.', $name) ); diff --git a/Services/Utils/RouteCheckerExist.php b/Services/Utils/RouteCheckerExist.php index 6b39451..2dbf936 100644 --- a/Services/Utils/RouteCheckerExist.php +++ b/Services/Utils/RouteCheckerExist.php @@ -42,8 +42,8 @@ public function check() : void if (is_string($controller)) { if (strpos($controller, '::') !== false) { $callback = explode('::', $controller, 2); - $class = (string)$callback[0]; - $method = (string)$callback[1]; + $class = $callback[0]; + $method = $callback[1]; } else { // __invoke $class = $controller; diff --git a/Tests/Cases/BitrixRouteConvertorTest.php b/Tests/Cases/BitrixRouteConvertorTest.php new file mode 100644 index 0000000..8db8366 --- /dev/null +++ b/Tests/Cases/BitrixRouteConvertorTest.php @@ -0,0 +1,154 @@ +obTestObject = new BitrixRouteConvertor($this->getRouteCollection()); + } + + /** + * parseControllerString(). Неправильное название action. + * + * @return void + * @throws ReflectionException + */ + public function testParseControllerStringInvalidAction() : void + { + $this->expectException(LogicException::class); + + PHPUnitUtils::callMethod( + $this->obTestObject, + 'parseControllerString', + ['fooName', 'Controller::action'] + ); + } + + /** + * parseControllerString(). Неправильное название action. + * + * @return void + * @throws ReflectionException + */ + public function testParseControllerStringInvalidArgument() : void + { + $this->expectException(LogicException::class); + + PHPUnitUtils::callMethod( + $this->obTestObject, + 'parseControllerString', + ['fooName', ''] + ); + } + + /** + * parseControllerString(). Неправильное название action. + * + * @return void + * @throws ReflectionException + */ + public function testParseControllerStringValidArgument() : void + { + $result = PHPUnitUtils::callMethod( + $this->obTestObject, + 'parseControllerString', + ['fooName', 'Controller::doAction'] + ); + + $this->assertEquals( + ['Controller', 'doAction'], + $result + ); + } + + /** + * parseControllerString(). Неправильное название action. Массив. + * + * @return void + * @throws ReflectionException + */ + public function testParseControllerStringInvalidArray() : void + { + $this->expectException(LogicException::class); + + PHPUnitUtils::callMethod( + $this->obTestObject, + 'parseControllerString', + ['fooName', ['Controller', 'do']] + ); + } + + /** + * parseControllerString(). Массив. + * + * @return void + * @throws ReflectionException + */ + public function testParseControllerStringValidArray() : void + { + $result = PHPUnitUtils::callMethod( + $this->obTestObject, + 'parseControllerString', + ['fooName', ['Controller', 'doAction']] + ); + + $this->assertEquals( + ['Controller', 'doAction'], + $result + ); + } + + /** + * parseControllerString(). Неправильное название action. + * + * @return void + * @throws ReflectionException + */ + public function testParseControllerStringInvoked() : void + { + $this->expectException(LogicException::class); + + PHPUnitUtils::callMethod( + $this->obTestObject, + 'parseControllerString', + ['fooName', 'InvokedController'] + ); + } + + /** + * @return RouteCollection + */ + private function getRouteCollection() : RouteCollection + { + $collection = new RouteCollection(); + + return $collection; + + } +} diff --git a/psalm.xml b/psalm.xml index 1c7ee6d..148dc97 100644 --- a/psalm.xml +++ b/psalm.xml @@ -55,6 +55,11 @@ + + + + +