Skip to content

Commit 740b217

Browse files
Fix PHPStan issues
1 parent a0bab97 commit 740b217

5 files changed

Lines changed: 68 additions & 9 deletions

File tree

src/Controller/Images.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,20 @@ public function getImagesList(Request $request): JsonResponse
4646
], Response::HTTP_FORBIDDEN);
4747
}
4848

49-
$locationName = $request->query->get('location', 'files');
49+
$locationName = $request->query->getString('location', 'files');
5050
$path = $this->config->getPath($locationName, true);
5151

5252
$files = $this->getImageFilesIndex($path);
5353

5454
return new JsonResponse($files);
5555
}
5656

57+
/**
58+
* @return Collection<int, array{
59+
* thumb: string,
60+
* url: string,
61+
* }>
62+
*/
5763
private function getImageFilesIndex(string $path): Collection
5864
{
5965
$glob = '*.{' . implode(',', self::getImageTypes()) . '}';
@@ -82,14 +88,21 @@ public function getFilesList(Request $request): JsonResponse
8288
], Response::HTTP_FORBIDDEN);
8389
}
8490

85-
$locationName = $request->query->get('location', 'files');
91+
$locationName = $request->query->getString('location', 'files');
8692
$path = $this->config->getPath($locationName, true);
8793

8894
$files = $this->getFilesIndex($path);
8995

9096
return new JsonResponse($files);
9197
}
9298

99+
/**
100+
* @return Collection<int, array{
101+
* title: string,
102+
* url: string,
103+
* size: string,
104+
* }>
105+
*/
93106
private function getFilesIndex(string $path): Collection
94107
{
95108
$glob = '*.{' . implode(',', self::getFileTypes()) . '}';
@@ -121,11 +134,17 @@ private function findFiles(string $path, ?string $glob = null): Finder
121134
return $finder;
122135
}
123136

137+
/**
138+
* @return string[]
139+
*/
124140
private static function getImageTypes(): array
125141
{
126142
return ['gif', 'png', 'jpg', 'jpeg', 'svg', 'avif', 'webp'];
127143
}
128144

145+
/**
146+
* @return string[]
147+
*/
129148
private static function getFileTypes(): array
130149
{
131150
return ['doc', 'docx', 'txt', 'pdf', 'xls', 'xlsx', 'zip', 'tgz', 'gz'];

src/Controller/Upload.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public function handleUpload(Request $request): JsonResponse
4848
], Response::HTTP_FORBIDDEN);
4949
}
5050

51-
$locationName = $request->query->get('location', '');
52-
$path = $request->query->get('path', '');
51+
$locationName = $request->query->getString('location');
52+
$path = $request->query->getString('path');
5353

5454
$target = $this->config->getPath($locationName, true, $path);
5555

src/Extension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ public function initialize(): void
2222

2323
public function install(): void
2424
{
25+
/** @var string $projectDir */
2526
$projectDir = $this->getContainer()->getParameter('kernel.project_dir');
27+
/** @var string $public */
2628
$public = $this->getContainer()->getParameter('bolt.public_folder');
2729

2830
$source = dirname(__DIR__) . '/assets/';

src/RedactorConfig.php

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Bolt\Entity\Content;
99
use Bolt\Extension\ExtensionRegistry;
1010
use Bolt\Storage\Query;
11+
use Pagerfanta\PagerfantaInterface;
12+
use RuntimeException;
1113
use Symfony\Bundle\SecurityBundle\Security;
1214
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1315
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
@@ -18,7 +20,10 @@ class RedactorConfig
1820
{
1921
private const CACHE_DURATION = 1800; // 30 minutes
2022

23+
/** @var array<string, null|bool|string|array<string, array<string, bool|string>|string>> */
2124
private ?array $config = null;
25+
26+
/** @var array<string, string[]> */
2227
private ?array $plugins = null;
2328

2429
public function __construct(
@@ -32,26 +37,32 @@ public function __construct(
3237
) {
3338
}
3439

40+
/**
41+
* @phpstan-ignore missingType.iterableValue (complex type)
42+
*/
3543
public function getConfig(): array
3644
{
3745
if ($this->config) {
3846
return $this->config;
3947
}
4048

41-
$extension = $this->registry->getExtension(Extension::class);
49+
$extension = $this->getExtension();
4250

4351
$this->config = array_replace_recursive($this->getDefaults(), $extension->getConfig()['default'], $this->getLinks());
4452

4553
return $this->config;
4654
}
4755

56+
/**
57+
* @phpstan-ignore missingType.iterableValue (complex type)
58+
*/
4859
public function getPlugins(): array
4960
{
5061
if ($this->plugins) {
5162
return $this->plugins;
5263
}
5364

54-
$extension = $this->registry->getExtension(Extension::class);
65+
$extension = $this->getExtension();
5566

5667
$this->plugins = $this->getDefaultPlugins();
5768

@@ -62,6 +73,9 @@ public function getPlugins(): array
6273
return $this->plugins;
6374
}
6475

76+
/**
77+
* @return array<string, null|bool|string|array<string, array<string, bool|string>|string>>
78+
*/
6579
public function getDefaults(): array
6680
{
6781
$defaults = [
@@ -113,6 +127,9 @@ public function getDefaults(): array
113127
return $defaults;
114128
}
115129

130+
/**
131+
* @return array<string, string[]>
132+
*/
116133
public function getDefaultPlugins(): array
117134
{
118135
return [
@@ -142,6 +159,9 @@ public function getDefaultPlugins(): array
142159
];
143160
}
144161

162+
/**
163+
* @phpstan-ignore missingType.iterableValue (complex type)
164+
*/
145165
private function getLinks(): array
146166
{
147167
return $this->cache->get('redactor_insert_links', function (ItemInterface $item): array {
@@ -151,6 +171,9 @@ private function getLinks(): array
151171
});
152172
}
153173

174+
/**
175+
* @phpstan-ignore missingType.iterableValue (complex type)
176+
*/
154177
private function getLinksHelper(): array
155178
{
156179
$amount = 100;
@@ -161,7 +184,11 @@ private function getLinksHelper(): array
161184
];
162185
$contentTypes = $this->boltConfig->get('contenttypes')->where('viewless', false)->keys()->implode(',');
163186

164-
$records = $this->query->getContentForTwig($contentTypes, $params)->setMaxPerPage($amount);
187+
/** @var Content[]|PagerfantaInterface<Content> $records */
188+
$records = $this->query->getContentForTwig($contentTypes, $params) ?? [];
189+
if ($records instanceof PagerfantaInterface) {
190+
$records->setMaxPerPage($amount);
191+
}
165192

166193
$links = [
167194
'___' => [
@@ -170,7 +197,6 @@ private function getLinksHelper(): array
170197
],
171198
];
172199

173-
/** @var Content $record */
174200
foreach ($records as $record) {
175201
$extras = $record->getExtras();
176202

@@ -186,4 +212,12 @@ private function getLinksHelper(): array
186212
'definedlinks' => array_values($links),
187213
];
188214
}
215+
216+
public function getExtension(): Extension
217+
{
218+
/** @var Extension|null $extension */
219+
$extension = $this->registry->getExtension(Extension::class);
220+
221+
return $extension ?? throw new RuntimeException('Redactor extension not registered');
222+
}
189223
}

src/RedactorInjectorWidget.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Bolt\Widget\Injector\RequestZone;
99
use Bolt\Widget\Injector\Target;
1010
use Bolt\Widget\TwigAwareInterface;
11+
use Symfony\Component\HttpFoundation\Request;
1112

1213
class RedactorInjectorWidget extends BaseWidget implements TwigAwareInterface
1314
{
@@ -21,12 +22,15 @@ public function __construct()
2122
{
2223
}
2324

25+
/**
26+
* @phpstan-ignore missingType.iterableValue (not used here)
27+
*/
2428
public function run(array $params = []): ?string
2529
{
2630
$request = $this->getExtension()->getRequest();
2731
// Only produce output when editing or creating a Record, with GET method.
2832
if (! in_array($request->get('_route'), ['bolt_content_edit', 'bolt_content_new', 'bolt_content_duplicate'], true) ||
29-
($this->getExtension()->getRequest()->getMethod() !== 'GET')) {
33+
($this->getExtension()->getRequest()->getMethod() !== Request::METHOD_GET)) {
3034
return null;
3135
}
3236

0 commit comments

Comments
 (0)