Skip to content

Commit 69132bf

Browse files
authored
Handle strict type errors (see #110)
* Cast interger values to string (#108) * Handle more strict type logic errors
1 parent c68bade commit 69132bf

File tree

9 files changed

+57
-29
lines changed

9 files changed

+57
-29
lines changed

src/ContaoManager/Plugin.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public function getBundles(ParserInterface $parser): array
3333
];
3434
}
3535

36+
/**
37+
* @throws \Exception
38+
*/
3639
public function getRouteCollection(LoaderResolverInterface $resolver, KernelInterface $kernel): RouteCollection|null
3740
{
3841
return $resolver

src/Controller/BackendModule/ImportController.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public function __invoke(): Response
5151
// Add xml extension as valid upload type
5252
Config::set('uploadTypes', Config::get('uploadTypes') . ',xml');
5353

54-
/** @var FileUpload $objUploader */
5554
$objUploader = new FileUpload();
5655

5756
// Get current request
@@ -143,7 +142,10 @@ public function createImportTree($archives, $groups, $files): array
143142
{
144143
$collection[ $alias ] = $group;
145144

146-
array_splice($_groups, array_search($alias, $_groups), 1);
145+
// If nothing was found, cast strings and false to 0
146+
$offset = (int) array_search($alias, $_groups);
147+
148+
array_splice($_groups, $offset, 1);
147149
}
148150
}
149151

@@ -191,6 +193,7 @@ public function importByFileUploader(FileUpload $objUploader, bool $partial = fa
191193
$uploaded = $objUploader->uploadTo('system/tmp');
192194

193195
// Get root dir
196+
/** @var string $rootDir */
194197
$rootDir = System::getContainer()->getParameter('kernel.project_dir');
195198

196199
if (empty($uploaded))
@@ -273,7 +276,7 @@ public static function importFiles(array $files, bool $blnSave = true, ?array $a
273276
// Continue if there is no readable XML file
274277
if (!$xml->loadXML($objFile->getContent()))
275278
{
276-
Message::addError($translator->trans('tl_theme.missing_xml', [basename($filePath)], 'contao_default'));
279+
Message::addError($translator?->trans('tl_theme.missing_xml', [basename($filePath)], 'contao_default'));
277280
continue;
278281
}
279282

@@ -362,7 +365,7 @@ public static function importFiles(array $files, bool $blnSave = true, ?array $a
362365
if($strField === 'field')
363366
{
364367
$strName = $archive->item($a)->getAttribute('title');
365-
$strValue = $archive->item($a)->nodeValue;
368+
$strValue = $archive->item($a)->nodeValue ?? '';
366369

367370
if($strName === 'id' || strtolower($strValue) === 'null')
368371
{
@@ -425,9 +428,12 @@ public static function importFiles(array $files, bool $blnSave = true, ?array $a
425428
case 'cssClasses':
426429
if($objChildren->{$strName})
427430
{
431+
/** @var array<array<int|string>> $arrClasses */
428432
$arrClasses = StringUtil::deserialize($objChildren->{$strName}, true);
429433
$arrExists = Sync::flattenKeyValueArray($arrClasses);
430-
$arrValues = StringUtil::deserialize($strValue, true);
434+
435+
/** @var array<array<int|string>> $arrValues */
436+
$arrValues = StringUtil::deserialize($strValue, true);
431437

432438
foreach($arrValues as $cssClass)
433439
{
@@ -451,7 +457,9 @@ public static function importFiles(array $files, bool $blnSave = true, ?array $a
451457

452458
if(isset($dcaField['eval']['multiple']) && !!$dcaField['eval']['multiple'] && $dcaField['inputType'] === 'checkbox')
453459
{
460+
/** @var array<array<int|string>> $arrElements */
454461
$arrElements = StringUtil::deserialize($objChildren->{$strName}, true);
462+
/** @var array<array<int|string>> $arrValues */
455463
$arrValues = StringUtil::deserialize($strValue, true);
456464

457465
foreach($arrValues as $element)

src/EventSubscriber/KernelRequestSubscriber.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717

1818
class KernelRequestSubscriber implements EventSubscriberInterface
1919
{
20-
protected $scopeMatcher;
20+
protected ScopeMatcher $scopeMatcher;
2121

2222
public function __construct(ScopeMatcher $scopeMatcher)
2323
{
2424
$this->scopeMatcher = $scopeMatcher;
2525
}
2626

27-
public static function getSubscribedEvents()
27+
public static function getSubscribedEvents(): array
2828
{
2929
return [KernelEvents::REQUEST => 'onKernelRequest'];
3030
}

src/StyleManager/Config.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@ public static function getGroups(?string $table=null): ?array
7676
*/
7777
public static function getBundleConfigurationFiles(): ?array
7878
{
79+
80+
/** @var string $projectDir */
7981
$projectDir = System::getContainer()->getParameter('kernel.project_dir');
80-
$arrFiles = System::getContainer()->get('contao.resource_finder')->findIn('templates')->files()->name('style-manager-*.xml');
82+
$arrFiles = System::getContainer()->get('contao.resource_finder')?->findIn('templates')?->files()?->name('style-manager-*.xml');
8183
$arrBundleConfigs = null;
8284

8385
if ($projectTemplates = array_merge((glob($projectDir . '/templates/style-manager-*.xml') ?: []), (glob($projectDir . '/templates/*/style-manager-*.xml') ?: [])))
@@ -129,9 +131,11 @@ public static function getBundleConfigurationFiles(): ?array
129131
*/
130132
protected function loadBundleConfiguration(): array
131133
{
132-
if($arrFiles = $this->getBundleConfigurationFiles())
133-
{
134-
return ImportController::importFiles($arrFiles, false);
134+
if (
135+
($arrFiles = $this->getBundleConfigurationFiles())
136+
&& (is_array($bundleConfig = ImportController::importFiles($arrFiles, false)))
137+
) {
138+
return $bundleConfig;
135139
}
136140

137141
return [[],[]];

src/StyleManager/StyleManager.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public static function clearClasses(mixed $varValue, $dc)
107107
$arrValues[ $k ] = ' ' . $v . ' ';
108108
}
109109

110+
// Might need a strict type check for varvalue in the future as all following operations won't work with id or null
110111
$varValue = ' ' . $varValue . ' ';
111112
$varValue = str_replace($arrValues, ' ', $varValue);
112113
$varValue = trim(preg_replace('#\s+#', ' ', $varValue));

src/StyleManager/Styles.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public function format(string $format, string $method=''): string
139139

140140
if(null !== $this->exclGroups)
141141
{
142-
$arrValues = array_diff($arrValues, $this->exclGroups);
142+
$arrValues = array_diff($arrValues ?? [], $this->exclGroups);
143143
}
144144

145145
if($arrValues !== null && $jsonValue = json_encode($arrValues))
@@ -224,6 +224,11 @@ private function parseValueType($strValue)
224224
*/
225225
private function removeExcludedGroups(array &$currGroups): void
226226
{
227+
if (null === $this->exclGroups)
228+
{
229+
return;
230+
}
231+
227232
foreach ($this->exclGroups as $exclude)
228233
{
229234
if (array_key_exists($exclude, $currGroups))

src/StyleManager/Sync.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,9 @@ public static function mergeGroupObjects(?StyleManagerModel $objOriginal, ?Style
196196
foreach ($objMerge->row() as $field => $value)
197197
{
198198
if(
199-
($skipEmpty && (!$value || strtolower((string) $value) === 'null')) ||
200-
(null !== $skipFields && in_array($field, $skipFields))
201-
)
202-
{
199+
($skipEmpty && (!$value || strtolower((string) $value) === 'null'))
200+
|| (null !== $skipFields && in_array($field, $skipFields))
201+
) {
203202
continue;
204203
}
205204

@@ -209,9 +208,12 @@ public static function mergeGroupObjects(?StyleManagerModel $objOriginal, ?Style
209208
case 'cssClasses':
210209
if($objOriginal->{$field})
211210
{
211+
/** @var array $arrClasses */
212212
$arrClasses = StringUtil::deserialize($objOriginal->{$field}, true);
213-
$arrExists = self::flattenKeyValueArray($arrClasses);
214-
$arrValues = StringUtil::deserialize($value, true);
213+
$arrExists = self::flattenKeyValueArray($arrClasses);
214+
215+
/** @var array $arrValues */
216+
$arrValues = StringUtil::deserialize($value, true);
215217

216218
foreach($arrValues as $cssClass)
217219
{
@@ -250,8 +252,11 @@ public static function mergeGroupObjects(?StyleManagerModel $objOriginal, ?Style
250252

251253
if(isset($fieldOptions['eval']['multiple']) && !!$fieldOptions['eval']['multiple'] && $fieldOptions['inputType'] === 'checkbox')
252254
{
255+
/** @var array $arrElements */
253256
$arrElements = StringUtil::deserialize($objOriginal->{$field}, true);
254-
$arrValues = StringUtil::deserialize($value, true);
257+
258+
/** @var array $arrValues */
259+
$arrValues = StringUtil::deserialize($value, true);
255260

256261
foreach($arrValues as $element)
257262
{
@@ -399,7 +404,7 @@ protected function addRowData(DOMDocument $xml, DOMNode $row, array $arrData): v
399404
$v = 'NULL';
400405
}
401406

402-
$value = $xml->createTextNode($v);
407+
$value = $xml->createTextNode((string) $v);
403408
$field->appendChild($value);
404409
}
405410
}
@@ -411,7 +416,7 @@ public static function flattenKeyValueArray($arr): array
411416
{
412417
if(empty($arr))
413418
{
414-
return $arr;
419+
return [];
415420
}
416421

417422
$arrTmp = array();

src/Twig/StyleManagerExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function createContext(array $data): Styles
4444
$arrStyles = StringUtil::deserialize($data['styleManager'], true);
4545
}*/
4646

47+
/** @var array $arrStyles */
4748
$arrStyles = StringUtil::deserialize($data['styleManager'], true);
4849

4950
return new Styles($arrStyles[StyleManager::VARS_KEY] ?? null);

src/Widget/ComponentStyleSelect.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ class ComponentStyleSelect extends Widget
4646
*/
4747
protected $strTemplate = 'be_widget';
4848

49-
private readonly ContaoCsrfTokenManager $tokenManager;
49+
private readonly ?ContaoCsrfTokenManager $tokenManager;
5050

51-
private readonly InsertTagParser $insertTagParser;
51+
private readonly ?InsertTagParser $insertTagParser;
5252

53-
private readonly RequestStack $requestStack;
53+
private readonly ?RequestStack $requestStack;
5454

5555
private readonly bool $showGroupTitle;
5656

@@ -120,7 +120,8 @@ public function generate()
120120
// skip specific content elements
121121
if(!!$objStyleGroup->extendContentElement && $this->strTable === 'tl_content')
122122
{
123-
$arrContentElements = StringUtil::deserialize($objStyleGroup->contentElements);
123+
/** @var array $arrContentElements */
124+
$arrContentElements = StringUtil::deserialize($objStyleGroup->contentElements, true);
124125

125126
if($arrContentElements !== null && !in_array($this->activeRecord->type, $arrContentElements))
126127
{
@@ -253,8 +254,8 @@ public function generate()
253254
return $this->renderEmptyMessage();
254255
}
255256

256-
$objSession = $this->requestStack->getSession()->getBag('contao_backend');
257-
$arrSession = $objSession->get('stylemanager_section_states');
257+
$objSession = $this->requestStack?->getSession()->getBag('contao_backend');
258+
$arrSession = $objSession?->get('stylemanager_section_states');
258259

259260
$arrGroups = array();
260261
$arrSections = array();
@@ -290,7 +291,7 @@ public function generate()
290291
$this->id,
291292
$groupAlias,
292293
$identifier,
293-
$this->tokenManager->getDefaultTokenValue()
294+
$this->tokenManager?->getDefaultTokenValue()
294295
);
295296

296297
$arrNavigation[ $index ] = sprintf('<input type="radio" id="nav-%s" class="tab-nav" name="nav-%s" %s><label for="nav-%s" %s>%s</label>',
@@ -304,7 +305,7 @@ public function generate()
304305

305306
$arrContent[ $index ] = sprintf('<div id="tab-%s" class="tab-content">%s%s</div>',
306307
$identifier,
307-
(trim($group['desc'] ?? '') ? '<div class="long desc">' . $this->insertTagParser->replaceInline(nl2br($group['desc'])) . '</div>' : ''),
308+
(trim($group['desc'] ?? '') ? '<div class="long desc">' . $this->insertTagParser?->replaceInline(nl2br($group['desc'])) . '</div>' : ''),
308309
implode("", $group['fields'])
309310
);
310311

0 commit comments

Comments
 (0)