Skip to content

Commit e8e97a3

Browse files
authored
chore: make code more stable (#159)
1 parent f6660da commit e8e97a3

15 files changed

+236
-107
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
parameters:
2+
ignoreErrors:
3+
-
4+
message: "#^Result of && is always false\\.$#"
5+
count: 1
6+
path: src/DependencyInjection/ThumbnailService.php
7+
reportUnmatched: false
8+
9+
-
10+
message: "#^Strict comparison using \\=\\=\\= between false and true will always evaluate to false\\.$#"
11+
count: 1
12+
path: src/DependencyInjection/ThumbnailService.php
13+
reportUnmatched: false
14+
-
15+
message: "#^Call to an undefined method Shopware\\\\Core\\\\Content\\\\Media\\\\Message\\\\GenerateThumbnailsMessage\\:\\:setContext\\(\\)\\.$#"
16+
count: 1
17+
path: src/DependencyInjection/FileSaver.php
18+
reportUnmatched: false
19+
20+
-
21+
message: '#^Call to deprecated method#'
22+
path: src/DependencyInjection/FileSaver.php
23+
reportUnmatched: false
24+
25+
-
26+
message: "#^Method Frosh\\\\ThumbnailProcessor\\\\DependencyInjection\\\\ThumbnailService\\:\\:(.*)\\(\\) is unused\\.$#"
27+
path: src/DependencyInjection/ThumbnailService.php
28+
reportUnmatched: false
29+
30+
-
31+
message: "#^Anonymous function should return array but returns mixed.#"
32+
path: src/DependencyInjection/ThumbnailService.php
33+
reportUnmatched: false
34+
35+
-
36+
message: "#tag\\:v6\\.7\\.0 - reason\\:return-type-change - Return type will be native#"
37+
path: src/DependencyInjection/ThumbnailService.php
38+
reportUnmatched: false
39+
40+
-
41+
message: "#^Frosh\\\\ThumbnailProcessor\\\\DependencyInjection\\\\FileSaver::__construct\\(\\) does not call parent constructor from Shopware\\\\Core\\\\Content\\\\Media\\\\File\\\\FileSaver\\.$#"
42+
count: 1
43+
path: src/DependencyInjection/FileSaver.php
44+
reportUnmatched: false
45+
46+
-
47+
message: "#^Only booleans are allowed in a negated boolean, string\\|null given\\.$#"
48+
count: 1
49+
path: src/DependencyInjection/FileSaver.php
50+
reportUnmatched: false
51+
52+
-
53+
message: "#^Call to function is_string\\(\\) with string will always evaluate to true\\.$#"
54+
count: 1
55+
path: src/DependencyInjection/FileSaver.php
56+
reportUnmatched: false
57+
58+
-
59+
message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#"
60+
count: 1
61+
path: src/DependencyInjection/FileSaver.php
62+
reportUnmatched: false
63+
64+
-
65+
message: "#^Casting to string something that's already string\\.$#"
66+
count: 1
67+
path: src/DependencyInjection/FileSaver.php
68+
reportUnmatched: false
69+
70+
-
71+
message: "#^Only booleans are allowed in a negated boolean, Shopware\\\\Core\\\\Content\\\\Media\\\\Aggregate\\\\MediaThumbnail\\\\MediaThumbnailCollection\\|null given\\.$#"
72+
count: 1
73+
path: src/DependencyInjection/FileSaver.php
74+
reportUnmatched: false
75+
76+
-
77+
message: "#^Frosh\\\\ThumbnailProcessor\\\\DependencyInjection\\\\ThumbnailService::__construct\\(\\) does not call parent constructor from Shopware\\\\Core\\\\Content\\\\Media\\\\Thumbnail\\\\ThumbnailService\\.$#"
78+
count: 1
79+
path: src/DependencyInjection/ThumbnailService.php
80+
reportUnmatched: false
81+
82+
-
83+
message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#"
84+
count: 4
85+
path: src/DependencyInjection/ThumbnailService.php
86+
reportUnmatched: false
87+
88+
-
89+
message: "#^Only booleans are allowed in a negated boolean, Shopware\\\\Core\\\\Content\\\\Media\\\\Aggregate\\\\MediaThumbnail\\\\MediaThumbnailCollection\\|null given\\.$#"
90+
count: 1
91+
path: src/DependencyInjection/ThumbnailService.php
92+
reportUnmatched: false
93+
94+
-
95+
message: "#^Parameter \\#4 \\$metadata of method Frosh\\\\ThumbnailProcessor\\\\DependencyInjection\\\\FileSaver::updateMediaEntity\\(\\) expects array<string, mixed>\\|null, array\\|null given\\.$#"
96+
count: 1
97+
path: src/DependencyInjection/FileSaver.php
98+
reportUnmatched: false

phpstan.neon

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1+
includes:
2+
- phpstan.baseline-generated-files.neon
3+
14
parameters:
25
level: max
36
paths:
47
- src
58
- tests
69
ignoreErrors:
10+
711
-
8-
message: "#^Result of && is always false\\.$#"
9-
count: 1
10-
path: src/DependencyInjection/ThumbnailService.php
12+
message: "#Call to static method PHPUnit\\\\Framework\\\\Assert::assertIsIterable\\(\\) with array<int\\|string, string> will always evaluate to true.#"
1113
reportUnmatched: false
1214

1315
-
14-
message: "#^Strict comparison using \\=\\=\\= between false and true will always evaluate to false\\.$#"
15-
count: 1
16-
path: src/DependencyInjection/ThumbnailService.php
16+
message: "#staticMethod.alreadyNarrowedType#"
17+
path: tests/unit/Core/Media/MediaUrlGeneratorTest.php
18+
reportUnmatched: false
19+
20+
-
21+
message: "#tag\\:v6\\.8\\.0 - reason:return-type-change - return type will be nullable#"
22+
path: tests/integration/MediaUrlTest.php
1723
reportUnmatched: false
1824

1925
-
@@ -38,52 +44,64 @@ parameters:
3844
-
3945
message: "#possible param types, only#"
4046
reportUnmatched: false
47+
4148
-
42-
message: "#^Call to an undefined method Shopware\\\\Core\\\\Content\\\\Media\\\\Message\\\\GenerateThumbnailsMessage\\:\\:setContext\\(\\)\\.$#"
43-
count: 1
44-
path: src/DependencyInjection/FileSaver.php
49+
message: "#(.*?)Shopware\\\\Tests\\\\Unit\\\\Common\\\\Stubs\\\\SystemConfigService\\\\StaticSystemConfigService#"
4550
reportUnmatched: false
4651

4752
-
48-
message: "#^Method Frosh\\\\ThumbnailProcessor\\\\DependencyInjection\\\\ThumbnailService\\:\\:(.*)\\(\\) is unused\\.$#"
49-
path: src/DependencyInjection/ThumbnailService.php
53+
message: "#(.*?)Shopware\\\\Tests\\\\Unit\\\\Common\\\\Stubs\\\\DataAbstractionLayer\\\\StaticEntityRepository#"
54+
reportUnmatched: false
5055

5156
-
52-
message: "#^Anonymous function should return array but returns mixed.#"
53-
path: src/DependencyInjection/ThumbnailService.php
57+
message: '#.* generic class Shopware\\Core\\Framework\\DataAbstractionLayer\\EntityRepository.*not specify its types: TEntityCollection#'
5458
reportUnmatched: false
5559

5660
-
57-
message: "#tag\\:v6\\.7\\.0 - reason\\:return-type-change - Return type will be native#"
58-
path: src/DependencyInjection/ThumbnailService.php
59-
reportUnmatched: false
61+
message: "#^Parameter \\#1 \\$objectOrClass of class ReflectionClass constructor expects class\\-string\\<T of object\\>\\|T of object, string given\\.$#"
62+
count: 1
63+
path: src/DependencyInjection/GeneratorCompilerPass.php
64+
65+
-
66+
message: "#^Property Frosh\\\\ThumbnailProcessor\\\\Tests\\\\Integration\\\\MediaUrlTest.*does not accept object.*#"
67+
path: tests/integration/MediaUrlTest.php
6068

6169
-
62-
message: '#^Call to deprecated method#'
63-
path: src/DependencyInjection/FileSaver.php
70+
message: "#Use AbstractMediaUrlGenerator instead#"
6471
reportUnmatched: false
6572

6673
-
67-
message: "#(.*?)Shopware\\\\Tests\\\\Unit\\\\Common\\\\Stubs\\\\SystemConfigService\\\\StaticSystemConfigService#"
74+
message: "#^Strict comparison using === between array<PhpParser\\\\Node\\\\Stmt> and null will always evaluate to false\\.$#"
75+
count: 1
76+
path: src/DependencyInjection/GeneratorCompilerPass.php
6877
reportUnmatched: false
6978

7079
-
71-
message: "#(.*?)Shopware\\\\Tests\\\\Unit\\\\Common\\\\Stubs\\\\DataAbstractionLayer\\\\StaticEntityRepository#"
80+
message: "#^Call to function method_exists\\(\\) with 'PhpParser\\\\\\\\ParserFactory' and 'createForHostVersion' will always evaluate to true\\.$#"
81+
count: 1
82+
path: src/DependencyInjection/GeneratorCompilerPass.php
7283
reportUnmatched: false
7384

7485
-
75-
message: '#.* generic class Shopware\\Core\\Framework\\DataAbstractionLayer\\EntityRepository.*not specify its types: TEntityCollection#'
86+
message: "#^Access to undefined constant PhpParser\\\\ParserFactory::ONLY_PHP7\\.$#"
87+
count: 1
88+
path: src/DependencyInjection/GeneratorCompilerPass.php
7689
reportUnmatched: false
7790

7891
-
79-
message: "#^Parameter \\#1 \\$objectOrClass of class ReflectionClass constructor expects class\\-string\\<T of object\\>\\|T of object, string given\\.$#"
92+
message: "#^Call to an undefined method PhpParser\\\\ParserFactory::create\\(\\)\\.$#"
8093
count: 1
8194
path: src/DependencyInjection/GeneratorCompilerPass.php
95+
reportUnmatched: false
8296

8397
-
84-
message: "#^Property Frosh\\\\ThumbnailProcessor\\\\Tests\\\\Integration\\\\MediaUrlTest.*does not accept object.*#"
85-
path: tests/integration/MediaUrlTest.php
98+
message: "#^Method Frosh\\\\ThumbnailProcessor\\\\DependencyInjection\\\\GeneratorCompilerPass::getPhpParser\\(\\) should return PhpParser\\\\Parser but returns mixed\\.$#"
99+
count: 1
100+
path: src/DependencyInjection/GeneratorCompilerPass.php
101+
reportUnmatched: false
86102

87103
-
88-
message: "#Use AbstractMediaUrlGenerator instead#"
104+
message: "#^Property PhpParser\\\\Node\\\\Stmt\\\\Namespace_::\\$stmts \\(array<PhpParser\\\\Node\\\\Stmt>\\) does not accept array<PhpParser\\\\Node\\\\Stmt>\\|null\\.$#"
105+
count: 1
106+
path: src/DependencyInjection/GeneratorCompilerPass.php
89107
reportUnmatched: false

src/Controller/Api/TestController.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ private function getProductFolderId(Context $context): string
7979
->addAssociation('defaultFolder')
8080
->setLimit(1);
8181

82-
$ids = $this->mediaFolderRepository
82+
$id = $this->mediaFolderRepository
8383
->searchIds($criteria, $context)
84-
->getIds();
84+
->getIds()[0] ?? null;
8585

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

9090
throw new \RuntimeException('Media folder for product could not have been found!');
@@ -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) {
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

0 commit comments

Comments
 (0)