Skip to content

Commit

Permalink
Psalm fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ProklUng committed Jul 24, 2021
1 parent 7ce8df4 commit 5795b9b
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 10 deletions.
2 changes: 2 additions & 0 deletions DependencyInjection/SymfonyRouterExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 . '/';
}
Expand Down
15 changes: 12 additions & 3 deletions Services/Agnostic/RoutesLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class RoutesLoader
private $checker;

/**
* @var ResourceCheckerConfigCache $cacheFreshChecker
* @var ResourceCheckerConfigCache | null $cacheFreshChecker
*/
private $cacheFreshChecker;

Expand Down Expand Up @@ -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]
);

Expand Down Expand Up @@ -139,6 +139,10 @@ public function getRoutes() : RouteCollection
*/
public function purgeCache() : void
{
if (!$this->cacheDir) {
return;
}

$filesystem = new Filesystem();

if (!$filesystem->exists($this->cacheDir)) {
Expand All @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions Services/Router/Annotations/SearchAnnotatedClasses.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class SearchAnnotatedClasses
{
/**
* @var array $paths Пути, где искать классы.
* @var array|null $paths Пути, где искать классы.
*/
private $paths;

Expand All @@ -37,7 +37,7 @@ class SearchAnnotatedClasses
*/
public function __construct(
string $documentRoot,
array $paths = null
?array $paths = null
) {
$this->paths = $paths;
$this->documentRoot = $documentRoot;
Expand All @@ -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 [];
}

Expand Down
1 change: 1 addition & 0 deletions Services/Router/InitRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public function __construct(
// Роуты бандлов.
$this->mixRoutesBundles();

/** @psalm-suppress UndefinedInterfaceMethod */
$matcher = $this->router->getMatcher();
$matcher->setContext($requestContext);

Expand Down
20 changes: 18 additions & 2 deletions Services/Utils/BitrixRouteConvertor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
);
Expand Down
4 changes: 2 additions & 2 deletions Services/Utils/RouteCheckerExist.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
154 changes: 154 additions & 0 deletions Tests/Cases/BitrixRouteConvertorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

namespace Prokl\BitrixSymfonyRouterBundle\Tests\Cases;

use LogicException;
use Prokl\BitrixSymfonyRouterBundle\Services\Utils\BitrixRouteConvertor;
use Prokl\TestingTools\Base\BaseTestCase;
use Prokl\TestingTools\Tools\PHPUnitUtils;
use ReflectionException;
use Symfony\Component\Routing\RouteCollection;

/**
* Class BitrixRouteConvertorTest
* @package Prokl\BitrixSymfonyRouterBundle\Tests
* @coversDefaultClass BitrixRouteConvertor
*
* @since 24.07.2021
*/
class BitrixRouteConvertorTest extends BaseTestCase
{
/**
* @var BitrixRouteConvertor $obTestObject Тестируемый объект.
*/
protected $obTestObject;

/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();

$this->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;

}
}
5 changes: 5 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
<directory name="/"/>
</errorLevel>
</UndefinedClass>
<UndefinedFunction>
<errorLevel type="suppress">
<directory name="/"/>
</errorLevel>
</UndefinedFunction>
<PropertyNotSetInConstructor>
<errorLevel type="suppress">
<directory name="/"/>
Expand Down

0 comments on commit 5795b9b

Please sign in to comment.