Skip to content

Commit 9f2eaf7

Browse files
committed
chore: make code more stable
1 parent 1d5b064 commit 9f2eaf7

File tree

11 files changed

+73
-55
lines changed

11 files changed

+73
-55
lines changed

src/Controller/Api/TestController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private function getProductFolderId(Context $context): string
8383
->searchIds($criteria, $context)
8484
->getIds();
8585

86-
if (!empty($ids[0]) && \is_string($ids[0])) {
86+
if (isset($ids[0]) && \is_string($ids[0])) {
8787
return $ids[0];
8888
}
8989

@@ -100,7 +100,7 @@ private function getSampleMedia(string $testFile): MediaEntity
100100
$pathInfo = pathinfo($testFile);
101101

102102
$existingMedia = $this->getMediaById($pathInfo['filename'], $context);
103-
if ($existingMedia) {
103+
if ($existingMedia !== null) {
104104
return $existingMedia;
105105
}
106106

@@ -130,7 +130,7 @@ private function getSampleMedia(string $testFile): MediaEntity
130130
);
131131

132132
$existingMedia = $this->getMediaById($pathInfo['filename'], $context);
133-
if ($existingMedia) {
133+
if ($existingMedia !== null) {
134134
return $existingMedia;
135135
}
136136

src/Core/Media/MediaUrlGenerator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ public function generate(array $paths): array
6262
private function canProcess(string $path): bool
6363
{
6464
$fileExtension = \pathinfo($path, \PATHINFO_EXTENSION);
65-
\assert(\is_string($fileExtension));
6665

6766
return $this->canProcessFileExtension($fileExtension);
6867
}
@@ -71,7 +70,7 @@ private function canProcessFileExtension(string $fileExtension): bool
7170
{
7271
$extensionsAllowList = $this->getExtensionsAllowList();
7372

74-
if (empty($extensionsAllowList)) {
73+
if ($extensionsAllowList === []) {
7574
return false;
7675
}
7776

@@ -101,7 +100,8 @@ private function getExtensionsAllowList(): array
101100
\explode(
102101
',',
103102
(string) \preg_replace('/\s+/', '', \strtolower($extensionsAllowListConfig))
104-
)
103+
),
104+
static fn($value) => $value !== ''
105105
)
106106
);
107107
}
@@ -111,7 +111,7 @@ private function getExtensionsAllowList(): array
111111

112112
private function getWidth(string $maxWidth, UrlParams $value): string
113113
{
114-
if ($value instanceof ExtendedUrlParams && !empty($value->width)) {
114+
if ($value instanceof ExtendedUrlParams && $value->width !== null && $value->width !== 0) {
115115
return (string) $value->width;
116116
}
117117

src/Core/Media/MediaUrlLoader.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function loaded(iterable $entities): void
1818
{
1919
$mapping = $this->map($entities);
2020

21-
if (empty($mapping)) {
21+
if ($mapping !== []) {
2222
return;
2323
}
2424

@@ -65,12 +65,12 @@ private function map(iterable $entities): array
6565
$mapped = [];
6666

6767
foreach ($entities as $entity) {
68-
if (!$entity->has('path') || empty($entity->get('path'))) {
68+
if (!$entity->has('path') || $entity->get('path') === null || $entity->get('path') === '') {
6969
continue;
7070
}
7171

7272
// don't generate private urls
73-
if (!$entity->has('private') || $entity->get('private')) {
73+
if (!$entity->has('private') || $entity->get('private') === true) {
7474
continue;
7575
}
7676

@@ -91,7 +91,7 @@ private function map(iterable $entities): array
9191
continue;
9292
}
9393

94-
if (!$thumbnail->has('path') || empty($thumbnail->get('path'))) {
94+
if (!$thumbnail->has('path') || $thumbnail->get('path') === null || $thumbnail->get('path') === '') {
9595
continue;
9696
}
9797

src/DependencyInjection/GeneratorCompilerPass.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function process(ContainerBuilder $container): void
3434
{
3535
$fileContents = $this->getFileContent();
3636

37-
if (empty($fileContents)) {
37+
if ($fileContents === null || $fileContents === '') {
3838
$this->removeReflectionClass();
3939

4040
return;
@@ -71,7 +71,7 @@ public function process(ContainerBuilder $container): void
7171
$doc = '/**' . \PHP_EOL . 'THIS CLASS HAS BEEN GENERATED AUTOMATICALLY' . \PHP_EOL . '*/';
7272

7373
$existingDocs = $class->getDocComment()?->getText() ?? '';
74-
if (!empty($existingDocs)) {
74+
if ($existingDocs !== '') {
7575
$doc .= \PHP_EOL . $existingDocs;
7676
}
7777

@@ -123,7 +123,7 @@ private function handleThumbnailService(NodeFinder $nodeFinder, array $ast): voi
123123
$updateThumbnailsNode = $this->getClassMethod($nodeFinder, 'updateThumbnails', $ast);
124124

125125
$stmts = $updateThumbnailsNode->getStmts();
126-
if (empty($stmts)) {
126+
if ($stmts === null || $stmts === []) {
127127
throw new \RuntimeException(\sprintf('Method %s in class %s is empty', 'updateThumbnails', $this->class));
128128
}
129129

@@ -174,7 +174,7 @@ private function addUsesOfNamespace(?array &$stmts, string $namespace): void
174174
$filePath = $this->getFileName();
175175
$files = glob(\dirname($filePath) . '/*.php');
176176

177-
if (empty($files)) {
177+
if ($files === false || $files === []) {
178178
return;
179179
}
180180

@@ -188,7 +188,7 @@ private function addUsesOfNamespace(?array &$stmts, string $namespace): void
188188
$uses[] = new UseUse(new Name($class));
189189
}
190190

191-
if (empty($uses)) {
191+
if ($uses === []) {
192192
return;
193193
}
194194

@@ -197,7 +197,13 @@ private function addUsesOfNamespace(?array &$stmts, string $namespace): void
197197

198198
private function getFileContent(): ?string
199199
{
200-
return file_get_contents($this->getFileName()) ?: null;
200+
$content = file_get_contents($this->getFileName());
201+
202+
if (\is_string($content)) {
203+
return $content;
204+
}
205+
206+
return null;
201207
}
202208

203209
private function getFileName(): string
@@ -206,7 +212,7 @@ private function getFileName(): string
206212

207213
$fileName = $reflectionClass->getFileName();
208214

209-
if (empty($fileName)) {
215+
if ($fileName === false || $fileName === '') {
210216
throw new \RuntimeException(\sprintf('Cannot get fileName of class %s', $this->class));
211217
}
212218

@@ -231,7 +237,7 @@ private function getClassName(): string
231237
{
232238
$lastOccur = strrchr($this->class, '\\');
233239

234-
if (empty($lastOccur)) {
240+
if ($lastOccur === false || $lastOccur === '') {
235241
throw new \RuntimeException(\sprintf('Cannot determine className from %s', $this->class));
236242
}
237243

src/EventListener/ThumbnailSizesChangedListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ public function onThumbnailSizeChanged(EntityWrittenEvent $event): void
3636
foreach ($event->getWriteResults() as $writeResult) {
3737
$mediaFolderConfigurationId = $writeResult->getPayload()['mediaFolderConfigurationId'] ?? null;
3838

39-
if (!empty($mediaFolderConfigurationId) && \is_string($mediaFolderConfigurationId)) {
39+
if ($mediaFolderConfigurationId !== '' && \is_string($mediaFolderConfigurationId)) {
4040
$updatedMediaFolderConfigurationIds[] = $mediaFolderConfigurationId;
4141
}
4242
}
4343

4444
$updatedMediaFolderConfigurationIds = \array_unique($updatedMediaFolderConfigurationIds);
4545

46-
if (empty($updatedMediaFolderConfigurationIds)) {
46+
if ($updatedMediaFolderConfigurationIds === []) {
4747
return;
4848
}
4949

src/Migration/Migration1686772873AddActiveConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function update(Connection $connection): void
1616
{
1717
$currentPluginVersion = $this->getPluginVersion($connection);
1818

19-
if (empty($currentPluginVersion)) {
19+
if ($currentPluginVersion === null || $currentPluginVersion === '') {
2020
return;
2121
}
2222

src/Service/ConfigReader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private function isActive(mixed $config): bool
6666
return true;
6767
}
6868

69-
return $this->isTestActive() || !empty($config);
69+
return $this->isTestActive() || ($config !== false && $config !== '0' && $config !== 0);
7070
}
7171

7272
private function isTestActive(): bool

src/Service/ThumbnailUrlTemplate.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ public function __construct(
1313

1414
public function getUrl(string $mediaUrl, string $mediaPath, string $width, ?\DateTimeInterface $mediaUpdatedAt): string
1515
{
16+
$timestamp = $mediaUpdatedAt !== null ? $mediaUpdatedAt->getTimestamp() : '0';
17+
1618
return str_replace(
1719
['{mediaUrl}', '{mediaPath}', '{width}', '{mediaUpdatedAt}'],
18-
[$mediaUrl, $mediaPath, $width, $mediaUpdatedAt?->getTimestamp() ?: '0'],
20+
[$mediaUrl, $mediaPath, $width, $timestamp],
1921
$this->getPattern()
2022
);
2123
}

tests/unit/Core/Media/MediaUrlGeneratorTest.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public function testGenerateWithUrlParam(): void
2121
{
2222
$decoratedMediaUrlGenerator = $this->createMock(AbstractMediaUrlGenerator::class);
2323
$thumbnailUrlTemplate = $this->createMock(ThumbnailUrlTemplateInterface::class);
24-
$thumbnailUrlTemplate->expects(static::once())
24+
$thumbnailUrlTemplate->expects($this->once())
2525
->method('getUrl')
2626
->willReturn('https://localhost/media/123.jpg?width=3000');
2727
$filesystem = $this->createMock(PrefixFilesystem::class);
28-
$filesystem->expects(static::once())
28+
$filesystem->expects($this->once())
2929
->method('publicUrl')
3030
->willReturn('https://localhost');
3131

@@ -37,7 +37,7 @@ public function testGenerateWithUrlParam(): void
3737
'ExtensionsAllowList' => 'jpg',
3838
'ThumbnailPattern' => '{mediaUrl}/{mediaPath}?width={width}',
3939
];
40-
$configReader->expects(static::any())
40+
$configReader->expects($this->any())
4141
->method('getConfig')
4242
->willReturnCallback(function ($key) use ($defaultPluginConfig) {
4343
return $defaultPluginConfig[$key] ?? null;
@@ -68,11 +68,11 @@ public function testGenerateWithExtendedUrlParam(): void
6868
{
6969
$decoratedMediaUrlGenerator = $this->createMock(AbstractMediaUrlGenerator::class);
7070
$thumbnailUrlTemplate = $this->createMock(ThumbnailUrlTemplateInterface::class);
71-
$thumbnailUrlTemplate->expects(static::once())
71+
$thumbnailUrlTemplate->expects($this->once())
7272
->method('getUrl')
7373
->willReturn('https://localhost/media/123.jpg?width=100');
7474
$filesystem = $this->createMock(PrefixFilesystem::class);
75-
$filesystem->expects(static::once())
75+
$filesystem->expects($this->once())
7676
->method('publicUrl')
7777
->willReturn('https://localhost');
7878

@@ -84,7 +84,7 @@ public function testGenerateWithExtendedUrlParam(): void
8484
'ExtensionsAllowList' => 'jpg',
8585
'ThumbnailPattern' => '{mediaUrl}/{mediaPath}?width={width}',
8686
];
87-
$configReader->expects(static::any())
87+
$configReader->expects($this->any())
8888
->method('getConfig')
8989
->willReturnCallback(function ($key) use ($defaultPluginConfig) {
9090
return $defaultPluginConfig[$key] ?? null;
@@ -120,16 +120,16 @@ public function testGenerateWithExtendedUrlParam(): void
120120
public function testGenerateWithInactiveConfig(): void
121121
{
122122
$decoratedMediaUrlGenerator = $this->createMock(AbstractMediaUrlGenerator::class);
123-
$decoratedMediaUrlGenerator->expects(static::once())
123+
$decoratedMediaUrlGenerator->expects($this->once())
124124
->method('generate')
125125
->willReturn(['https://localhost/media/123.jpg']);
126126

127127
$thumbnailUrlTemplate = $this->createMock(ThumbnailUrlTemplateInterface::class);
128-
$thumbnailUrlTemplate->expects(static::never())
128+
$thumbnailUrlTemplate->expects($this->never())
129129
->method('getUrl')
130130
->willReturn('https://localhost/media/123.jpg?width=3000');
131131
$filesystem = $this->createMock(PrefixFilesystem::class);
132-
$filesystem->expects(static::never())
132+
$filesystem->expects($this->never())
133133
->method('publicUrl')
134134
->willReturn('https://localhost');
135135

@@ -141,7 +141,7 @@ public function testGenerateWithInactiveConfig(): void
141141
'ExtensionsAllowList' => 'jpg',
142142
'ThumbnailPattern' => '{mediaUrl}/{mediaPath}?width={width}',
143143
];
144-
$configReader->expects(static::any())
144+
$configReader->expects($this->any())
145145
->method('getConfig')
146146
->willReturnCallback(function ($key) use ($defaultPluginConfig) {
147147
return $defaultPluginConfig[$key] ?? null;
@@ -174,16 +174,16 @@ public function testGenerateWithInactiveConfig(): void
174174
public function testGenerateWithNotAllowedExtensionResultsInOriginal(string $allowList): void
175175
{
176176
$decoratedMediaUrlGenerator = $this->createMock(AbstractMediaUrlGenerator::class);
177-
$decoratedMediaUrlGenerator->expects(static::once())
177+
$decoratedMediaUrlGenerator->expects($this->once())
178178
->method('generate')
179179
->willReturn(['https://localhost/media/123.jpg']);
180180

181181
$thumbnailUrlTemplate = $this->createMock(ThumbnailUrlTemplateInterface::class);
182-
$thumbnailUrlTemplate->expects(static::never())
182+
$thumbnailUrlTemplate->expects($this->never())
183183
->method('getUrl')
184184
->willReturn('https://localhost/media/123.jpg?width=3000');
185185
$filesystem = $this->createMock(PrefixFilesystem::class);
186-
$filesystem->expects(static::once())
186+
$filesystem->expects($this->once())
187187
->method('publicUrl')
188188
->willReturn('https://localhost');
189189

@@ -195,7 +195,7 @@ public function testGenerateWithNotAllowedExtensionResultsInOriginal(string $all
195195
'ExtensionsAllowList' => $allowList,
196196
'ThumbnailPattern' => '{mediaUrl}/{mediaPath}?width={width}',
197197
];
198-
$configReader->expects(static::any())
198+
$configReader->expects($this->any())
199199
->method('getConfig')
200200
->willReturnCallback(function ($key) use ($defaultPluginConfig) {
201201
return $defaultPluginConfig[$key] ?? null;
@@ -226,11 +226,11 @@ public function testMultipleGenerationsUseCachedExtensionAllowList(): void
226226
{
227227
$decoratedMediaUrlGenerator = $this->createMock(AbstractMediaUrlGenerator::class);
228228
$thumbnailUrlTemplate = $this->createMock(ThumbnailUrlTemplateInterface::class);
229-
$thumbnailUrlTemplate->expects(static::exactly(2))
229+
$thumbnailUrlTemplate->expects($this->exactly(2))
230230
->method('getUrl')
231231
->willReturn('https://localhost/media/123.jpg?width=100');
232232
$filesystem = $this->createMock(PrefixFilesystem::class);
233-
$filesystem->expects(static::once())
233+
$filesystem->expects($this->once())
234234
->method('publicUrl')
235235
->willReturn('https://localhost');
236236

@@ -242,7 +242,7 @@ public function testMultipleGenerationsUseCachedExtensionAllowList(): void
242242
'ExtensionsAllowList' => 'jpg',
243243
'ThumbnailPattern' => '{mediaUrl}/{mediaPath}?width={width}',
244244
];
245-
$configReader->expects(static::exactly(3))
245+
$configReader->expects($this->exactly(3))
246246
->method('getConfig')
247247
->willReturnCallback(function ($key) use ($defaultPluginConfig) {
248248
return $defaultPluginConfig[$key] ?? null;

0 commit comments

Comments
 (0)