Skip to content

Commit

Permalink
[CHORE] Change phpstan level to 6 and cleanup phpstan errors
Browse files Browse the repository at this point in the history
  • Loading branch information
garbast committed Nov 17, 2024
1 parent f2f1064 commit fee79e0
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 10 deletions.
13 changes: 13 additions & 0 deletions Build/Scripts/phpstan.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

THIS_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)"
cd "$THIS_SCRIPT_DIR" || exit 1
cd ../../ || exit 1
CORE_ROOT="${PWD}"

Build/Scripts/runTests.sh -s composerInstall

Build/Scripts/runTests.sh -s phpstan

Build/Scripts/runTests.sh -s clean
Build/Scripts/additionalTests.sh -s clean
2 changes: 2 additions & 0 deletions Build/phpstan/phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
ignoreErrors:
4 changes: 4 additions & 0 deletions Build/phpstan/phpstan-constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

// testing-framework defines this, used in various tests.
define('ORIGINAL_ROOT', dirname(__FILE__, 2) . '/');
2 changes: 2 additions & 0 deletions Build/phpstan/phpstan.local.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
includes:
- phpstan.neon
21 changes: 21 additions & 0 deletions Build/phpstan/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
includes:
- phpstan-baseline.neon
- ../vendor/friendsoftypo3/phpstan-typo3/extension.neon

parameters:
# Use local .cache dir instead of /tmp
tmpDir: ../.cache/phpstan

level: 6

bootstrapFiles:
- phpstan-constants.php

paths:
- ../../

excludePaths:
# we do not check required extensions
- ../../Build/*
# ext_emconf.php get the $_EXTKEY set from outside. We'll ignore all of them
- ../ext_emconf.php
13 changes: 13 additions & 0 deletions Classes/Controller/FileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ public function convertFileAction(ServerRequestInterface $request): ResponseInte
return $moduleTemplate->renderResponse('File/ConvertFile');
}

/**
* @return array<string|array<array<string, string>>>
* @throws PropagateResponseException
*/
protected function prepareExtensions(ServerRequestInterface $request, bool $selected = true): array
{
$extensions = $this->extensionService->getLocalExtensions();
Expand All @@ -133,6 +137,10 @@ protected function prepareExtensions(ServerRequestInterface $request, bool $sele
return [$extensions, $selectedExtension, $selectedExtensionKey];
}

/**
* @return array<string|array<array<string, string>>>
* @throws PropagateResponseException
*/
protected function prepareFiles(ServerRequestInterface $request, string $extension, bool $selected = true): array
{
$files = $this->extensionService->getFilesOfExtension($extension);
Expand All @@ -152,6 +160,7 @@ protected function prepareFiles(ServerRequestInterface $request, string $extensi
}

/**
* @param array<string, mixed> $extensions
* @throws PropagateResponseException
*/
protected function getSelectedExtension(ServerRequestInterface $request, array $extensions): string
Expand All @@ -164,6 +173,7 @@ protected function getSelectedExtension(ServerRequestInterface $request, array $
}

/**
* @param array<string, mixed> $files
* @throws PropagateResponseException
*/
protected function getSelectedFile(ServerRequestInterface $request, array $files): string
Expand All @@ -175,6 +185,9 @@ protected function getSelectedFile(ServerRequestInterface $request, array $files
return $selectedFile;
}

/**
* @param array<string, mixed> $values
*/
protected function isArgumentSetAndAvailable(ServerRequestInterface $request, array $values, string $key): ?string
{
$formFieldValue = (string)($request->getParsedBody()[$key] ?? $request->getQueryParams()[$key] ?? '');
Expand Down
14 changes: 6 additions & 8 deletions Classes/File/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ protected function checkLanguageFilename(string $xmlFile): string

/**
* @param string $languageFile Absolute reference to the base locallang file
*
* @return array
* @return string[]
*/
protected function getAvailableTranslations(string $languageFile): array
{
Expand Down Expand Up @@ -198,10 +197,9 @@ protected function getOriginalFileName(string $filename, string $langKey): strin

/**
* Reads/Requires locallang files and returns raw $LOCAL_LANG array
*
* @param string $languageFile Absolute reference to the ll-XML locallang file.
*
* @return array LOCAL_LANG array from ll-XML file (with all possible sub-files for languages included)
* @return array<string, array<string, string>> LOCAL_LANG array from ll-XML file
* (with all possible sub-files for languages included)
*/
protected function getCombinedTranslationFileContent(string $languageFile): array
{
Expand All @@ -211,15 +209,15 @@ protected function getCombinedTranslationFileContent(string $languageFile): arra
$includedLanguages = array_keys($ll['data']);

foreach ($includedLanguages as $langKey) {
/** @var $parser LocallangXmlParser */
/** @var LocallangXmlParser $parser */
$parser = GeneralUtility::makeInstance(LocallangXmlParser::class);
$localLangContent = $parser->getParsedData($languageFile, $langKey);
unset($parser);
$LOCAL_LANG[$langKey] = $localLangContent[$langKey];
}
} else {
require($languageFile);
$includedLanguages = isset($LOCAL_LANG) ? array_keys($LOCAL_LANG) : [];
$includedLanguages = array_keys($LOCAL_LANG);
}

if (empty($includedLanguages)) {
Expand All @@ -238,7 +236,7 @@ protected function getCombinedTranslationFileContent(string $languageFile): arra
* @param string $namespacePrefix The tag-prefix resolve, e.g. a namespace like "T3:"
* @param bool $reportDocTag If set, the document tag will be set in the key "_DOCUMENT_TAG" of the output array
*
* @return array|string If the parsing had errors, a string with the error message is returned.
* @return array<string, mixed>|string If the parsing had errors, a string with the error message is returned.
* Otherwise, an array with the content.
*
* @see GeneralUtility::array2xml(),GeneralUtility::xml2arrayProcess()
Expand Down
6 changes: 6 additions & 0 deletions Classes/File/TransUnit.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@

protected string $target;

/**
* @param string|array<string, string> $data
* @param string $key
* @param string $langKey
* @param array<string, array<string, string>> $LOCAL_LANG
*/
public function __construct(
string|array $data,
protected string $key,
Expand Down
9 changes: 8 additions & 1 deletion Classes/Localization/Parser/LocallangXmlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class LocallangXmlParser extends AbstractXmlParser
{
/**
* Associative array of "filename => parsed data" pairs.
* @var array<string, array<string, mixed>> $parsedTargetFiles
*/
protected array $parsedTargetFiles = [];

Expand All @@ -38,7 +39,7 @@ class LocallangXmlParser extends AbstractXmlParser
* @param string $sourcePath Source file path
* @param string $languageKey Language key
*
* @return array
* @return array<string, array<string, string>>
*
* @throws FileNotFoundException
* @throws InvalidXmlFileException
Expand Down Expand Up @@ -71,6 +72,7 @@ public function getParsedData($sourcePath, $languageKey): array

/**
* Parse the given language key tag
* @return array<string, array<string, string>>
*/
protected function getParsedDataForElement(\SimpleXMLElement $bodyOfFileTag, string $element): array
{
Expand All @@ -97,6 +99,7 @@ protected function getParsedDataForElement(\SimpleXMLElement $bodyOfFileTag, str

/**
* Returns array representation of XLIFF data, starting from a root node.
* @return array<string, array<string, string>>
*/
protected function doParsingFromRoot(\SimpleXMLElement $root): array
{
Expand All @@ -105,6 +108,7 @@ protected function doParsingFromRoot(\SimpleXMLElement $root): array

/**
* Returns array representation of XLIFF data, starting from a root node.
* @return array<string, array<string, string>>
*/
protected function doParsingTargetFromRoot(\SimpleXMLElement $root): array
{
Expand All @@ -113,6 +117,7 @@ protected function doParsingTargetFromRoot(\SimpleXMLElement $root): array

/**
* Returns array representation of XLIFF data, starting from a root node.
* @return array<string, array<string, string>>
*/
protected function doParsingFromRootForElement(\SimpleXMLElement $root, string $element): array
{
Expand Down Expand Up @@ -153,6 +158,7 @@ protected function doParsingFromRootForElement(\SimpleXMLElement $root, string $
* Returns parsed representation of XML file.
*
* Parses XML if it wasn't done before. Caches parsed data.
* @return array<string, array<string, string>>
*/
protected function getParsedTargetData(string $path): array
{
Expand All @@ -164,6 +170,7 @@ protected function getParsedTargetData(string $path): array

/**
* Reads and parses XML file and returns internal representation of data.
* @return array<string, array<string, string>>
*/
protected function parseXmlTargetFile(string $targetPath): array
{
Expand Down
8 changes: 8 additions & 0 deletions Classes/Service/ExtensionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public function __construct(
protected Converter $converter,
) {}

/**
* @return array<array<string, mixed>>
*/
public function getLocalExtensions(): array
{
$availableExtensions = $this->listUtility->getAvailableExtensions();
Expand All @@ -52,6 +55,7 @@ function (array $extension) {

/**
* Gather files for given extension key that need to be converted
* @return array<string, array<string, string>>
*/
public function getFilesOfExtension(string $extensionKey): array
{
Expand Down Expand Up @@ -82,6 +86,10 @@ protected function isLanguageFile(string $filePath): bool
return str_contains($filePath, 'Resources/Private/Language/');
}

/**
* @param array<string, array<string, string>> $files
* @return array<string, string|bool>
*/
public function convertLanguageFile(string $selectedExtension, string $selectedFile, array $files): array
{
$wasConvertedPreviously = false;
Expand Down
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@
"psr/http-message": "^1.1 || ^2.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.57.1",
"friendsofphp/php-cs-fixer": "^3.64.0",
"friendsoftypo3/phpstan-typo3": "^0.9.0",
"phpstan/phpdoc-parser": "^1.30.0",
"phpstan/phpstan": "^1.12.5",
"phpunit/phpunit": "^11.0.3",
"typo3/testing-framework": "dev-main"
},
"minimum-stability": "dev",
Expand Down

0 comments on commit fee79e0

Please sign in to comment.