diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index db1e5cddf1..004f9da8ba 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -585,11 +585,6 @@ parameters: count: 1 path: src/Config/Form.php - - - message: "#^Unreachable statement \\- code above always terminates\\.$#" - count: 2 - path: src/Config/Form.php - - message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#" count: 2 @@ -7340,11 +7335,6 @@ parameters: count: 1 path: src/Error/Error.php - - - message: "#^Match expression does not handle remaining values\\: 3\\|int\\\\|int\\<5, 7\\>\\|int\\<9, 15\\>\\|int\\<17, 31\\>\\|int\\<33, 63\\>\\|int\\<65, 127\\>\\|int\\<129, 255\\>\\|int\\<257, 511\\>\\|int\\<513, 1023\\>\\|int\\<1025, 2047\\>\\|int\\<2049, 4095\\>\\|int\\<4097, 8191\\>\\|int\\<8193, 16383\\>\\|int\\<16385, max\\>$#" - count: 2 - path: src/Error/Error.php - - message: "#^Only booleans are allowed in \\|\\|, int\\<0, 18176\\> given on the right side\\.$#" count: 1 @@ -14385,11 +14375,6 @@ parameters: count: 1 path: src/Theme/Theme.php - - - message: "#^Unreachable statement \\- code above always terminates\\.$#" - count: 1 - path: src/Theme/Theme.php - - message: "#^Left side of && is always true\\.$#" count: 1 @@ -14415,11 +14400,6 @@ parameters: count: 1 path: src/Theme/ThemeManager.php - - - message: "#^Unreachable statement \\- code above always terminates\\.$#" - count: 1 - path: src/Theme/ThemeManager.php - - message: "#^Cannot access offset string on mixed\\.$#" count: 2 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 88c85faad8..f82ca3d10d 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -399,10 +399,6 @@ - - - - @@ -11269,9 +11265,6 @@ - - - @@ -11306,9 +11299,6 @@ - - - diff --git a/src/Config/Form.php b/src/Config/Form.php index 567e6322f9..f306d4fd27 100644 --- a/src/Config/Form.php +++ b/src/Config/Form.php @@ -7,6 +7,9 @@ declare(strict_types=1); namespace PhpMyAdmin\Config; +use PhpMyAdmin\Exceptions\UndefinedOption; +use Webmozart\Assert\Assert; + use function array_combine; use function array_shift; use function array_walk; @@ -21,9 +24,6 @@ use function mb_strrpos; use function mb_substr; use function str_replace; use function str_starts_with; -use function trigger_error; - -use const E_USER_ERROR; /** * Base class for forms, loads default configuration options, checks allowed @@ -110,16 +110,10 @@ class Form { $value = $this->configFile->getDbEntry($optionPath); if ($value === null) { - trigger_error($optionPath . ' - select options not defined', E_USER_ERROR); - - return []; + throw new UndefinedOption($optionPath . ' - select options not defined'); } - if (! is_array($value)) { - trigger_error($optionPath . ' - not a static value list', E_USER_ERROR); - - return []; - } + Assert::isArray($value, $optionPath . ' - not a static value list'); // convert array('#', 'a', 'b') to array('a', 'b') if (isset($value[0]) && $value[0] === '#') { diff --git a/src/Error/Error.php b/src/Error/Error.php index 251a92c0f3..c6b40536e0 100644 --- a/src/Error/Error.php +++ b/src/Error/Error.php @@ -258,7 +258,7 @@ class Error extends Message public function getType(): string { return match ($this->errorNumber) { - 0 => 'Internal error', + default => 'Internal error', E_ERROR => 'Error', E_WARNING => 'Warning', E_PARSE => 'Parsing Error', @@ -285,7 +285,7 @@ class Error extends Message public function getLevel(): string { return match ($this->errorNumber) { - 0 => 'error', + default => 'error', E_ERROR => 'error', E_WARNING => 'error', E_PARSE => 'error', diff --git a/src/Exceptions/InvalidImagePath.php b/src/Exceptions/InvalidImagePath.php new file mode 100644 index 0000000000..d725173ed0 --- /dev/null +++ b/src/Exceptions/InvalidImagePath.php @@ -0,0 +1,11 @@ +getName(), - ), - E_USER_ERROR, - ); - - return false; + throw new InvalidImagePath(sprintf( + __('No valid image path for theme %s found!'), + $this->getName(), + )); } /** diff --git a/src/Theme/ThemeManager.php b/src/Theme/ThemeManager.php index 6d4dc4c02a..8cc9fec3f1 100644 --- a/src/Theme/ThemeManager.php +++ b/src/Theme/ThemeManager.php @@ -4,24 +4,18 @@ declare(strict_types=1); namespace PhpMyAdmin\Theme; +use DirectoryIterator; use PhpMyAdmin\Config; use PhpMyAdmin\Current; +use PhpMyAdmin\Exceptions\MissingTheme; use function __; use function array_key_exists; -use function closedir; use function htmlspecialchars; -use function is_dir; use function is_string; use function ksort; -use function opendir; -use function readdir; use function sprintf; -use function trigger_error; -use const DIRECTORY_SEPARATOR; -use const E_USER_ERROR; -use const E_USER_WARNING; use const ROOT_PATH; /** @@ -70,17 +64,14 @@ class ThemeManager $configThemeExists = $this->checkTheme($config->settings['ThemeDefault']); if (! $configThemeExists) { - trigger_error( - sprintf( - __('Default theme %s not found!'), - htmlspecialchars($config->settings['ThemeDefault']), - ), - E_USER_ERROR, - ); - } else { - $this->themeDefault = $config->settings['ThemeDefault']; + throw new MissingTheme(sprintf( + __('Default theme %s not found!'), + htmlspecialchars($config->settings['ThemeDefault']), + )); } + $this->themeDefault = $config->settings['ThemeDefault']; + // check if user have a theme cookie $cookieTheme = $this->getThemeCookie(); if ( @@ -116,15 +107,10 @@ class ThemeManager public function setActiveTheme(string|null $theme): bool { if (! $this->checkTheme($theme)) { - trigger_error( - sprintf( - __('Theme %s not found!'), - htmlspecialchars((string) $theme), - ), - E_USER_ERROR, - ); - - return false; + throw new MissingTheme(sprintf( + __('Theme %s not found!'), + htmlspecialchars((string) $theme), + )); } $this->activeTheme = $theme; @@ -216,24 +202,21 @@ class ThemeManager public function loadThemes(): void { $this->themes = []; - $dirHandle = opendir($this->themesPath); - if ($dirHandle === false) { - trigger_error('Error: cannot open themes folder: ./themes', E_USER_WARNING); + $directoryIterator = new DirectoryIterator($this->themesPath); - return; - } - - while (($dir = readdir($dirHandle)) !== false) { - if ($dir === '.' || $dir === '..' || ! @is_dir($this->themesPath . $dir)) { + foreach ($directoryIterator as $directoryInfo) { + if ($directoryInfo->isDot() || ! $directoryInfo->isDir()) { continue; } + $dir = $directoryInfo->getFilename(); + if (array_key_exists($dir, $this->themes)) { continue; } - $newTheme = Theme::load($this->themesPathUrl . $dir, $this->themesPath . $dir . DIRECTORY_SEPARATOR, $dir); + $newTheme = Theme::load($this->themesPathUrl . $dir, $this->themesPath . $dir . '/', $dir); if (! $newTheme instanceof Theme) { continue; } @@ -241,7 +224,6 @@ class ThemeManager $this->themes[$dir] = $newTheme; } - closedir($dirHandle); ksort($this->themes); } @@ -278,7 +260,7 @@ class ThemeManager */ public static function getThemesFsDir(): string { - return ROOT_PATH . 'public/themes' . DIRECTORY_SEPARATOR; + return ROOT_PATH . 'public/themes/'; } /**