Skip to content

Commit e41b82d

Browse files
committed
chore: pass PHPStan strict level
1 parent 7421699 commit e41b82d

10 files changed

+42
-31
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"doctrine/common": "^2.0|^3.1",
3939
"doctrine/doctrine-bundle": "^2.0",
4040
"phpstan/phpstan": "^1.12",
41+
"phpstan/phpstan-strict-rules": "^1.6",
4142
"phpunit/phpunit": "^9.6.18",
4243
"symfony/error-handler": "^6.4|^7.0",
4344
"symfony/framework-bundle": "^5.4|^6.4|^7.0",

phpstan.neon.dist

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
parameters:
22
level: 9
33
paths:
4-
- src
4+
- src
5+
includes:
6+
- vendor/phpstan/phpstan-strict-rules/rules.neon

src/DependencyInjection/WebfactoryPolyglotExtension.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@
1616

1717
final class WebfactoryPolyglotExtension extends Extension
1818
{
19-
/**
20-
* @param list<array{defaultLocale: string}> $configs
21-
*/
2219
public function load(array $configs, ContainerBuilder $container): void
2320
{
2421
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
2522
$loader->load('services.xml');
2623

2724
$m = ['defaultLocale' => 'de_DE'];
25+
26+
/**
27+
* @var array{defaultLocale: string} $c
28+
*/
2829
foreach ($configs as $c) {
2930
$m = array_merge($m, $c);
3031
}

src/Doctrine/PersistentTranslatable.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function eject(): void
120120

121121
$type = $this->translatedProperty->getType();
122122
if ($type instanceof ReflectionNamedType && TranslatableInterface::class === $type->getName() && \is_string($value)) {
123-
if (!$this->valueForEjection || $this->valueForEjection->getPrimaryValue() !== $value) {
123+
if ($this->valueForEjection === null || $this->valueForEjection->getPrimaryValue() !== $value) {
124124
$this->valueForEjection = new UninitializedPersistentTranslatable($value);
125125
}
126126
$value = $this->valueForEjection;
@@ -167,12 +167,13 @@ private function createTranslationEntity(string $locale): object
167167

168168
public function setTranslation(mixed $value, ?string $locale = null): void
169169
{
170-
$locale = $locale ?: $this->getDefaultLocale();
170+
$locale ??= $this->getDefaultLocale();
171+
171172
if ($locale === $this->primaryLocale) {
172173
$this->setPrimaryValue($value);
173174
} else {
174175
$entity = $this->getTranslationEntity($locale);
175-
if (!$entity) {
176+
if ($entity === null) {
176177
$entity = $this->createTranslationEntity($locale);
177178
}
178179
$this->translationProperty->setValue($entity, $value);
@@ -184,13 +185,15 @@ public function setTranslation(mixed $value, ?string $locale = null): void
184185
*/
185186
public function translate(?string $locale = null): mixed
186187
{
187-
$locale = $locale ?: $this->getDefaultLocale();
188+
$locale ??= $this->getDefaultLocale();
189+
188190
try {
189191
if ($locale === $this->primaryLocale) {
190192
return $this->primaryValue;
191193
}
192194

193-
if ($entity = $this->getTranslationEntity($locale)) {
195+
$entity = $this->getTranslationEntity($locale);
196+
if ($entity !== null) {
194197
$translated = $this->translationProperty->getValue($entity);
195198
if (null !== $translated) {
196199
return $translated;
@@ -212,12 +215,12 @@ public function translate(?string $locale = null): mixed
212215
public function isTranslatedInto(string $locale): bool
213216
{
214217
if ($locale === $this->primaryLocale) {
215-
return !empty($this->primaryValue);
218+
return $this->primaryValue !== '' && $this->primaryValue !== null;
216219
}
217220

218221
$entity = $this->getTranslationEntity($locale);
219222

220-
return $entity && null !== $this->translationProperty->getValue($entity);
223+
return $entity !== null && $this->translationProperty->getValue($entity) !== null;
221224
}
222225

223226
public function __toString(): string
@@ -279,7 +282,7 @@ private function stringifyException(Throwable $e): string
279282
{
280283
$exceptionAsString = '';
281284
while (null !== $e) {
282-
if (!empty($exceptionAsString)) {
285+
if ($exceptionAsString !== '') {
283286
$exceptionAsString .= \PHP_EOL.'Previous exception: '.\PHP_EOL;
284287
}
285288
$exceptionAsString .= \sprintf(

src/Doctrine/PolyglotListener.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ private function getTranslationMetadatas(object $entity, EntityManagerInterface
126126
$classMetadata = $em->getClassMetadata($class);
127127

128128
foreach (array_merge([$classMetadata->name], $classMetadata->parentClasses) as $className) {
129-
if ($tm = $this->loadTranslationMetadataForClass($className, $em)) {
129+
$tm = $this->loadTranslationMetadataForClass($className, $em);
130+
if ($tm !== null) {
130131
$this->translatableClassMetadatasByClass[$class][] = $tm;
131132
}
132133
}
@@ -149,7 +150,7 @@ private function loadTranslationMetadataForClass(string $className, EntityManage
149150
$cache = $em->getConfiguration()->getMetadataCache();
150151
$cacheKey = $this->getCacheKey($className);
151152

152-
if ($cache?->hasItem($cacheKey)) {
153+
if ($cache !== null && $cache->hasItem($cacheKey)) {
153154
$item = $cache->getItem($cacheKey);
154155
/** @var SerializedTranslatableClassMetadata|null $data */
155156
$data = $item->get();
@@ -175,7 +176,7 @@ private function loadTranslationMetadataForClass(string $className, EntityManage
175176
}
176177

177178
// Save if cache driver available
178-
if ($cache) {
179+
if ($cache !== null) {
179180
$item = $cache->getItem($cacheKey);
180181
$item->set($meta?->sleep());
181182
$cache->save($item);

src/Doctrine/TranslatableClassMetadata.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ private function assertAttributesAreComplete(string $class): void
218218
*/
219219
private function findTranslatedProperties(ClassMetadata $cm, ClassMetadataFactory $classMetadataFactory): void
220220
{
221-
if (!$this->translationClass) {
221+
if ($this->translationClass === null) {
222222
return;
223223
}
224224

@@ -234,21 +234,21 @@ private function findTranslatedProperties(ClassMetadata $cm, ClassMetadataFactor
234234
already contains that declaration, we need not include it.
235235
*/
236236
$declaringClass = $reflectionProperty->getDeclaringClass()->name;
237-
if ($declaringClass !== $cm->name && $cm->parentClasses && is_a($cm->parentClasses[0], $declaringClass, true)) {
237+
if ($declaringClass !== $cm->name && $cm->parentClasses !== [] && is_a($cm->parentClasses[0], $declaringClass, true)) {
238238
continue;
239239
}
240240

241241
$attributes = $reflectionProperty->getAttributes(Attribute\Translatable::class);
242242

243-
if (!$attributes) {
243+
if ($attributes === []) {
244244
continue;
245245
}
246246

247247
$attribute = $attributes[0]->newInstance();
248248
$this->translatedProperties[$propertyName] = $reflectionService->getAccessibleProperty($cm->name, $propertyName) ??
249249
throw new ShouldNotHappen("Cannot get reflection for {$cm->name}::{$propertyName}.");
250250

251-
$translationFieldname = $attribute->getTranslationFieldname() ?: $propertyName;
251+
$translationFieldname = $attribute->getTranslationFieldname() ?? $propertyName;
252252

253253
$this->translationFieldMapping[$propertyName] = $reflectionService->getAccessibleProperty($translationClassMetadata->name, $translationFieldname) ??
254254
throw new ShouldNotHappen("Cannot get reflection for {$translationClassMetadata->name}::{$translationFieldname}.");
@@ -268,7 +268,7 @@ private function findTranslationsCollection(ClassMetadata $cm, ClassMetadataFact
268268

269269
$reflectionProperty = $cm->getReflectionProperty($fieldName);
270270

271-
if ($reflectionProperty?->getAttributes(Attribute\TranslationCollection::class) ?? false) {
271+
if ($reflectionProperty !== null && $reflectionProperty->getAttributes(Attribute\TranslationCollection::class) !== []) {
272272
if (!$mapping instanceof InverseSideMapping) {
273273
return;
274274
}
@@ -309,7 +309,7 @@ private function parseTranslationsEntity(ClassMetadata $cm): void
309309
foreach ($cm->fieldMappings as $fieldName => $mapping) {
310310
$reflectionProperty = $cm->getReflectionProperty($fieldName);
311311

312-
if ($reflectionProperty?->getAttributes(Attribute\Locale::class) ?? false) {
312+
if ($reflectionProperty !== null && $reflectionProperty->getAttributes(Attribute\Locale::class) !== []) {
313313
$this->translationLocaleProperty = $reflectionProperty;
314314

315315
return;

src/Doctrine/TranslatableStringType.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@ class TranslatableStringType extends Type
1616
{
1717
public const NAME = 'translatable_string';
1818

19-
/**
20-
* @param array{options: array{use_text_column?: string}} $column
21-
*/
2219
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
2320
{
24-
if ($column['options']['use_text_column'] ?? false) {
21+
if (isset($column['options']) && is_array($column['options']) && ($column['options']['use_text_column'] ?? false)) {
2522
return $platform->getClobTypeDeclarationSQL($column);
2623
}
2724

25+
// @phpstan-ignore function.alreadyNarrowedType
2826
if (method_exists($platform, 'getStringTypeDeclarationSQL')) {
2927
return $platform->getStringTypeDeclarationSQL($column);
3028
} else {

src/Translatable.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ final class Translatable implements TranslatableInterface
4545
/**
4646
* Maps locales to translations.
4747
*
48-
* @var array<string, mixed>
48+
* @var array<string, T>
4949
*/
5050
private array $translations = [];
5151

@@ -73,11 +73,11 @@ public function setDefaultLocale(string $locale): void
7373
}
7474

7575
/**
76-
* @return T
76+
* @return T|null
7777
*/
7878
public function translate(?string $locale = null): mixed
7979
{
80-
$locale = $locale ?: $this->getDefaultLocale();
80+
$locale ??= $this->getDefaultLocale();
8181

8282
return $this->translations[$locale] ?? null;
8383
}
@@ -87,14 +87,14 @@ public function translate(?string $locale = null): mixed
8787
*/
8888
public function setTranslation(mixed $value, ?string $locale = null): void
8989
{
90-
$locale = $locale ?: $this->getDefaultLocale();
90+
$locale ??= $this->getDefaultLocale();
9191

9292
$this->translations[$locale] = $value;
9393
}
9494

9595
public function isTranslatedInto(string $locale): bool
9696
{
97-
return isset($this->translations[$locale]) && !empty($this->translations[$locale]);
97+
return isset($this->translations[$locale]) && (string) $this->translations[$locale] !== '';
9898
}
9999

100100
public function __toString(): string

src/TranslatableChain.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public function translate(?string $locale = null): mixed
6161
$c = $this->comparator;
6262
foreach ($this->translatables as $translation) {
6363
$value = $translation->translate($locale);
64+
65+
if (null === $value) {
66+
continue;
67+
}
68+
6469
if ($c($value)) {
6570
return $value;
6671
}

src/TranslatableInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ interface TranslatableInterface
2121
*
2222
* @param string|null $locale The target locale or null for the current locale.
2323
*
24-
* @return TTranslatedValue The translation or null if not available.
24+
* @return TTranslatedValue|null The translation or null if not available.
2525
*/
2626
public function translate(?string $locale = null): mixed;
2727

0 commit comments

Comments
 (0)