diff --git a/Build/phpstan/phpstan-baseline.neon b/Build/phpstan/phpstan-baseline.neon index 88c80239e4bc50be7368e17ff63ebfc36481632c..6393b4de838c741f976ff4ec7783886f94aa9394 100644 --- a/Build/phpstan/phpstan-baseline.neon +++ b/Build/phpstan/phpstan-baseline.neon @@ -1630,11 +1630,6 @@ parameters: count: 1 path: ../../typo3/sysext/core/Classes/Utility/CommandUtility.php - - - message: "#^Method TYPO3\\\\CMS\\\\Core\\\\Utility\\\\CommandUtility\\:\\:checkCommand\\(\\) should return bool but returns int\\.$#" - count: 1 - path: ../../typo3/sysext/core/Classes/Utility/CommandUtility.php - - message: "#^Else branch is unreachable because ternary operator condition is always true\\.$#" count: 1 diff --git a/typo3/sysext/backend/Classes/Backend/ToolbarItems/SystemInformationToolbarItem.php b/typo3/sysext/backend/Classes/Backend/ToolbarItems/SystemInformationToolbarItem.php index bca62953c0e4f3040068375979c1a416dabf44b8..cab1742bfd380795ea09b559835e3cb1a606b97b 100644 --- a/typo3/sysext/backend/Classes/Backend/ToolbarItems/SystemInformationToolbarItem.php +++ b/typo3/sysext/backend/Classes/Backend/ToolbarItems/SystemInformationToolbarItem.php @@ -291,8 +291,9 @@ class SystemInformationToolbarItem implements ToolbarItemInterface, RequestAware return; } // check if git exists + $returnCode = 0; CommandUtility::exec('git --version', $_, $returnCode); - if ((int)$returnCode !== 0) { + if ($returnCode !== 0) { // git is not available return; } diff --git a/typo3/sysext/core/Classes/Utility/CommandUtility.php b/typo3/sysext/core/Classes/Utility/CommandUtility.php index 74b7d0b1c900b2e2b3f6b2bbf70c7b294d9b68f4..05143771eb1ef30ee786ca8aac14622160287a51 100644 --- a/typo3/sysext/core/Classes/Utility/CommandUtility.php +++ b/typo3/sysext/core/Classes/Utility/CommandUtility.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + /* * This file is part of the TYPO3 CMS project. * @@ -49,10 +51,8 @@ class CommandUtility { /** * Tells if object is already initialized - * - * @var bool */ - protected static $initialized = false; + protected static bool $initialized = false; /** * Contains application list. This is an array with the following structure: @@ -61,27 +61,25 @@ class CommandUtility * - valid => TRUE or FALSE * Array key is identical to 'app'. * - * @var array + * @var array<string, array{app: string, path: string, valid: bool}> */ - protected static $applications = []; + protected static array $applications = []; /** * Paths where to search for applications * - * @var array + * The key is a path. The value is either the same path, or false if the path is not valid. + * + * @var array<string, string|false> */ - protected static $paths; + protected static array $paths; /** * Wrapper function for php exec function - * Needs to be central to have better control and possible fix for issues * - * @param string $command - * @param array|null $output - * @param int $returnValue - * @return string + * Needs to be central to have better control and possible fix for issues */ - public static function exec($command, &$output = null, &$returnValue = 0) + public static function exec(string $command, ?array &$output = null, int &$returnValue = 0): string { return exec($command, $output, $returnValue); } @@ -94,7 +92,7 @@ class CommandUtility * @param string $path Override the default path (e.g. used by the install tool) * @return string Compiled command that deals with ImageMagick & GraphicsMagick */ - public static function imageMagickCommand($command, $parameters, $path = '') + public static function imageMagickCommand(string $command, string $parameters, string $path = ''): string { $gfxConf = $GLOBALS['TYPO3_CONF_VARS']['GFX']; $isExt = Environment::isWindows() ? '.exe' : ''; @@ -121,8 +119,8 @@ class CommandUtility // Determine whether the strip profile action has be disabled by TypoScript: if ($gfxConf['processor_stripColorProfileByDefault'] && $gfxConf['processor_stripColorProfileCommand'] !== '' - && !str_contains($parameters, $gfxConf['processor_stripColorProfileCommand']) && $parameters !== '-version' + && !str_contains($parameters, $gfxConf['processor_stripColorProfileCommand']) && !str_contains($parameters, '###SkipStripProfile###') ) { $parameters = $gfxConf['processor_stripColorProfileCommand'] . ' ' . $parameters; @@ -157,16 +155,16 @@ class CommandUtility * Checks if a command is valid or not, updates global variables * * @param string $cmd The command that should be executed. eg: "convert" - * @param string $handler Executer for the command. eg: "perl" - * @return bool FALSE if cmd is not found, or -1 if the handler is not found + * @param string $handler Executor for the command. eg: "perl" + * @return bool|int True if the command is valid; False if cmd is not found; -1 if the handler is not found */ - public static function checkCommand($cmd, $handler = '') + public static function checkCommand(string $cmd, string $handler = ''): bool|int { if (!self::init()) { return false; } - if ($handler && !self::checkCommand($handler)) { + if ($handler !== '' && !self::checkCommand($handler)) { return -1; } // Already checked and valid @@ -230,9 +228,9 @@ class CommandUtility * @param string $cmd The command that should be executed. eg: "convert" * @param string $handler Handler (executor) for the command. eg: "perl" * @param string $handlerOpt Options for the handler, like '-w' for "perl" - * @return mixed Returns command string, or FALSE if cmd is not found, or -1 if the handler is not found + * @return string|bool|int Returns command string, or FALSE if cmd is not found, or -1 if the handler is not found */ - public static function getCommand($cmd, $handler = '', $handlerOpt = '') + public static function getCommand(string $cmd, string $handler = '', string $handlerOpt = ''): string|bool|int { if (!self::init()) { return false; @@ -262,7 +260,7 @@ class CommandUtility * * @param string $paths Comma separated list of extra paths where a command should be searched. Relative paths (without leading "/") are prepend with public web path */ - public static function addPaths($paths) + public static function addPaths(string $paths): void { self::initPaths($paths); } @@ -271,32 +269,23 @@ class CommandUtility * Returns an array of search paths * * @param bool $addInvalid If set the array contains invalid path too. Then the key is the path and the value is empty - * @return array Array of search paths (empty if exec is disabled) + * @return array<string, string|false> Array of search paths (empty if exec is disabled) */ - public static function getPaths($addInvalid = false) + public static function getPaths(bool $addInvalid = false): array { if (!self::init()) { return []; } - $paths = self::$paths; - - if (!$addInvalid) { - foreach ($paths as $path => $validPath) { - if (!$validPath) { - unset($paths[$path]); - } - } - } - return $paths; + return $addInvalid + ? self::$paths + : array_filter(self::$paths); } /** * Initializes this class - * - * @return bool */ - protected static function init() + protected static function init(): bool { if ($GLOBALS['TYPO3_CONF_VARS']['BE']['disable_exec_function']) { return false; @@ -314,7 +303,7 @@ class CommandUtility * * @param string $paths Comma separated list of extra paths where a command should be searched. Relative paths (without leading "/") are prepend with public web path */ - protected static function initPaths($paths = '') + protected static function initPaths(string $paths = ''): void { $doCheck = false; @@ -326,18 +315,16 @@ class CommandUtility // Merge the submitted paths array to the global if ($paths) { $paths = GeneralUtility::trimExplode(',', $paths, true); - if (is_array($paths)) { - foreach ($paths as $path) { - // Make absolute path of relative - if (!preg_match('#^/#', $path)) { - $path = Environment::getPublicPath() . '/' . $path; - } - if (!isset(self::$paths[$path])) { - if (@is_dir($path)) { - self::$paths[$path] = $path; - } else { - self::$paths[$path] = false; - } + foreach ($paths as $path) { + // Make absolute path of relative + if (!str_starts_with($path, '/')) { + $path = Environment::getPublicPath() . '/' . $path; + } + if (!isset(self::$paths[$path])) { + if (@is_dir($path)) { + self::$paths[$path] = $path; + } else { + self::$paths[$path] = false; } } } @@ -356,9 +343,9 @@ class CommandUtility /** * Processes and returns the paths from $GLOBALS['TYPO3_CONF_VARS']['SYS']['binSetup'] * - * @return array Array of commands and path + * @return array<string, array{app: string, path: string, valid: bool}> Array of commands and path */ - protected static function getConfiguredApps() + protected static function getConfiguredApps(): array { $cmdArr = []; @@ -382,9 +369,9 @@ class CommandUtility /** * Sets the search paths from different sources, internal * - * @return array Array of absolute paths (keys and values are equal) + * @return array<string, string> Array of absolute paths (keys and values are equal) */ - protected static function getPathsInternal() + protected static function getPathsInternal(): array { $pathsArr = []; $sysPathArr = []; @@ -424,9 +411,7 @@ class CommandUtility ]); } - $pathsArr = array_merge($pathsArr, $sysPathArr); - - return $pathsArr; + return array_merge($pathsArr, $sysPathArr); } /** @@ -435,7 +420,7 @@ class CommandUtility * @param string $path Input path * @return string Output path */ - protected static function fixPath($path) + protected static function fixPath(string $path): string { return str_replace('//', '/', $path . '/'); } @@ -448,7 +433,7 @@ class CommandUtility * @param string[] $input Input arguments to be escaped * @return string[] Escaped shell arguments */ - public static function escapeShellArguments(array $input) + public static function escapeShellArguments(array $input): array { $isUTF8Filesystem = !empty($GLOBALS['TYPO3_CONF_VARS']['SYS']['UTF8filesystem']); $currentLocale = false; @@ -504,7 +489,7 @@ class CommandUtility * @param string $input Input-argument to be escaped * @return string Escaped shell argument */ - public static function escapeShellArgument($input) + public static function escapeShellArgument(string $input): string { return self::escapeShellArguments([$input])[0]; } diff --git a/typo3/sysext/core/Documentation/Changelog/12.0/Breaking-97210-TypesAddedToMethodSignatures.rst b/typo3/sysext/core/Documentation/Changelog/12.0/Breaking-97210-TypesAddedToMethodSignatures.rst index b300ba2e6ce96bdee5fe31d660fa20945c63a008..e58a0912ffb5745d3dffab9a4a97a19d6d7b2050 100644 --- a/typo3/sysext/core/Documentation/Changelog/12.0/Breaking-97210-TypesAddedToMethodSignatures.rst +++ b/typo3/sysext/core/Documentation/Changelog/12.0/Breaking-97210-TypesAddedToMethodSignatures.rst @@ -15,6 +15,7 @@ expectations and existing behavior. - :php:`\TYPO3\CMS\Core\Utility\ArrayUtility` - :php:`\TYPO3\CMS\Core\Utility\ClassNamingUtility` +- :php:`\TYPO3\CMS\Core\Utility\CommandUtility` - :php:`\TYPO3\CMS\Core\Utility\MathUtility` diff --git a/typo3/sysext/install/Classes/Configuration/Image/GraphicsMagickPreset.php b/typo3/sysext/install/Classes/Configuration/Image/GraphicsMagickPreset.php index 6c0b9b61817bad3052e84de7e2e03b1893363a35..909bff757a930cd1b96f574035467a096cfafdb4 100644 --- a/typo3/sysext/install/Classes/Configuration/Image/GraphicsMagickPreset.php +++ b/typo3/sysext/install/Classes/Configuration/Image/GraphicsMagickPreset.php @@ -76,7 +76,7 @@ class GraphicsMagickPreset extends AbstractImagePreset } if (@is_file($path . $executable)) { $command = escapeshellarg($path . $executable) . ' -version'; - $executingResult = false; + $executingResult = []; CommandUtility::exec($command, $executingResult); // First line of exec command should contain string GraphicsMagick $firstResultLine = array_shift($executingResult); diff --git a/typo3/sysext/install/Classes/Configuration/Image/ImageMagick6Preset.php b/typo3/sysext/install/Classes/Configuration/Image/ImageMagick6Preset.php index a3828fe252a7e50152ef8c7f21b955ee1aa81e8c..040be8cf5122781f3ff0f59f98cf85181fbea393 100644 --- a/typo3/sysext/install/Classes/Configuration/Image/ImageMagick6Preset.php +++ b/typo3/sysext/install/Classes/Configuration/Image/ImageMagick6Preset.php @@ -80,7 +80,7 @@ class ImageMagick6Preset extends AbstractImagePreset } if (@is_file($path . $executable)) { $command = escapeshellarg($path . $executable) . ' -version'; - $executingResult = false; + $executingResult = []; CommandUtility::exec($command, $executingResult); // First line of exec command should contain string GraphicsMagick $firstResultLine = array_shift($executingResult);