Skip to content

Commit dee6f02

Browse files
author
Konrad Michalik
authored
Merge pull request #58 from xima-media/phpstan
build: increase phpstan checks
2 parents c755a29 + c8f7fe1 commit dee6f02

13 files changed

Lines changed: 212 additions & 69 deletions

Classes/EventListener/ModifyButtonBarEventListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
use TYPO3\CMS\Backend\Template\Components\Buttons\InputButton;
77
use TYPO3\CMS\Backend\Template\Components\ModifyButtonBarEvent;
88
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
9-
use TYPO3\CMS\Core\Imaging\Icon;
109
use TYPO3\CMS\Core\Imaging\IconFactory;
1110
use TYPO3\CMS\Core\Information\Typo3Version;
1211
use TYPO3\CMS\Core\Localization\LanguageService;
1312
use TYPO3\CMS\Core\Page\PageRenderer;
1413
use TYPO3\CMS\Core\Utility\GeneralUtility;
1514
use Xima\XimaTypo3FrontendEdit\Configuration;
15+
use Xima\XimaTypo3FrontendEdit\Utility\IconUtility;
1616

1717
final class ModifyButtonBarEventListener
1818
{
@@ -44,7 +44,7 @@ public function __invoke(ModifyButtonBarEvent $event): void
4444
->setValue('1')
4545
->setForm($saveButton->getForm())
4646
->setTitle($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:rm.saveCloseDoc'))
47-
->setIcon($iconFactory->getIcon('actions-document-save-close', Icon::SIZE_SMALL))
47+
->setIcon($iconFactory->getIcon('actions-document-save-close', IconUtility::getDefaultIconSize()))
4848
->setShowLabelText(true);
4949

5050
$typo3Version = GeneralUtility::makeInstance(Typo3Version::class)->getMajorVersion();

Classes/Middleware/EditInformationMiddleware.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
3939

4040
$data = json_decode($request->getBody()->getContents(), true) ?? [];
4141

42-
if (!$this->checkBackendUserPageAccess((int)$pid)) {
42+
if (!$this->checkBackendUserPageAccess($pid)) {
4343
return new JsonResponse([]);
4444
}
4545

4646
return new JsonResponse(
4747
mb_convert_encoding(
4848
$this->menuGenerator->getDropdown(
49-
(int)$pid,
49+
$pid,
5050
$returnUrl,
51-
(int)$languageUid,
51+
$languageUid,
5252
$data
5353
),
5454
'UTF-8'

Classes/Middleware/ToolRendererMiddleware.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313

1414
class ToolRendererMiddleware implements MiddlewareInterface
1515
{
16-
public function __construct(protected ResourceRenderer $resourceRenderer)
17-
{
18-
}
19-
2016
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
2117
{
2218
$response = $handler->handle($request);
@@ -25,15 +21,15 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
2521
if (
2622
array_key_exists('tx_ximatypo3frontendedit_enable', $typoScriptConfig)
2723
&& $typoScriptConfig['tx_ximatypo3frontendedit_enable']
28-
&& $GLOBALS['BE_USER']
24+
&& $GLOBALS['BE_USER'] !== null
2925
&& (!array_key_exists('tx_ximatypo3frontendedit_disable', $GLOBALS['BE_USER']->user) || !$GLOBALS['BE_USER']->user['tx_ximatypo3frontendedit_disable'])
3026
) {
3127
$body = $response->getBody();
3228
$body->rewind();
3329
$contents = $response->getBody()->getContents();
3430
$content = str_ireplace(
3531
'</body>',
36-
$this->resourceRenderer->render() . '</body>',
32+
ResourceRenderer::render() . '</body>',
3733
$contents
3834
);
3935
$body = new Stream('php://temp', 'rw');

Classes/Service/MenuGenerator.php

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
1111
use TYPO3\CMS\Core\Core\Bootstrap;
1212
use TYPO3\CMS\Core\EventDispatcher\EventDispatcher;
13-
use TYPO3\CMS\Core\Imaging\Icon;
1413
use TYPO3\CMS\Core\Imaging\IconFactory;
1514
use TYPO3\CMS\Core\Utility\GeneralUtility;
1615
use Xima\XimaTypo3FrontendEdit\Configuration;
1716
use Xima\XimaTypo3FrontendEdit\Enumerations\ButtonType;
1817
use Xima\XimaTypo3FrontendEdit\Event\FrontendEditDropdownModifyEvent;
1918
use Xima\XimaTypo3FrontendEdit\Template\Component\Button;
2019
use Xima\XimaTypo3FrontendEdit\Utility\ContentUtility;
20+
use Xima\XimaTypo3FrontendEdit\Utility\IconUtility;
2121
use Xima\XimaTypo3FrontendEdit\Utility\UrlUtility;
2222

2323
final class MenuGenerator
@@ -42,7 +42,7 @@ public function getDropdown(int $pid, string $returnUrl, int $languageUid, array
4242
}
4343
}
4444

45-
/* @var $backendUser \TYPO3\CMS\Core\Authentication\BackendUserAuthentication */
45+
/* @var $backendUser BackendUserAuthentication */
4646
$backendUser = $GLOBALS['BE_USER'];
4747
if ($backendUser->user === null) {
4848
Bootstrap::initializeBackendAuthentication();
@@ -80,7 +80,7 @@ public function getDropdown(int $pid, string $returnUrl, int $languageUid, array
8080
$contentElementConfig = ContentUtility::getContentElementConfig($contentElement['CType'], $contentElement['list_type']);
8181
$returnUrlAnchor = $returnUrl . '#c' . $contentElement['uid'];
8282

83-
$simpleMode = (array_key_exists('simpleMode', $this->configuration) && $this->configuration['simpleMode']) || $this->settingsService->checkSimpleModeMenuStructure();
83+
$simpleMode = (array_key_exists('simpleMode', $this->configuration) && (bool)$this->configuration['simpleMode']) || $this->settingsService->checkSimpleModeMenuStructure();
8484

8585
if ($simpleMode) {
8686
$menuButton = new Button(
@@ -98,14 +98,14 @@ public function getDropdown(int $pid, string $returnUrl, int $languageUid, array
9898
'returnUrl' => $returnUrlAnchor,
9999
]
100100
)->__toString(),
101-
icon: $this->iconFactory->getIcon('actions-open', Icon::SIZE_SMALL),
102-
targetBlank: array_key_exists('linkTargetBlank', $this->configuration) && $this->configuration['linkTargetBlank']
101+
icon: $this->iconFactory->getIcon('actions-open', IconUtility::getDefaultIconSize()),
102+
targetBlank: array_key_exists('linkTargetBlank', $this->configuration) && (bool)$this->configuration['linkTargetBlank']
103103
);
104104
} else {
105105
$menuButton = new Button(
106106
'LLL:EXT:xima_typo3_frontend_edit/Resources/Private/Language/locallang.xlf:edit_menu',
107107
ButtonType::Menu,
108-
icon: $this->iconFactory->getIcon('actions-open', Icon::SIZE_SMALL)
108+
icon: $this->iconFactory->getIcon('actions-open', IconUtility::getDefaultIconSize())
109109
);
110110

111111
/*
@@ -118,7 +118,7 @@ public function getDropdown(int $pid, string $returnUrl, int $languageUid, array
118118
$menuButton,
119119
'header',
120120
ButtonType::Info,
121-
label: $GLOBALS['LANG']->sL($contentElementConfig['label']) . '<p><small>' . ($contentElement['header'] ? ContentUtility::shortenString($contentElement['header']) : '') . $additionalUid . '</small></p>',
121+
label: $GLOBALS['LANG']->sL($contentElementConfig['label']) . '<p><small>' . ($contentElement['header'] !== null ? ContentUtility::shortenString($contentElement['header']) : '') . $additionalUid . '</small></p>',
122122
icon: $contentElementConfig['icon']
123123
);
124124

@@ -254,31 +254,39 @@ private function processNewButton(Button &$button, string $identifier, ButtonTyp
254254
}
255255

256256
$button->appendChild(new Button(
257-
$label ?: "LLL:EXT:xima_typo3_frontend_edit/Resources/Private/Language/locallang.xlf:$identifier",
257+
$label !== null ? $label : "LLL:EXT:xima_typo3_frontend_edit/Resources/Private/Language/locallang.xlf:$identifier",
258258
$type,
259259
$url,
260-
$icon ? $this->iconFactory->getIcon($icon, Icon::SIZE_SMALL) : null,
260+
$icon !== null ? $this->iconFactory->getIcon($icon, IconUtility::getDefaultIconSize()) : null,
261261
array_key_exists('linkTargetBlank', $this->configuration) && $this->configuration['linkTargetBlank']
262262
), $identifier);
263263
}
264264

265265
private function handleAdditionalData(Button $button, array $contentElement, array $contentElementConfig, array $data, BackendUserAuthentication $backendUser, int $languageUid, string $returnUrlAnchor): void
266266
{
267-
if ((array_key_exists($contentElement['uid'], $data) && ($uid = $contentElement['uid']) && !empty($data[$uid])) ||
268-
(array_key_exists('l10n_source', $contentElement) && array_key_exists($contentElement['l10n_source'], $data) && ($uid = $contentElement['l10n_source']) && !empty($data[$uid]))
267+
$uid = null;
268+
if (
269+
(array_key_exists($contentElement['uid'], $data) && $data[$contentElement['uid']] !== []) ||
270+
(
271+
array_key_exists('l10n_source', $contentElement)
272+
&& array_key_exists($contentElement['l10n_source'], $data)
273+
&& $data[$contentElement['l10n_source']] !== []
274+
&& ($uid = $contentElement['l10n_source']) !== 0
275+
)
269276
) {
277+
$uid = $uid ?? $contentElement['uid'];
270278
$button->appendChild(new Button(
271279
'LLL:EXT:xima_typo3_frontend_edit/Resources/Private/Language/locallang.xlf:div_data',
272280
ButtonType::Divider
273281
), 'div_data');
274282

275283
foreach ($data[$uid] as $key => $dataEntry) {
276-
if (!$dataEntry['label'] || !(($dataEntry['table'] && $dataEntry['uid']) || ($dataEntry['url']))) {
284+
if (!$dataEntry['label'] || !(($dataEntry['table'] !== null && $dataEntry['uid'] !== null) || ($dataEntry['url'] !== null))) {
277285
continue;
278286
}
279287

280288
$recordUid = null;
281-
if ($dataEntry['table'] && $dataEntry['uid']) {
289+
if ($dataEntry['table'] !== null && $dataEntry['uid'] !== null) {
282290
if (!$backendUser->recordEditAccessInternals($dataEntry['table'], $dataEntry)) {
283291
continue;
284292
}
@@ -288,7 +296,7 @@ private function handleAdditionalData(Button $button, array $contentElement, arr
288296
* Check if the record is translated and if so, get the translation
289297
*/
290298
$record = BackendUtility::getRecord($dataEntry['table'], $recordUid);
291-
if ($record && array_key_exists('sys_language_uid', $record) && $record['sys_language_uid'] !== $languageUid) {
299+
if ($record !== null && array_key_exists('sys_language_uid', $record) && $record['sys_language_uid'] !== $languageUid) {
292300
$record = ContentUtility::getTranslatedRecord($dataEntry['table'], $recordUid, $languageUid);
293301
if ($record) {
294302
$recordUid = $record['uid'];
@@ -298,7 +306,7 @@ private function handleAdditionalData(Button $button, array $contentElement, arr
298306
}
299307
}
300308

301-
$url = $dataEntry['url'] ?: GeneralUtility::makeInstance(UriBuilder::class)->buildUriFromRoute(
309+
$url = $dataEntry['url'] !== null ? $dataEntry['url'] : GeneralUtility::makeInstance(UriBuilder::class)->buildUriFromRoute(
302310
'record_edit',
303311
[
304312
'edit' => [
@@ -310,7 +318,7 @@ private function handleAdditionalData(Button $button, array $contentElement, arr
310318
'returnUrl' => $returnUrlAnchor,
311319
],
312320
)->__toString();
313-
$icon = $dataEntry['icon'] ? $this->iconFactory->getIcon($dataEntry['icon'], Icon::SIZE_SMALL) : $this->iconFactory->getIcon($contentElementConfig['icon'], Icon::SIZE_SMALL);
321+
$icon = $dataEntry['icon'] !== null ? $this->iconFactory->getIcon($dataEntry['icon'], IconUtility::getDefaultIconSize()) : $this->iconFactory->getIcon($contentElementConfig['icon'], IconUtility::getDefaultIconSize());
314322

315323
$button->appendChild(new Button(
316324
ContentUtility::shortenString($dataEntry['label']),

Classes/Service/SettingsService.php

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function checkSimpleModeMenuStructure(): bool
6161

6262
$menuStructure = $configuration['defaultMenuStructure'];
6363

64-
if (!is_array($menuStructure) || empty($menuStructure)) {
64+
if (!is_array($menuStructure) || $menuStructure === []) {
6565
return false;
6666
}
6767

@@ -79,14 +79,16 @@ public function checkSimpleModeMenuStructure(): bool
7979

8080
private function getConfiguration(): array
8181
{
82-
if (!empty($this->configuration)) {
82+
if ($this->configuration !== []) {
8383
return $this->configuration;
8484
}
8585

8686
if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '12.0.0', '<')) {
8787
$fullTypoScript = $this->getTypoScriptSetupArrayV11();
88-
} else {
88+
} elseif (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '13.0.0', '<')) {
8989
$fullTypoScript = $this->getTypoScriptSetupArrayV12($GLOBALS['TYPO3_REQUEST']);
90+
} else {
91+
$fullTypoScript = $this->getTypoScriptSetupArrayV13($GLOBALS['TYPO3_REQUEST']);
9092
}
9193

9294
$settings = $fullTypoScript['plugin.']['tx_ximatypo3frontendedit.']['settings.'] ?? [];
@@ -104,9 +106,9 @@ private function getConfiguration(): array
104106
private function getTypoScriptSetupArrayV11(): array
105107
{
106108
// Ensure, TSFE setup is loaded for cached pages
107-
if ($GLOBALS['TSFE']->tmpl === null || ($GLOBALS['TSFE']->tmpl && empty($GLOBALS['TSFE']->tmpl->setup))) {
108-
$this->context
109-
->setAspect('typoscript', GeneralUtility::makeInstance(TypoScriptAspect::class, true)); // @phpstan-ignore-line
109+
if ($GLOBALS['TSFE']->tmpl === null || ($GLOBALS['TSFE']->tmpl && $GLOBALS['TSFE']->tmpl->setup === [])) {
110+
/* @phpstan-ignore-next-line */
111+
$this->context->setAspect('typoscript', GeneralUtility::makeInstance(\TYPO3\CMS\Core\Context\TypoScriptAspect::class, true));
110112
$GLOBALS['TSFE']->getConfigArray();
111113
}
112114
return $GLOBALS['TSFE']->tmpl->setup;
@@ -116,26 +118,39 @@ private function getTypoScriptSetupArrayV12(ServerRequestInterface $request): ar
116118
{
117119
try {
118120
$fullTypoScript = $request->getAttribute('frontend.typoscript')->getSetupArray();
119-
} catch (\Exception $e) {
121+
} catch (\Exception) {
120122
// An exception is thrown, when TypoScript setup array is not available. This is usually the case,
121123
// when the current page request is cached. Therefore, the TSFE TypoScript parsing is forced here.
122124

123125
// ToDo: This workaround is not working for TYPO3 v13
124126
// @see https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/13.0/Breaking-102583-RemovedContextAspectTyposcript.html#breaking-102583-1701510037
125-
if (class_exists(\TYPO3\CMS\Core\Context\TypoScriptAspect::class)) {
127+
if (!class_exists(\TYPO3\CMS\Core\Context\TypoScriptAspect::class)) {
126128
return [];
127129
}
128130

129131
// Set a TypoScriptAspect which forces template parsing
130-
$this->context
131-
->setAspect('typoscript', GeneralUtility::makeInstance(TypoScriptAspect::class, true)); // @phpstan-ignore-line
132+
/* @phpstan-ignore-next-line */
133+
$this->context->setAspect('typoscript', GeneralUtility::makeInstance(\TYPO3\CMS\Core\Context\TypoScriptAspect::class, true));
132134
$tsfe = $request->getAttribute('frontend.controller');
133-
$requestWithFullTypoScript = $tsfe->getFromCache($request);
135+
$requestWithFullTypoScript = $tsfe->getFromCache($request); // @phpstan-ignore-line
134136

135137
// Call TSFE getFromCache, which re-processes TypoScript respecting $forcedTemplateParsing property
136138
// from TypoScriptAspect
137139
$fullTypoScript = $requestWithFullTypoScript->getAttribute('frontend.typoscript')->getSetupArray();
138140
}
139141
return $fullTypoScript;
140142
}
143+
144+
/**
145+
* @param ServerRequestInterface $request
146+
* @return array
147+
*/
148+
private function getTypoScriptSetupArrayV13(ServerRequestInterface $request): array
149+
{
150+
try {
151+
return $request->getAttribute('frontend.typoscript')->getSetupArray();
152+
} catch (\Exception) {
153+
return [];
154+
}
155+
}
141156
}

Classes/Template/Component/Button.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ public function setLabel(string $label): void
3737
}
3838

3939
/**
40-
* @return \Xima\XimaTypo3FrontendEdit\Enumerations\ButtonType
40+
* @return ButtonType
4141
*/
4242
public function getType(): ButtonType
4343
{
4444
return $this->type;
4545
}
4646

4747
/**
48-
* @param \Xima\XimaTypo3FrontendEdit\Enumerations\ButtonType $type
48+
* @param ButtonType $type
4949
*/
5050
public function setType(ButtonType $type): void
5151
{
@@ -99,7 +99,7 @@ public function appendChild(Button $button, string|int $key): void
9999

100100
public function appendAfterChild(Button $button, string|int $appendAfterKey, string|int $key): void
101101
{
102-
$offset = array_search($appendAfterKey, array_keys($this->children)) + 1;
102+
$offset = array_search($appendAfterKey, array_keys($this->children), true) + 1;
103103
$this->children = array_slice($this->children, 0, $offset, true) +
104104
[$key => $button] +
105105
array_slice($this->children, $offset, null, true);
@@ -117,17 +117,17 @@ public function render(): array
117117
'type' => $this->type->value,
118118
];
119119

120-
if ($this->url) {
120+
if ($this->url !== null && $this->url !== '') {
121121
$result['url'] = $this->url;
122122
$result['targetBlank'] = $this->targetBlank;
123123
}
124124

125-
if ($this->icon) {
125+
if ($this->icon instanceof Icon) {
126126
$result['icon'] = $this->icon->getAlternativeMarkup('inline');
127127
}
128128

129-
if (!empty($this->children)) {
130-
$result['children'] = array_map(fn (Button $button) => $button->render(), $this->children);
129+
if ($this->children !== []) {
130+
$result['children'] = array_map(static fn (Button $button) => $button->render(), $this->children);
131131
}
132132

133133
return $result;

Classes/Utility/ContentUtility.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static function isSubpageOf(int $subPageId, int $parentPageId): bool
7676
{
7777
$rootLine = GeneralUtility::makeInstance(RootlineUtility::class, $subPageId)->get();
7878
foreach ($rootLine as $page) {
79-
if ((int)$page['uid'] === (int)$parentPageId) {
79+
if ($page['uid'] === $parentPageId) {
8080
return true;
8181
}
8282
}

Classes/Utility/IconUtility.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Xima\XimaTypo3FrontendEdit\Utility;
6+
7+
use TYPO3\CMS\Core\Imaging\IconSize;
8+
use TYPO3\CMS\Core\Information\Typo3Version;
9+
use TYPO3\CMS\Core\Utility\GeneralUtility;
10+
11+
class IconUtility
12+
{
13+
/**
14+
* ToDo: Version switch be removed in TYPO3 13
15+
*
16+
* @return string|IconSize
17+
*/
18+
public static function getDefaultIconSize(): string|\TYPO3\CMS\Core\Imaging\IconSize
19+
{
20+
$typo3Version = GeneralUtility::makeInstance(Typo3Version::class)->getMajorVersion();
21+
22+
if ($typo3Version >= 13) {
23+
return \TYPO3\CMS\Core\Imaging\IconSize::SMALL;
24+
}
25+
return \TYPO3\CMS\Core\Imaging\Icon::SIZE_SMALL; // @phpstan-ignore classConstant.deprecated
26+
}
27+
}

0 commit comments

Comments
 (0)