Skip to content

Commit 538b151

Browse files
author
Sengorius
committed
raised PhpStan to level 8 and fixed code-style issues
1 parent bc98655 commit 538b151

26 files changed

Lines changed: 356 additions & 166 deletions

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
"ext-iconv": "*",
1919
"doctrine/doctrine-bundle": "^2.14",
2020
"doctrine/orm": "^3.3",
21-
"symfony/messenger": "^7.",
21+
"symfony/messenger": "^7.3",
2222
"symfony/options-resolver": "^7.3",
2323
"symfony/property-access": "^7.3",
2424
"symfony/property-info": "^7.3",
2525
"symfony/serializer": "^7.3",
2626
"symfony/validator": "^7.3"
2727
},
2828
"require-dev": {
29-
"escapestudios/symfony2-coding-standard": "^3.15",
29+
"escapestudios/symfony2-coding-standard": "^3.16",
3030
"phpstan/phpstan-symfony": "^2.0",
3131
"phpunit/phpunit": "^10.5",
3232
"squizlabs/php_codesniffer": "^3.13",
@@ -57,6 +57,6 @@
5757
"php vendor/bin/phpcs"
5858
],
5959
"phpunit-check": "php vendor/bin/phpunit",
60-
"phpstan-check": "php vendor/bin/phpstan analyse --level 5 src"
60+
"phpstan-check": "php vendor/bin/phpstan analyse --level 8 src"
6161
}
6262
}

src/Component/AbstractApiControllerFactory.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
use function sprintf;
1414
use function strtoupper;
1515

16+
/** @template T of object */
1617
abstract class AbstractApiControllerFactory extends AbstractApiHandlerFactory
1718
{
1819
/**
1920
* @param Request $request
2021
*
21-
* @return array
22+
* @return array<mixed>
2223
*
2324
* @throws ApiProcessException
2425
*/
@@ -46,7 +47,9 @@ protected function unpackRequestContent(Request $request): array
4647
* @param object[] $entities
4748
* @param string[] $groups
4849
*
49-
* @return array
50+
* @phpstan-param T[] $entities
51+
*
52+
* @return array<mixed>
5053
*
5154
* @throws ApiProcessException
5255
*/
@@ -58,10 +61,22 @@ protected function normalizeCollection(array $entities, array $groups = []): arr
5861
/**
5962
* Maps the normalized collection from pagination + pagination data into an array
6063
*
61-
* @param Pagination $pagination
62-
* @param array $groups
64+
* @param Pagination<T> $pagination
65+
* @param string[] $groups
6366
*
64-
* @return array
67+
* @return array{
68+
* 'count': int,
69+
* 'perPage': int,
70+
* 'maxPages': int,
71+
* 'page': int,
72+
* 'lowerBound': int|null,
73+
* 'upperBound': int|null,
74+
* 'isSatisfied': bool,
75+
* 'isEmpty': bool,
76+
* 'isFirstPage': bool,
77+
* 'isLastPage': bool,
78+
* 'items': array<mixed>,
79+
* }
6580
*/
6681
protected function normalizePagination(Pagination $pagination, array $groups = []): array
6782
{

src/Component/AbstractApiHandlerFactory.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ public function setServices(ValidatorInterface $validator, SerializerInterface&N
3333
* @param object $entity
3434
* @param string[] $groups
3535
*
36-
* @return array
36+
* @return array<mixed>|\ArrayObject<int, null>|bool|float|int|string|null
3737
*
3838
* @throws ApiProcessException
3939
*/
40-
protected function normalize(object $entity, array $groups = []): array
40+
protected function normalize(object $entity, array $groups = []): array|\ArrayObject|bool|float|int|string|null
4141
{
4242
$context = [
4343
AbstractNormalizer::GROUPS => $groups,
44-
AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => function ($object, $format, $context): string|int|null {
44+
AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => function (object $object, string|null $format, array $context): string|int|null {
4545
if (method_exists($object, 'getUuid')) {
4646
return $object->getUuid();
4747
}
@@ -64,11 +64,11 @@ protected function normalize(object $entity, array $groups = []): array
6464
/**
6565
* Simple mapper from array to entity (by class)
6666
*
67-
* @param array $data
68-
* @param string $entityClass
69-
* @param object|null $populated
70-
* @param string[] $groups
71-
* @param string[] $entityClassMap
67+
* @param array<mixed> $data
68+
* @param string $entityClass
69+
* @param object|null $populated
70+
* @param string[] $groups
71+
* @param string[] $entityClassMap
7272
*
7373
* @return object
7474
*

src/Component/ApiBusWrapper.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ class ApiBusWrapper
1515
{
1616
use DoctrineTransformerTrait;
1717

18-
public const TYPE_NULL = 'null';
19-
public const TYPE_BOOL = 'bool';
20-
public const TYPE_INT = 'int';
21-
public const TYPE_DOUBLE = 'double';
22-
public const TYPE_STRING = 'string';
23-
public const TYPE_ARRAY = 'array';
18+
public const string TYPE_NULL = 'null';
19+
public const string TYPE_BOOL = 'bool';
20+
public const string TYPE_INT = 'int';
21+
public const string TYPE_DOUBLE = 'double';
22+
public const string TYPE_STRING = 'string';
23+
public const string TYPE_ARRAY = 'array';
2424

2525

2626
public function __construct(private readonly MessageBusInterface $bus)
@@ -46,7 +46,7 @@ public function dispatch(object $message, array $stamps = []): Envelope
4646
* Simplifies the handle method to get the result from last handler
4747
*
4848
* @param Envelope $envelope The envelope after sending the message via MessageBus
49-
* @param array $expectedTypes An array of types defining the expected return types from a handler
49+
* @param string[] $expectedTypes An array of types defining the expected return types from a handler
5050
* @param bool $allowProxy Also allow Doctrine Proxy classes like "Proxies\__CG__\App\Entity\..."
5151
*
5252
* @return mixed|null Returns values with following priority
@@ -88,7 +88,7 @@ public function checkMessageResult(Envelope $envelope, array $expectedTypes = []
8888
* Simplifies the handle method to get results from all handlers
8989
*
9090
* @param Envelope $envelope The envelope after sending the message via MessageBus
91-
* @param array $expectedTypes An array of types defining the expected return types from a handler
91+
* @param string[] $expectedTypes An array of types defining the expected return types from a handler
9292
* @param bool $allowProxy Also allow Doctrine Proxy classes like "Proxies\__CG__\App\Entity\..."
9393
*
9494
* @return HandledStamp[]|false Returns values with following priority
@@ -129,7 +129,7 @@ public function checkAllMessageResults(Envelope $envelope, array $expectedTypes
129129
* Simplifies the handle method to get only expected results from all handlers
130130
*
131131
* @param Envelope $envelope The envelope after sending the message via MessageBus
132-
* @param array $expectedTypes An array of types defining the expected return types from a handler
132+
* @param string[] $expectedTypes An array of types defining the expected return types from a handler
133133
* @param bool $allowProxy Also allow Doctrine Proxy classes like "Proxies\__CG__\App\Entity\..."
134134
*
135135
* @return HandledStamp[]
@@ -155,9 +155,9 @@ public function filterAllMessageResults(Envelope $envelope, array $expectedTypes
155155
/**
156156
* Compare the result on a handle with the expected type
157157
*
158-
* @param mixed $result
159-
* @param array $expectedTypes
160-
* @param bool $allowProxy
158+
* @param mixed $result
159+
* @param string[] $expectedTypes
160+
* @param bool $allowProxy
161161
*
162162
* @return bool
163163
*
@@ -172,7 +172,7 @@ private function expectedTypesValid(mixed $result, array $expectedTypes, bool $a
172172

173173
// any expected type has to be described as a string, e.g. null == 'null'
174174
foreach ($expectedTypes as $type) {
175-
if (!is_string($type)) {
175+
if (!is_string($type)) { // @phpstan-ignore-line
176176
throw new ApiBusException('Only strings or null are allowed to compare expected types for message handling!');
177177
}
178178
}

src/Component/ApiResponse.php

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,42 @@ class ApiResponse extends JsonResponse
1010
{
1111
use ValidationPreparationTrait;
1212

13-
public const MSGTYPE_SUCCESS = 'success';
14-
public const MSGTYPE_INFO = 'info';
15-
public const MSGTYPE_WARNING = 'warning';
16-
public const MSGTYPE_ERROR = 'error';
17-
public const EMPTY_MESSAGES = [
13+
public const string MSGTYPE_SUCCESS = 'success';
14+
public const string MSGTYPE_INFO = 'info';
15+
public const string MSGTYPE_WARNING = 'warning';
16+
public const string MSGTYPE_ERROR = 'error';
17+
public const array EMPTY_MESSAGES = [
1818
self::MSGTYPE_SUCCESS => [],
1919
self::MSGTYPE_INFO => [],
2020
self::MSGTYPE_WARNING => [],
2121
self::MSGTYPE_ERROR => [],
2222
];
23-
public const VALID_ROOT = 'root';
23+
public const string VALID_ROOT = 'root';
2424

25+
/** @var array<mixed> */
2526
private array $apiData = [];
27+
28+
/**
29+
* @var array{
30+
* 'success'?: string[],
31+
* 'info'?: string[],
32+
* 'warning'?: string[],
33+
* 'error'?: string[],
34+
* }
35+
*/
2636
private array $apiMessages = self::EMPTY_MESSAGES;
37+
38+
/** @var array<string, string[]> */
2739
private array $validation = [];
40+
2841
private Throwable|null $throwable = null;
2942

3043

44+
/**
45+
* @param mixed $data
46+
* @param int $status
47+
* @param array<string, string|string[]|null> $headers
48+
*/
3149
public function __construct(mixed $data = [], int $status = 200, array $headers = [])
3250
{
3351
parent::__construct('', $status, $headers, true);
@@ -51,17 +69,18 @@ public function __construct(mixed $data = [], int $status = 200, array $headers
5169
/**
5270
* Factory method for chainability
5371
*
54-
* @param array $data
55-
* @param int $status
56-
* @param array $headers
72+
* @param mixed $data
73+
* @param int $status
74+
* @param array<string, string|string[]|null> $headers
5775
*
5876
* @return ApiResponse
5977
*/
60-
public static function create(array$data = [], int $status = 200, array $headers = []): self
78+
public static function create(mixed $data = [], int $status = 200, array $headers = []): self
6179
{
6280
return new self($data, $status, $headers);
6381
}
6482

83+
/** @return array<mixed> */
6584
public function getData(): array
6685
{
6786
return $this->apiData;
@@ -75,6 +94,11 @@ public function setData(mixed $data = []): static
7594
return $this;
7695
}
7796

97+
/**
98+
* @param string|null $type
99+
*
100+
* @return array<string, string[]>|string[]
101+
*/
78102
public function getMessages(string|null $type = null): array
79103
{
80104
if (null !== $type && array_key_exists($type, $this->apiMessages)) {
@@ -94,6 +118,12 @@ public function addMessage(string $type, string $message): self
94118
return $this;
95119
}
96120

121+
/**
122+
* @param string $type
123+
* @param string[] $messages
124+
*
125+
* @return $this
126+
*/
97127
public function mergeMessages(string $type, array $messages): self
98128
{
99129
if (array_key_exists($type, $this->apiMessages)) {
@@ -130,8 +160,15 @@ public function addValidationIssue(string $component, string $message): self
130160
return $this;
131161
}
132162

163+
/**
164+
* @param string $component
165+
* @param string[] $messages
166+
*
167+
* @return $this
168+
*/
133169
public function mergeValidationIssues(string $component, array $messages): self
134170
{
171+
// @phpstan-ignore-next-line
135172
$messages = array_filter($messages, fn ($message): bool => is_string($message) && !empty($message));
136173

137174
if (empty($messages)) {

src/Component/EntityDenormalizerTrait.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,22 @@
1010
use function is_string;
1111
use function sprintf;
1212

13+
/** @template T of object */
1314
trait EntityDenormalizerTrait
1415
{
1516
private string $defaultHydratingMethod = 'find';
17+
18+
/** @var array<string, ObjectRepository<T>> */
1619
private array $repositories;
20+
21+
/** @var array<string, string> */
1722
private array $normalizedClasses = [];
1823

1924

2025
/**
21-
* @param string $type
26+
* @param class-string $type
2227
*
23-
* @return ObjectRepository
28+
* @return ObjectRepository<T>
2429
*/
2530
private function getRepository(string $type): ObjectRepository
2631
{
@@ -33,7 +38,7 @@ private function getRepository(string $type): ObjectRepository
3338
}
3439

3540
try {
36-
return $this->repositories[$type] = $manager->getRepository($type);
41+
return $this->repositories[$type] = $manager->getRepository($type); // @phpstan-ignore-line
3742
} catch (Exception $e) {
3843
throw new UnexpectedValueException(sprintf('Repository for class "%s" not found!', $type), 0, $e);
3944
}
@@ -43,7 +48,7 @@ private function getRepository(string $type): ObjectRepository
4348
* In case we get an array like [EntityOne::class, EntityTwo:class => 'getOne'],
4449
* keys and values are mixed and need to be normalized
4550
*
46-
* @param array $classMap
51+
* @param array<string|int, string> $classMap
4752
*/
4853
private function normalizeClassMap(array $classMap): void
4954
{
@@ -63,7 +68,7 @@ private function normalizeClassMap(array $classMap): void
6368
throw new UnexpectedValueException(sprintf('Got "%s" which is not a valid class name!', $class));
6469
}
6570

66-
if (!is_string($method)) {
71+
if (!is_string($method)) { // @phpstan-ignore-line
6772
throw new UnexpectedValueException(sprintf('Got hydrating method "%s" which is not valid!', $method));
6873
}
6974

0 commit comments

Comments
 (0)