Skip to content

Commit

Permalink
Fix phpstan baseline
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed Jun 6, 2024
1 parent 0886127 commit 4227dad
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 30 deletions.
11 changes: 10 additions & 1 deletion Controller/RedirectRouteImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<array{
* exception: null|ImportException,
* lineNumber: int,
* lineContent: string,
* }>,
* }
*/
private function importFile(File $file)
{
Expand Down
4 changes: 2 additions & 2 deletions GoneSubscriber/GoneDocumentSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function createRedirects(RemoveEvent $event): void
}

/**
* @return array
* @return string[]
*/
protected function getUrls(BasePageDocument $document)
{
Expand Down Expand Up @@ -165,7 +165,7 @@ protected function getUrls(BasePageDocument $document)
* @param string $webspaceKey
* @param string $locale
*
* @return array
* @return string[]
*/
protected function getHistoryUrls(
ResourceLocatorStrategyInterface $resourceLocatorStrategy,
Expand Down
4 changes: 3 additions & 1 deletion Import/Converter/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion Import/Converter/ConverterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ interface ConverterInterface
/**
* Convert given raw-item to redirect-route.
*
* @param array<string, mixed> $item
*
* @return RedirectRouteInterface
*/
public function convert(array $item);

/**
* Returns true if the item is supported.
* @param array<string, mixed> $item
*
* @return bool
*/
Expand Down
7 changes: 5 additions & 2 deletions Import/Converter/ConverterNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
class ConverterNotFoundException extends ImportException
{
/**
* @var array
* @var array<string, mixed>
*/
private $data;

/**
* @param array<string, mixed> $data
*/
public function __construct(array $data)
{
parent::__construct(sprintf('Data %s is not supported', json_encode($data)));
Expand All @@ -33,7 +36,7 @@ public function __construct(array $data)
/**
* Returns data.
*
* @return array
* @return array<string, mixed>
*/
public function getData()
{
Expand Down
2 changes: 2 additions & 0 deletions Import/FileImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public function import($fileName)
/**
* Import given item.
*
* @param array<string, mixed> $item
*
* @return RedirectRouteInterface
*
* @throws \Exception
Expand Down
5 changes: 4 additions & 1 deletion Import/Reader/CsvReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ public function supports($fileName)
/**
* Interpret given line.
*
* @return array
* @param array<string|int, string> $line
* @param array<string|int, string> $header
*
* @return array<string|int, string|null>
*/
private function interpret(array $line, array $header)
{
Expand Down
7 changes: 5 additions & 2 deletions Import/Reader/ReaderItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ReaderItem
private $lineContent;

/**
* @var array
* @var array<string, mixed>
*/
private $data;

Expand All @@ -38,6 +38,9 @@ class ReaderItem
*/
private $exception;

/**
* @param array<string, mixed> $data
*/
public function __construct(int $lineNumber, string $lineContent, array $data, ?ImportException $exception = null)
{
$this->lineNumber = $lineNumber;
Expand Down Expand Up @@ -69,7 +72,7 @@ public function getLineContent()
/**
* Returns item.
*
* @return array
* @return array<string, mixed>
*/
public function getData()
{
Expand Down
2 changes: 1 addition & 1 deletion Import/Writer/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Writer implements WriterInterface
private $entityManager;

/**
* @var array
* @var string[]
*/
private $sources = [];

Expand Down
2 changes: 1 addition & 1 deletion Manager/RedirectRouteManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
8 changes: 7 additions & 1 deletion Manager/RedirectRouteManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
2 changes: 1 addition & 1 deletion Model/RedirectRouteRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface RedirectRouteRepositoryInterface extends RepositoryInterface
/**
* Find redirect-routes for given id.
*
* @param string $id
* @param string|int $id
*
* @return RedirectRouteInterface|null
*/
Expand Down
5 changes: 4 additions & 1 deletion Routing/RedirectRouteProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ class RedirectRouteProvider implements RouteProviderInterface
private $redirectRouteRepository;

/**
* @var array
* @var array<string, mixed>
*/
private $defaultOptions;

/**
* @param array<string, mixed> $defaultOptions
*/
public function __construct(
RedirectRouteRepositoryInterface $redirectRouteRepository,
array $defaultOptions = []
Expand Down
3 changes: 3 additions & 0 deletions Tests/Application/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public function registerContainerConfiguration(LoaderInterface $loader): void
$loader->load(__DIR__ . '/config/config_' . $this->getContext() . '.yml');
}

/**
* @return array<string, mixed>
*/
protected function getKernelParameters(): array
{
$parameters = parent::getKernelParameters();
Expand Down
30 changes: 27 additions & 3 deletions Tests/Functional/Controller/RedirectRouteControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -41,14 +51,15 @@ 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()
{
$response = $this->post($this->defaultData);

$this->assertHttpStatusCode(200, $response);
/** @var array<string, mixed> $result */
$result = json_decode($response->getContent(), true);

foreach ($this->defaultData as $key => $value) {
Expand All @@ -61,6 +72,7 @@ public function testPost410()
$response = $this->post($this->status410Data);

$this->assertHttpStatusCode(200, $response);
/** @var array<string, mixed> $result */
$result = json_decode($response->getContent(), true);

foreach ($this->status410Data as $key => $value) {
Expand Down Expand Up @@ -98,6 +110,7 @@ public function testPostWithSourceHostAlreadyExists()
public function testPostTriggerActionEnable(): void
{
$response = $this->post(array_merge($this->defaultData, ['enabled' => false]));
/** @var array<string, mixed> $data */
$data = json_decode($response->getContent(), true);

$this->client->request('POST', self::BASE_URL . '/' . $data['id'], [
Expand All @@ -106,33 +119,40 @@ public function testPostTriggerActionEnable(): void
$response = $this->client->getResponse();
$this->assertHttpStatusCode(200, $response);

/** @var array<string, mixed> $content */
$content = json_decode($response->getContent(), true);
$this->assertTrue($content['enabled']);
}

public function testPostTriggerActionDisable(): void
{
$response = $this->post(array_merge($this->defaultData, ['enabled' => true]));
/** @var array<string, mixed> $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<string, mixed> $content */
$content = json_decode($response->getContent(), true);
$this->assertFalse($content['enabled']);
}

public function testGet()
{
$response = $this->post($this->defaultData);
/** @var array<string, mixed> $data */
$data = json_decode($response->getContent(), true);

$response = $this->get($data['id']);

$this->assertHttpStatusCode(200, $response);
/** @var array<string, mixed> $result */
$result = json_decode($response->getContent(), true);

foreach ($this->defaultData as $key => $value) {
Expand All @@ -143,8 +163,10 @@ public function testGet()
public function testPutSameData()
{
$response = $this->post($this->defaultData);
/** @var array<string, mixed> $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);
Expand All @@ -153,11 +175,13 @@ public function testPutSameData()
public function testPutAlreadyExists()
{
$response = $this->post($this->defaultData);
/** @var array<string, mixed> $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);
Expand Down
Loading

0 comments on commit 4227dad

Please sign in to comment.