Skip to content

Commit 43c4dd2

Browse files
Solve easy PHPStan message, generate baseline for others
1 parent aed8a25 commit 43c4dd2

17 files changed

+415
-29
lines changed

phpstan-baseline.php

+311
Large diffs are not rendered by default.

phpstan.dist.neon

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
includes:
2+
- phpstan-baseline.php
3+
14
parameters:
25
level: 8
36
tmpDir: var/cache/phpstan

src/Database/Query/DiscriminatorTypeFunction.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Doctrine\ORM\Query\QueryException;
99
use Doctrine\ORM\Query\SqlWalker;
1010
use Doctrine\ORM\Query\TokenType;
11+
use Drenso\Shared\Exception\NullGuard\MustNotBeNullException;
1112

1213
/**
1314
* Provides a way to access an entity's discriminator field in DQL
@@ -45,8 +46,8 @@ class DiscriminatorTypeFunction extends FunctionNode
4546
public function getSql(SqlWalker $sqlWalker): string
4647
{
4748
$qComp = $sqlWalker->getQueryComponent($this->dqlAlias);
48-
/** @var ClassMetadataInfo $class */
49-
$class = $qComp['metadata'];
49+
/** @var ClassMetadataInfo<object> $class */
50+
$class = $qComp['metadata'] ?? throw new MustNotBeNullException();
5051
$tableAlias = $sqlWalker->getSQLTableAlias($class->getTableName(), $this->dqlAlias);
5152

5253
if (!isset($class->discriminatorColumn['name'])) {

src/Database/SoftDeletableFilterController.php

+2
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ public function enableSoftDeleteFilter(): void
2929
$this->entityManager->getFilters()->enable(self::SOFT_DELETE_FILTER);
3030
}
3131

32+
/** @param class-string $class */
3233
public function disableSoftDeleteFilterForEntity(string $class): void
3334
{
3435
$this->getSoftDeleteFilter()->disableForEntity($class);
3536
}
3637

38+
/** @param class-string $class */
3739
public function enableSoftDeleteFilterForEntity(string $class): void
3840
{
3941
$this->getSoftDeleteFilter()->enableForEntity($class);

src/Exception/EntityValidationFailedException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function __construct(private ?ConstraintViolationListInterface $violation
1919

2020
parent::__construct(join("\n", $messages));
2121
} else {
22-
parent::__construct($message);
22+
parent::__construct($message ?? '');
2323
}
2424
}
2525

src/Exception/ObjectTypeNotFoundException.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66

77
class ObjectTypeNotFoundException extends Exception
88
{
9-
/** ObjectTypeNotFoundException constructor. */
10-
public function __construct($type, ?array $types = null)
9+
/**
10+
* @param string $type
11+
* @param string[] $types
12+
*/
13+
public function __construct(mixed $type, ?array $types = null)
1114
{
1215
parent::__construct(sprintf('The object type "%s" is not valid.', $type) . ($types ? sprintf(' Valid options are %s.', implode(', ', $types)) : ''));
1316
}

src/FeatureFlags/FeatureFlags.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Drenso\Shared\FeatureFlags;
44

5+
use Drenso\Shared\Exception\NullGuard\MustNotBeNullException;
56
use Symfony\Component\DependencyInjection\Exception\EnvNotFoundException;
67
use Symfony\Contracts\Cache\CacheInterface;
78

@@ -10,7 +11,7 @@ class FeatureFlags implements FeatureFlagsInterface
1011
private const CACHE_KEY_MTIME = 'drenso.feature_flags.mtime';
1112
private const CACHE_KEY_CONFIG = 'drenso.feature_flags.configuration';
1213

13-
/** @var array<string, bool>|null */
14+
/** @var array<string, mixed>|null */
1415
private ?array $resolvedConfiguration = null;
1516

1617
public function __construct(
@@ -45,6 +46,7 @@ public function getConfiguredFlags(): array
4546
return array_keys($this->resolvedConfiguration ?? []);
4647
}
4748

49+
/** @phpstan-assert array<string, mixed> $this->resolvedConfiguration */
4850
private function resolve(): void
4951
{
5052
if (null !== $this->resolvedConfiguration) {
@@ -75,6 +77,9 @@ private function resolve(): void
7577
filemtime($this->configuration),
7678
$overrideAvailable ? filemtime($this->configurationOverride) : 0,
7779
);
80+
if ($currentMTime === false) {
81+
$currentMTime = 0;
82+
}
7883

7984
$cachedMTime = $this->appCache->get(self::CACHE_KEY_MTIME, static fn (): int => 0);
8085
if ($cachedMTime < $currentMTime) {
@@ -89,6 +94,12 @@ private function resolve(): void
8994
$this->appCache->get(self::CACHE_KEY_MTIME, static fn (): int => $currentMTime);
9095
}
9196

97+
/**
98+
* @return array<string, bool>
99+
*
100+
* @noinspection PhpUnhandledExceptionInspection
101+
* @noinspection PhpDocMissingThrowsInspection
102+
*/
92103
private function parseConfiguration(bool $overrideAvailable): array
93104
{
94105
$configuration = file_get_contents($this->configuration);
@@ -110,6 +121,7 @@ private function filterComments(string $data): string
110121
}
111122

112123
// Regex from https://stackoverflow.com/a/43439966
113-
return preg_replace('~ (" (?:\\\\. | [^"])*+ ") | // \V*+ | /\* .*? \*/ ~xs', '$1', $data);
124+
return preg_replace('~ (" (?:\\\\. | [^"])*+ ") | // \V*+ | /\* .*? \*/ ~xs', '$1', $data)
125+
?? throw new MustNotBeNullException();
114126
}
115127
}

src/Helper/ArrayHelper.php

+47-10
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@
33
namespace Drenso\Shared\Helper;
44

55
use Doctrine\Common\Collections\ReadableCollection;
6+
use Drenso\Shared\Exception\NullGuard\IdRequiredException;
67
use Drenso\Shared\Interfaces\IdInterface;
78
use InvalidArgumentException;
89

910
class ArrayHelper
1011
{
11-
/** Verify that all array elements are of the supplied type. */
12+
/**
13+
* Verify that all array elements are of the supplied type.
14+
*
15+
* @template T
16+
*
17+
* @param 'int'|'integer'|'string'|'float'|'bool'|'boolean'|'array'|class-string<T> $type
18+
*
19+
* @phpstan-assert T[] $variables
20+
*/
1221
public static function assertType(array $variables, string $type): void
1322
{
1423
match ($type) {
@@ -21,7 +30,11 @@ public static function assertType(array $variables, string $type): void
2130
};
2231
}
2332

24-
/** Verify that all array elements are integers. */
33+
/**
34+
* Verify that all array elements are integers.
35+
*
36+
* @phpstan-assert int[] $variables
37+
*/
2538
public static function assertInt(array $variables): void
2639
{
2740
foreach ($variables as $variable) {
@@ -32,7 +45,11 @@ public static function assertInt(array $variables): void
3245
}
3346
}
3447

35-
/** Verify that all array elements are strings. */
48+
/**
49+
* Verify that all array elements are strings.
50+
*
51+
* @phpstan-assert string[] $variables
52+
*/
3653
public static function assertString(array $variables): void
3754
{
3855
foreach ($variables as $variable) {
@@ -43,7 +60,11 @@ public static function assertString(array $variables): void
4360
}
4461
}
4562

46-
/** Verify that all array elements are floats. */
63+
/**
64+
* Verify that all array elements are floats.
65+
*
66+
* @phpstan-assert float[] $variables
67+
*/
4768
public static function assertFloat(array $variables): void
4869
{
4970
foreach ($variables as $variable) {
@@ -54,7 +75,11 @@ public static function assertFloat(array $variables): void
5475
}
5576
}
5677

57-
/** Verify that all array elements are booleans. */
78+
/**
79+
* Verify that all array elements are booleans.
80+
*
81+
* @phpstan-assert bool[] $variables
82+
*/
5883
public static function assertBool(array $variables): void
5984
{
6085
foreach ($variables as $variable) {
@@ -65,7 +90,11 @@ public static function assertBool(array $variables): void
6590
}
6691
}
6792

68-
/** Verify that all array elements are arrays. */
93+
/**
94+
* Verify that all array elements are arrays.
95+
*
96+
* @phpstan-assert array<array> $variables
97+
*/
6998
public static function assertArray(array $variables): void
7099
{
71100
foreach ($variables as $variable) {
@@ -76,7 +105,15 @@ public static function assertArray(array $variables): void
76105
}
77106
}
78107

79-
/** Verify that all array elements are objects of the supplied class. */
108+
/**
109+
* Verify that all array elements are objects of the supplied class.
110+
*
111+
* @template T
112+
*
113+
* @param class-string<T> $class
114+
*
115+
* @phpstan-assert T[] $objects
116+
*/
80117
public static function assertClass(array $objects, string $class): void
81118
{
82119
foreach ($objects as $object) {
@@ -101,7 +138,7 @@ public static function mapById(array $objects): array
101138
{
102139
$result = [];
103140
foreach ($objects as $object) {
104-
$result[$object->getId()] = $object;
141+
$result[$object->getId() ?? throw new IdRequiredException()] = $object;
105142
}
106143

107144
return $result;
@@ -117,7 +154,7 @@ public static function mapById(array $objects): array
117154
public static function mapToId(array $objects): array
118155
{
119156
return array_values(array_map(
120-
fn ($object): ?int => $object->getId(),
157+
fn ($object): int => $object->getId() ?? throw new IdRequiredException(),
121158
$objects
122159
));
123160
}
@@ -153,7 +190,7 @@ public static function filterEmptyValuesFromStringArray(array $data): array
153190
*
154191
* @template T
155192
*
156-
* @param array<T>|ReadableCollection<T> $arrayOrCollection
193+
* @param array<T>|ReadableCollection<int, T> $arrayOrCollection
157194
*
158195
* @return array<T>
159196
*/

src/Helper/SpreadsheetHelper.php

+17-6
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function () use ($spreadsheet): void {
9696
/**
9797
* Create a ZIP response container multiple Excel spreadsheets.
9898
*
99-
* @param array $spreadSheets array with ['sheet' => Spreadsheet object, 'filename' => string filename]
99+
* @param array<array{sheet: Spreadsheet, filename: string}> $spreadSheets array with ['sheet' => Spreadsheet object, 'filename' => string filename]
100100
*/
101101
public function createZippedExcelResponse(array $spreadSheets, string $zipName): StreamedResponse
102102
{
@@ -165,11 +165,13 @@ public function setCellBooleanValue(
165165
{
166166
if ($value !== null) {
167167
$value = $value ? 'excel.boolean.yes' : 'excel.boolean.no';
168+
169+
return $this->setCellTranslatedValue(
170+
$sheet, $column, $row, $value, $bold, 'drenso_shared',
171+
);
168172
}
169173

170-
return $this->setCellTranslatedValue(
171-
$sheet, $column, $row, $value, $bold, 'drenso_shared',
172-
);
174+
return $this->setCellValue($sheet, $column, $row, $value, $bold);
173175
}
174176

175177
public function setCellTranslatedValue(
@@ -195,6 +197,7 @@ public function setCellValue(Worksheet $sheet, int $column, int $row, mixed $val
195197
return $coordinate;
196198
}
197199

200+
/** @param string[] $lines */
198201
public function setCellMultilineValue(
199202
Worksheet $sheet,
200203
int $column,
@@ -343,11 +346,19 @@ public static function contentDisposition(Response $response, string $filename):
343346

344347
public static function sanitizeSheetName(string $sheetName): string
345348
{
346-
return str_replace(Worksheet::getInvalidCharacters(), '_', iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $sheetName));
349+
if (false === $iconv = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $sheetName)) {
350+
$iconv = $sheetName;
351+
}
352+
353+
return str_replace(Worksheet::getInvalidCharacters(), '_', $iconv);
347354
}
348355

349356
public static function sanitizeFilename(string $filename): string
350357
{
351-
return mb_strtolower((string)preg_replace('/[^A-Z\d.]/ui', '_', iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $filename)));
358+
if (false === $iconv = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $filename)) {
359+
$iconv = $filename;
360+
}
361+
362+
return mb_strtolower((string)preg_replace('/[^A-Z\d.]/ui', '_', $iconv));
352363
}
353364
}

src/Http/AcceptedResponse.php

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
class AcceptedResponse extends Response
1111
{
12+
/** @param array<string, list<string|null>> $headers */
1213
public function __construct(array $headers = [])
1314
{
1415
parent::__construct(null, Response::HTTP_ACCEPTED, $headers);

src/IdMap/IdMap.php

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
/**
1414
* @template T
1515
*
16+
* @template-implements ArrayAccess<int, T>
1617
* @template-implements IteratorAggregate<int, T>
1718
*/
1819
class IdMap implements ArrayAccess, Countable, IteratorAggregate, Stringable

src/Sentry/SentryTunnelController.php

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class SentryTunnelController extends AbstractController
1919
{
2020
private const CACHE_KEY = 'drenso.sentry_tunnel.available';
2121

22+
/** @param string[] $allowedDsn */
2223
public function __construct(
2324
private readonly HttpClientInterface $httpClient,
2425
private readonly ?CacheInterface $appCache,

src/Serializer/AbstractObjectSerializer.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ protected function addProperty(
112112
?string $prop = null,
113113
bool $insertUnderscore = true): void
114114
{
115-
$metadata = $event->getContext()->getMetadataFactory()->getMetadataForClass($objectClass)->propertyMetadata[$objectProperty];
115+
$metadata = $event->getContext()
116+
->getMetadataFactory()
117+
->getMetadataForClass($objectClass)
118+
?->propertyMetadata[$objectProperty] ?? null;
116119
if (!$metadata instanceof PropertyMetadata) {
117120
throw new RuntimeException('Invalid property metadata!');
118121
}
@@ -127,7 +130,7 @@ protected function addProperty(
127130
abstract protected function doSerialize(
128131
SerializationVisitorInterface $visitor,
129132
array $groups,
130-
$object,
133+
mixed $object,
131134
ObjectEvent $event): void;
132135

133136
private function propertyName(string $prop, bool $insertUnderscore): string

src/Serializer/Handlers/DecimalHandler.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function serializeJson(
4343

4444
public function deserializeJson(
4545
DeserializationVisitorInterface $visitor,
46-
$decimalAsString,
46+
mixed $decimalAsString,
4747
array $type,
4848
Context $context): Decimal
4949
{

src/Serializer/Handlers/EnumHandler.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function serialize(
1414
SerializationVisitorInterface $visitor,
1515
BackedEnum $enum,
1616
array $type,
17-
SerializationContext $context)
17+
SerializationContext $context): string
1818
{
1919
$value = $enum->value;
2020
if (is_int($value)) {

src/Serializer/Handlers/IdMapHandler.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function serializeJson(
5050

5151
public function deserializeJson(
5252
DeserializationVisitorInterface $visitor,
53-
$data,
53+
mixed $data,
5454
array $type,
5555
DeserializationContext $context): IdMap
5656
{

src/Twig/JmsSerializerExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function getFilters(): array
2222
];
2323
}
2424

25-
public function encode($data, ?array $serializationGroups = null): string
25+
public function encode(mixed $data, ?array $serializationGroups = null): string
2626
{
2727
$context = $this->contextFactory->createSerializationContext();
2828
$context->setGroups($serializationGroups ?? ['Default']);

0 commit comments

Comments
 (0)