Skip to content

Commit

Permalink
[TASK] Fix phpstan checkFunctionArgumentTypes errors in ext:form
Browse files Browse the repository at this point in the history
This patch fixes incompatible type usage in function arguments
and is preparatory work for introducing native type hints and
strict mode in all core files.

Releases: master, 10.4
Resolves: #92112
Change-Id: Iecb483c2575bd479ee9126034e7390691c954d13
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/65605
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Daniel Goerz <daniel.goerz@posteo.de>
Reviewed-by: Daniel Goerz <daniel.goerz@posteo.de>
  • Loading branch information
alexanderschnitzler authored and ervaude committed Sep 9, 2020
1 parent 80fa43a commit 1a9387e
Show file tree
Hide file tree
Showing 20 changed files with 43 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function createAction(string $formName, string $templatePath, string $pro
}

$templatePath = GeneralUtility::getFileAbsFileName($templatePath);
$form = Yaml::parse(file_get_contents($templatePath));
$form = Yaml::parse((string)file_get_contents($templatePath));
$form['label'] = $formName;
$form['identifier'] = $this->formPersistenceManager->getUniqueIdentifier($this->convertFormNameToIdentifier($formName));
$form['prototypeName'] = $prototypeName;
Expand Down Expand Up @@ -505,7 +505,7 @@ protected function convertFormNameToIdentifier(string $formName): string
$csConverter = GeneralUtility::makeInstance(CharsetConverter::class);

$formIdentifier = $csConverter->specCharsToASCII('utf-8', $formName);
$formIdentifier = preg_replace('/[^a-zA-Z0-9-_]/', '', $formIdentifier);
$formIdentifier = (string)preg_replace('/[^a-zA-Z0-9-_]/', '', $formIdentifier);
$formIdentifier = lcfirst($formIdentifier);
return $formIdentifier;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ protected function executeInternal()
{
$contentElementUid = $this->parseOption('contentElementUid');
$typoscriptObjectPath = $this->parseOption('typoscriptObjectPath');
$typoscriptObjectPath = is_string($typoscriptObjectPath) ? $typoscriptObjectPath : '';
if (!empty($contentElementUid)) {
$pathSegments = GeneralUtility::trimExplode('.', $typoscriptObjectPath);
$lastSegment = array_pop($pathSegments);
Expand Down
2 changes: 2 additions & 0 deletions typo3/sysext/form/Classes/Domain/Finishers/EmailFinisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ protected function executeInternal()
$subject = $this->parseOption('subject');
$recipients = $this->getRecipients('recipients', 'recipientAddress', 'recipientName');
$senderAddress = $this->parseOption('senderAddress');
$senderAddress = is_string($senderAddress) ? $senderAddress : '';
$senderName = $this->parseOption('senderName');
$senderName = is_string($senderName) ? $senderName : '';
$replyToRecipients = $this->getRecipients('replyToRecipients', 'replyToAddress');
$carbonCopyRecipients = $this->getRecipients('carbonCopyRecipients', 'carbonCopyAddress');
$blindCarbonCopyRecipients = $this->getRecipients('blindCarbonCopyRecipients', 'blindCarbonCopyAddress');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ protected function executeInternal()
$this->uriBuilder = $this->objectManager->get(UriBuilder::class);
$this->uriBuilder->setRequest($this->request);

$pageUid = (int)str_replace('pages_', '', $this->parseOption('pageUid'));
$pageUid = $this->parseOption('pageUid');
$pageUid = is_string($pageUid) ? $pageUid : '';
$pageUid = (int)str_replace('pages_', '', $pageUid);
$additionalParameters = $this->parseOption('additionalParameters');
$additionalParameters = is_string($additionalParameters) ? $additionalParameters : '';
$additionalParameters = '&' . ltrim($additionalParameters, '&');
$delay = (int)$this->parseOption('delay');
$statusCode = (int)$this->parseOption('statusCode');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ protected function executeInternal()
*
* @param array $elementsConfiguration
* @param array $databaseData
* @return mixed
* @return array
*/
protected function prepareData(array $elementsConfiguration, array $databaseData)
{
Expand Down Expand Up @@ -273,7 +273,9 @@ protected function process(int $iterationCount)
$this->throwExceptionOnInconsistentConfiguration();

$table = $this->parseOption('table');
$table = is_string($table) ? $table : '';
$elementsConfiguration = $this->parseOption('elements');
$elementsConfiguration = is_array($elementsConfiguration) ? $elementsConfiguration : [];
$databaseColumnMappingsConfiguration = $this->parseOption('databaseColumnMappings');

$this->databaseConnection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table);
Expand Down
2 changes: 2 additions & 0 deletions typo3/sysext/form/Classes/Domain/Model/FormDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ public function createPage(string $identifier, string $typeName = 'Page'): Page
throw new TypeDefinitionNotFoundException(sprintf('The "implementationClassName" was not set in type definition "%s".', $typeName), 1477083126);
}
$implementationClassName = $typeDefinition['implementationClassName'];
/** @var Page $page */
$page = $this->objectManager->get($implementationClassName, $identifier, $typeName);

if (isset($typeDefinition['label'])) {
Expand Down Expand Up @@ -511,6 +512,7 @@ public function createFinisher(string $finisherIdentifier, array $options = []):
$defaultOptions = $this->finishersDefinition[$finisherIdentifier]['options'] ?? [];
ArrayUtility::mergeRecursiveWithOverrule($defaultOptions, $options);

/** @var FinisherInterface $finisher */
$finisher = $this->objectManager->get($implementationClassName, $finisherIdentifier);
$finisher->setOptions($defaultOptions);
$this->addFinisher($finisher);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function getUniqueIdentifier(): string
{
$formDefinition = $this->getRootForm();
$uniqueIdentifier = sprintf('%s-%s', $formDefinition->getIdentifier(), $this->identifier);
$uniqueIdentifier = preg_replace('/[^a-zA-Z0-9_-]/', '_', $uniqueIdentifier);
$uniqueIdentifier = (string)preg_replace('/[^a-zA-Z0-9_-]/', '_', $uniqueIdentifier);
return lcfirst($uniqueIdentifier);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function getUniqueIdentifier(): string
{
$formDefinition = $this->getRootForm();
$uniqueIdentifier = sprintf('%s-%s', $formDefinition->getIdentifier(), $this->identifier);
$uniqueIdentifier = preg_replace('/[^a-zA-Z0-9-_]/', '_', $uniqueIdentifier);
$uniqueIdentifier = (string)preg_replace('/[^a-zA-Z0-9-_]/', '_', $uniqueIdentifier);
return lcfirst($uniqueIdentifier);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ public function createValidator(string $validatorIdentifier, array $options = []

ArrayUtility::mergeRecursiveWithOverrule($defaultOptions, $options);

/** @var ValidatorInterface $validator */
$validator = GeneralUtility::makeInstance(ObjectManager::class)
->get($implementationClassName, $defaultOptions);
$this->addValidator($validator);
Expand Down
4 changes: 3 additions & 1 deletion typo3/sysext/form/Classes/Hooks/FormFileExtensionUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ public function executeUpdate(): bool
$newPossiblePersistenceIdentifier,
PATHINFO_BASENAME
);
$newFileName = is_string($newFileName) ? $newFileName : '';

try {
$file->rename($newFileName, DuplicationBehavior::RENAME);
Expand Down Expand Up @@ -405,6 +406,7 @@ protected function getFormDefinitionsInformationFromExtensions(): array

foreach ($this->persistenceManager->retrieveYamlFilesFromExtensionFolders() as $persistenceIdentifier => $_) {
try {
/** @var File $file */
$file = $this->resourceFactory->retrieveFileOrFolderObject($persistenceIdentifier);
} catch (\Exception $exception) {
continue;
Expand Down Expand Up @@ -582,7 +584,7 @@ protected function getSheetIdentifiersForFinisherOverrides(array $flexform): arr
$sheetIdentifiers = [];
foreach ($this->getFinisherSheetsFromFlexform($flexform) as $sheetIdentifier => $sheetData) {
$itemOptionPath = array_keys($sheetData['lDEF']);
$firstSheetItemOptionPath = array_shift($itemOptionPath);
$firstSheetItemOptionPath = (string)array_shift($itemOptionPath);
preg_match('#^settings\.finishers\.(.*)\..+$#', $firstSheetItemOptionPath, $matches);
if (!isset($matches[1])) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,9 @@ protected function hasValidFileExtension(string $fileName): bool
*/
protected function isFileWithinAccessibleExtensionFolders(string $fileName): bool
{
$dirName = rtrim(PathUtility::pathinfo($fileName, PATHINFO_DIRNAME), '/') . '/';
$pathInfo = PathUtility::pathinfo($fileName, PATHINFO_DIRNAME);
$pathInfo = is_string($pathInfo) ? $pathInfo : '';
$dirName = rtrim($pathInfo, '/') . '/';
return array_key_exists($dirName, $this->getAccessibleExtensionFolders());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ public function convertFrom($source, $targetType, array $convertedChildPropertie
return $this->convertedResources[$source['tmp_name']];
}

if ($configuration === null) {
throw new \InvalidArgumentException('Argument $configuration must not be null', 1589183114);
}

try {
$resource = $this->importUploadedResource($source, $configuration);
} catch (TypeConverterException $e) {
Expand Down
4 changes: 2 additions & 2 deletions typo3/sysext/form/Classes/Mvc/Validation/CountValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function isValid($value)
$this->translateErrorMessage(
'validation.error.1475002976',
'form'
),
) ?? '',
1475002976
);
return;
Expand All @@ -65,7 +65,7 @@ public function isValid($value)
'validation.error.1475002994',
'form',
[$minimum, $maximum]
),
) ?? '',
1475002994,
[$this->options['minimum'], $this->options['maximum']]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function isValid($value)
'validation.error.1521293685',
'form',
[gettype($value)]
),
) ?? '',
1521293685
);

Expand All @@ -70,7 +70,7 @@ public function isValid($value)
'validation.error.1521293686',
'form',
[$minimum->format($format)]
),
) ?? '',
1521293686,
[$minimum->format($format)]
);
Expand All @@ -85,7 +85,7 @@ public function isValid($value)
'validation.error.1521293687',
'form',
[$maximum->format($format)]
),
) ?? '',
1521293687,
[$maximum->format($format)]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function isValid($value)
$this->translateErrorMessage(
'validation.error.1476396435',
'form'
),
) ?? '',
1476396435
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function isValid($resource)
$this->translateErrorMessage(
'validation.error.1505303626',
'form'
),
) ?? '',
1505303626
);
return;
Expand All @@ -71,7 +71,7 @@ public function isValid($resource)
'validation.error.1505305752',
'form',
[GeneralUtility::formatSize($minFileSize, $labels)]
),
) ?? '',
1505305752,
[GeneralUtility::formatSize($minFileSize, $labels)]
);
Expand All @@ -82,7 +82,7 @@ public function isValid($resource)
'validation.error.1505305753',
'form',
[GeneralUtility::formatSize($maxFileSize, $labels)]
),
) ?? '',
1505305753,
[GeneralUtility::formatSize($maxFileSize, $labels)]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function isValid($resource)
$this->translateErrorMessage(
'validation.error.1471708997',
'form'
),
) ?? '',
1471708997
);
return;
Expand All @@ -68,7 +68,7 @@ public function isValid($resource)
'validation.error.1471708998',
'form',
[$resource->getMimeType()]
),
) ?? '',
1471708998,
[$resource->getMimeType()]
);
Expand Down
4 changes: 2 additions & 2 deletions typo3/sysext/form/Classes/Service/TranslationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function translate(
$this->languageKey = $language;
}

$this->initializeLocalization($locallangPathAndFilename);
$this->initializeLocalization($locallangPathAndFilename ?? '');

// The "from" charset of csConv() is only set for strings from TypoScript via _LOCAL_LANG
if (!empty($this->LOCAL_LANG[$this->languageKey][$key][0]['target'])
Expand Down Expand Up @@ -471,7 +471,7 @@ public function translateFormElementError(
if (is_array($validationErrors)) {
foreach ($validationErrors as $validationError) {
if ((int)$validationError['code'] === $code) {
return sprintf($validationError['message'], $arguments);
return sprintf($validationError['message'], ...$arguments);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions typo3/sysext/form/Classes/Slot/FilePersistenceSlot.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,15 @@ public function onPreFileAdd(BeforeFileAddedEvent $event): void
$this->assertFileName(
self::COMMAND_FILE_ADD,
$combinedFileIdentifier,
file_get_contents($event->getSourceFilePath())
(string)file_get_contents($event->getSourceFilePath())
);
}

public function onPreFileRename(BeforeFileRenamedEvent $event): void
{
$combinedFileIdentifier = $this->buildCombinedIdentifier(
$event->getFile()->getParentFolder(),
$event->getTargetFileName()
$event->getTargetFileName() ?? ''
);

$this->assertFileName(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public static function renderStatic(

$classes[] = str_replace(
'{@numbersOfColumnsToUse}',
$numbersOfColumnsToUse,
(string)$numbersOfColumnsToUse,
$configuration['classPattern']
);
}
Expand Down

0 comments on commit 1a9387e

Please sign in to comment.