From 4227dad9b8250b9754e80c5620066d0e875e96cb Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Thu, 6 Jun 2024 17:18:01 +0200 Subject: [PATCH] Fix phpstan baseline --- Controller/RedirectRouteImportController.php | 11 ++++++- GoneSubscriber/GoneDocumentSubscriber.php | 4 +-- Import/Converter/Converter.php | 4 ++- Import/Converter/ConverterInterface.php | 4 ++- .../Converter/ConverterNotFoundException.php | 7 ++-- Import/FileImport.php | 2 ++ Import/Reader/CsvReader.php | 5 ++- Import/Reader/ReaderItem.php | 7 ++-- Import/Writer/Writer.php | 2 +- Manager/RedirectRouteManager.php | 2 +- Manager/RedirectRouteManagerInterface.php | 8 ++++- Model/RedirectRouteRepositoryInterface.php | 2 +- Routing/RedirectRouteProvider.php | 5 ++- Tests/Application/Kernel.php | 3 ++ .../RedirectRouteControllerTest.php | 30 +++++++++++++++-- phpstan-baseline.neon | 32 ++++++++++++------- phpstan.neon | 1 - 17 files changed, 99 insertions(+), 30 deletions(-) diff --git a/Controller/RedirectRouteImportController.php b/Controller/RedirectRouteImportController.php index ab674b0..def657a 100644 --- a/Controller/RedirectRouteImportController.php +++ b/Controller/RedirectRouteImportController.php @@ -14,6 +14,7 @@ use Sulu\Bundle\RedirectBundle\Admin\RedirectAdmin; use Sulu\Bundle\RedirectBundle\Import\Converter\ConverterNotFoundException; use Sulu\Bundle\RedirectBundle\Import\FileImportInterface; +use Sulu\Bundle\RedirectBundle\Import\ImportException; use Sulu\Bundle\RedirectBundle\Import\Item; use Sulu\Bundle\RedirectBundle\Import\Reader\ReaderNotFoundException; use Sulu\Component\Security\SecuredControllerInterface; @@ -88,7 +89,15 @@ public function postAction(Request $request) /** * Import given file and returns serializable response. * - * @return array + * @return array{ + * fileName: string, + * total: int, + * exceptions: array, + * } */ private function importFile(File $file) { diff --git a/GoneSubscriber/GoneDocumentSubscriber.php b/GoneSubscriber/GoneDocumentSubscriber.php index 7b17e07..2354319 100644 --- a/GoneSubscriber/GoneDocumentSubscriber.php +++ b/GoneSubscriber/GoneDocumentSubscriber.php @@ -112,7 +112,7 @@ public function createRedirects(RemoveEvent $event): void } /** - * @return array + * @return string[] */ protected function getUrls(BasePageDocument $document) { @@ -165,7 +165,7 @@ protected function getUrls(BasePageDocument $document) * @param string $webspaceKey * @param string $locale * - * @return array + * @return string[] */ protected function getHistoryUrls( ResourceLocatorStrategyInterface $resourceLocatorStrategy, diff --git a/Import/Converter/Converter.php b/Import/Converter/Converter.php index b399df0..95db7b5 100644 --- a/Import/Converter/Converter.php +++ b/Import/Converter/Converter.php @@ -45,7 +45,9 @@ public function convert(array $item) { $accessor = PropertyAccess::createPropertyAccessor(); - $entity = $this->repository->findBySource($item[self::SOURCE]); + /** @var string $source */ + $source = $item[self::SOURCE]; + $entity = $this->repository->findBySource($source); if (!$entity) { /** @var RedirectRouteInterface $entity */ $entity = $this->repository->createNew(); diff --git a/Import/Converter/ConverterInterface.php b/Import/Converter/ConverterInterface.php index 9682151..02a7c37 100644 --- a/Import/Converter/ConverterInterface.php +++ b/Import/Converter/ConverterInterface.php @@ -21,12 +21,14 @@ interface ConverterInterface /** * Convert given raw-item to redirect-route. * + * @param array $item + * * @return RedirectRouteInterface */ public function convert(array $item); /** - * Returns true if the item is supported. + * @param array $item * * @return bool */ diff --git a/Import/Converter/ConverterNotFoundException.php b/Import/Converter/ConverterNotFoundException.php index eeffefa..8aa8073 100644 --- a/Import/Converter/ConverterNotFoundException.php +++ b/Import/Converter/ConverterNotFoundException.php @@ -19,10 +19,13 @@ class ConverterNotFoundException extends ImportException { /** - * @var array + * @var array */ private $data; + /** + * @param array $data + */ public function __construct(array $data) { parent::__construct(sprintf('Data %s is not supported', json_encode($data))); @@ -33,7 +36,7 @@ public function __construct(array $data) /** * Returns data. * - * @return array + * @return array */ public function getData() { diff --git a/Import/FileImport.php b/Import/FileImport.php index b4005ce..f16d937 100644 --- a/Import/FileImport.php +++ b/Import/FileImport.php @@ -74,6 +74,8 @@ public function import($fileName) /** * Import given item. * + * @param array $item + * * @return RedirectRouteInterface * * @throws \Exception diff --git a/Import/Reader/CsvReader.php b/Import/Reader/CsvReader.php index 9642b7d..a621601 100644 --- a/Import/Reader/CsvReader.php +++ b/Import/Reader/CsvReader.php @@ -56,7 +56,10 @@ public function supports($fileName) /** * Interpret given line. * - * @return array + * @param array $line + * @param array $header + * + * @return array */ private function interpret(array $line, array $header) { diff --git a/Import/Reader/ReaderItem.php b/Import/Reader/ReaderItem.php index f3813c3..3ac5578 100644 --- a/Import/Reader/ReaderItem.php +++ b/Import/Reader/ReaderItem.php @@ -29,7 +29,7 @@ class ReaderItem private $lineContent; /** - * @var array + * @var array */ private $data; @@ -38,6 +38,9 @@ class ReaderItem */ private $exception; + /** + * @param array $data + */ public function __construct(int $lineNumber, string $lineContent, array $data, ?ImportException $exception = null) { $this->lineNumber = $lineNumber; @@ -69,7 +72,7 @@ public function getLineContent() /** * Returns item. * - * @return array + * @return array */ public function getData() { diff --git a/Import/Writer/Writer.php b/Import/Writer/Writer.php index cf479bb..0a0d711 100644 --- a/Import/Writer/Writer.php +++ b/Import/Writer/Writer.php @@ -33,7 +33,7 @@ class Writer implements WriterInterface private $entityManager; /** - * @var array + * @var string[] */ private $sources = []; diff --git a/Manager/RedirectRouteManager.php b/Manager/RedirectRouteManager.php index 427f129..58717ae 100644 --- a/Manager/RedirectRouteManager.php +++ b/Manager/RedirectRouteManager.php @@ -54,7 +54,7 @@ public function saveByData($data) // update data $redirectRoute->setSource($data['source']); $redirectRoute->setSourceHost($data['sourceHost']); - $redirectRoute->setTarget($data['target']); + $redirectRoute->setTarget((string) $data['target']); $redirectRoute->setStatusCode($data['statusCode']); if (410 === $redirectRoute->getStatusCode()) { diff --git a/Manager/RedirectRouteManagerInterface.php b/Manager/RedirectRouteManagerInterface.php index 1a9a18e..e82ecc8 100644 --- a/Manager/RedirectRouteManagerInterface.php +++ b/Manager/RedirectRouteManagerInterface.php @@ -22,7 +22,13 @@ interface RedirectRouteManagerInterface /** * Save given redirect-route. * - * @param array $data The data of the tag to save + * @param array{ + * source: string, + * sourceHost: string|null, + * target: string|null, + * statusCode: int, + * id?: int|string, + * } $data The data of the tag to save * * @return RedirectRouteInterface * diff --git a/Model/RedirectRouteRepositoryInterface.php b/Model/RedirectRouteRepositoryInterface.php index 6f5bf61..afd5997 100644 --- a/Model/RedirectRouteRepositoryInterface.php +++ b/Model/RedirectRouteRepositoryInterface.php @@ -21,7 +21,7 @@ interface RedirectRouteRepositoryInterface extends RepositoryInterface /** * Find redirect-routes for given id. * - * @param string $id + * @param string|int $id * * @return RedirectRouteInterface|null */ diff --git a/Routing/RedirectRouteProvider.php b/Routing/RedirectRouteProvider.php index d6468fa..c9eca10 100644 --- a/Routing/RedirectRouteProvider.php +++ b/Routing/RedirectRouteProvider.php @@ -29,10 +29,13 @@ class RedirectRouteProvider implements RouteProviderInterface private $redirectRouteRepository; /** - * @var array + * @var array */ private $defaultOptions; + /** + * @param array $defaultOptions + */ public function __construct( RedirectRouteRepositoryInterface $redirectRouteRepository, array $defaultOptions = [] diff --git a/Tests/Application/Kernel.php b/Tests/Application/Kernel.php index a637ec5..fd51fd3 100644 --- a/Tests/Application/Kernel.php +++ b/Tests/Application/Kernel.php @@ -32,6 +32,9 @@ public function registerContainerConfiguration(LoaderInterface $loader): void $loader->load(__DIR__ . '/config/config_' . $this->getContext() . '.yml'); } + /** + * @return array + */ protected function getKernelParameters(): array { $parameters = parent::getKernelParameters(); diff --git a/Tests/Functional/Controller/RedirectRouteControllerTest.php b/Tests/Functional/Controller/RedirectRouteControllerTest.php index 08a8ae7..b080fbb 100644 --- a/Tests/Functional/Controller/RedirectRouteControllerTest.php +++ b/Tests/Functional/Controller/RedirectRouteControllerTest.php @@ -19,12 +19,22 @@ class RedirectRouteControllerTest extends SuluTestCase public const BASE_URL = '/admin/api/redirect-routes'; /** - * @var array + * @var array{ + * source: string, + * sourceHost: string|null, + * target: string|null, + * statusCode: int, + * } */ private $defaultData; /** - * @var array + * @var array{ + * source: string, + * sourceHost: string|null, + * target: string|null, + * statusCode: int, + * } */ private $status410Data; @@ -41,7 +51,7 @@ protected function setUp(): void $this->purgeDatabase(); $this->defaultData = ['source' => '/test1', 'sourceHost' => null, 'target' => '/test2', 'statusCode' => 301]; - $this->status410Data = ['source' => '/test410', 'sourceHost' => null, 'statusCode' => 410, 'target' => null]; + $this->status410Data = ['source' => '/test410', 'sourceHost' => null, 'target' => null, 'statusCode' => 410, ]; } public function testPost() @@ -49,6 +59,7 @@ public function testPost() $response = $this->post($this->defaultData); $this->assertHttpStatusCode(200, $response); + /** @var array $result */ $result = json_decode($response->getContent(), true); foreach ($this->defaultData as $key => $value) { @@ -61,6 +72,7 @@ public function testPost410() $response = $this->post($this->status410Data); $this->assertHttpStatusCode(200, $response); + /** @var array $result */ $result = json_decode($response->getContent(), true); foreach ($this->status410Data as $key => $value) { @@ -98,6 +110,7 @@ public function testPostWithSourceHostAlreadyExists() public function testPostTriggerActionEnable(): void { $response = $this->post(array_merge($this->defaultData, ['enabled' => false])); + /** @var array $data */ $data = json_decode($response->getContent(), true); $this->client->request('POST', self::BASE_URL . '/' . $data['id'], [ @@ -106,6 +119,7 @@ public function testPostTriggerActionEnable(): void $response = $this->client->getResponse(); $this->assertHttpStatusCode(200, $response); + /** @var array $content */ $content = json_decode($response->getContent(), true); $this->assertTrue($content['enabled']); } @@ -113,14 +127,18 @@ public function testPostTriggerActionEnable(): void public function testPostTriggerActionDisable(): void { $response = $this->post(array_merge($this->defaultData, ['enabled' => true])); + /** @var array $data */ $data = json_decode($response->getContent(), true); + $this->assertArrayHasKey('id', $data); + $this->client->request('POST', self::BASE_URL . '/' . $data['id'], [ 'action' => 'disable', ]); $response = $this->client->getResponse(); $this->assertHttpStatusCode(200, $response); + /** @var array $content */ $content = json_decode($response->getContent(), true); $this->assertFalse($content['enabled']); } @@ -128,11 +146,13 @@ public function testPostTriggerActionDisable(): void public function testGet() { $response = $this->post($this->defaultData); + /** @var array $data */ $data = json_decode($response->getContent(), true); $response = $this->get($data['id']); $this->assertHttpStatusCode(200, $response); + /** @var array $result */ $result = json_decode($response->getContent(), true); foreach ($this->defaultData as $key => $value) { @@ -143,8 +163,10 @@ public function testGet() public function testPutSameData() { $response = $this->post($this->defaultData); + /** @var array $data */ $data = json_decode($response->getContent(), true); $this->assertHttpStatusCode(200, $response); + $this->assertArrayHasKey('id', $data); $response = $this->put($data['id'], $this->defaultData); $this->assertHttpStatusCode(200, $response); @@ -153,11 +175,13 @@ public function testPutSameData() public function testPutAlreadyExists() { $response = $this->post($this->defaultData); + /** @var array $data */ $data = json_decode($response->getContent(), true); $this->assertHttpStatusCode(200, $response); $response = $this->post(array_merge($this->defaultData, ['source' => '/test2'])); $this->assertHttpStatusCode(200, $response); + $this->assertArrayHasKey('id', $data); $response = $this->put($data['id'], array_merge($this->defaultData, ['source' => '/test2'])); $this->assertHttpStatusCode(409, $response); diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 68435fa..389b837 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,5 +1,15 @@ parameters: ignoreErrors: + - + message: "#^Parameter \\#1 \\$data of method Sulu\\\\Bundle\\\\RedirectBundle\\\\Manager\\\\RedirectRouteManagerInterface\\:\\:saveByData\\(\\) expects array\\{source\\: string, sourceHost\\: string\\|null, target\\: string\\|null, statusCode\\: int, id\\?\\: int\\|string\\}, array\\ given\\.$#" + count: 1 + path: Controller/RedirectRouteController.php + + - + message: "#^Parameter \\#1 \\$data of method Sulu\\\\Bundle\\\\RedirectBundle\\\\Manager\\\\RedirectRouteManagerInterface\\:\\:saveByData\\(\\) expects array\\{source\\: string, sourceHost\\: string\\|null, target\\: string\\|null, statusCode\\: int, id\\?\\: int\\|string\\}, non\\-empty\\-array\\ given\\.$#" + count: 1 + path: Controller/RedirectRouteController.php + - message: "#^Parameter \\#3 \\$route of class Sulu\\\\Component\\\\Rest\\\\ListBuilder\\\\ListRepresentation constructor expects string, mixed given\\.$#" count: 1 @@ -35,6 +45,11 @@ parameters: count: 1 path: Import/Converter/ConverterFacade.php + - + message: "#^Parameter \\#3 \\$data of class Sulu\\\\Bundle\\\\RedirectBundle\\\\Import\\\\Reader\\\\ReaderItem constructor expects array\\, array\\ given\\.$#" + count: 1 + path: Import/Reader/CsvReader.php + - message: "#^Method Sulu\\\\Bundle\\\\RedirectBundle\\\\Import\\\\Reader\\\\ReaderFacade\\:\\:read\\(\\) should return array\\ but returns null\\.$#" count: 1 @@ -90,6 +105,11 @@ parameters: count: 1 path: Manager/RedirectRouteManager.php + - + message: "#^Parameter \\#1 \\$id of class Sulu\\\\Bundle\\\\RedirectBundle\\\\Exception\\\\RedirectRouteNotFoundException constructor expects int, int\\\\|int\\<1, max\\>\\|string given\\.$#" + count: 1 + path: Manager/RedirectRouteManager.php + - message: "#^Interface Sulu\\\\Bundle\\\\RedirectBundle\\\\Model\\\\RedirectRouteRepositoryInterface extends generic interface Sulu\\\\Component\\\\Persistence\\\\Repository\\\\RepositoryInterface but does not specify its types\\: T$#" count: 1 @@ -130,14 +150,9 @@ parameters: count: 2 path: Tests/Functional/Controller/RedirectRouteControllerTest.php - - - message: "#^Cannot access offset 'enabled' on mixed\\.$#" - count: 2 - path: Tests/Functional/Controller/RedirectRouteControllerTest.php - - message: "#^Cannot access offset 'id' on mixed\\.$#" - count: 14 + count: 6 path: Tests/Functional/Controller/RedirectRouteControllerTest.php - @@ -150,11 +165,6 @@ parameters: count: 1 path: Tests/Functional/Controller/RedirectRouteControllerTest.php - - - message: "#^Cannot access offset \\(int\\|string\\) on mixed\\.$#" - count: 3 - path: Tests/Functional/Controller/RedirectRouteControllerTest.php - - message: "#^Cannot access offset 0 on mixed\\.$#" count: 1 diff --git a/phpstan.neon b/phpstan.neon index 48bc0bf..eb7045b 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -20,4 +20,3 @@ parameters: constant_hassers: false doctrine: objectManagerLoader: Tests/phpstan/object-manager.php - checkMissingIterableValueType: false