From a47c9a7cf3693984d5decf6c05fc0dd84ea13d6b Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Wed, 13 Mar 2024 16:03:45 +0100 Subject: [PATCH 1/3] Throw exceptions instead of errors Signed-off-by: Kamil Tekiela --- phpstan-baseline.neon | 20 ------------- psalm-baseline.xml | 10 ------- src/Config/Form.php | 14 ++++----- src/Error/Error.php | 4 +-- src/Exceptions/InvalidImagePath.php | 11 +++++++ src/Exceptions/MismatchedSessionId.php | 11 +++++++ src/Exceptions/MissingTheme.php | 11 +++++++ src/Exceptions/NotAList.php | 11 +++++++ src/Exceptions/UndefinedOption.php | 11 +++++++ src/Exceptions/UnsupportedLanguageCode.php | 11 +++++++ .../Middleware/TokenRequestParamChecking.php | 7 ++--- src/LanguageManager.php | 10 ++----- src/Theme/Theme.php | 17 ++++------- src/Theme/ThemeManager.php | 30 +++++++------------ 14 files changed, 94 insertions(+), 84 deletions(-) create mode 100644 src/Exceptions/InvalidImagePath.php create mode 100644 src/Exceptions/MismatchedSessionId.php create mode 100644 src/Exceptions/MissingTheme.php create mode 100644 src/Exceptions/NotAList.php create mode 100644 src/Exceptions/UndefinedOption.php create mode 100644 src/Exceptions/UnsupportedLanguageCode.php diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index a0628e234e..f8d04b9b81 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 @@ -7360,11 +7355,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 @@ -14405,11 +14395,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 @@ -14435,11 +14420,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 558397b2c4..35f00134a9 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -399,10 +399,6 @@ - - - - @@ -11295,9 +11291,6 @@ - - - @@ -11332,9 +11325,6 @@ - - - diff --git a/src/Config/Form.php b/src/Config/Form.php index 567e6322f9..972924e401 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\NotAList; +use PhpMyAdmin\Exceptions\UndefinedOption; + 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,15 +110,11 @@ 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 []; + throw new NotAList($optionPath . ' - not a static value list'); } // convert array('#', 'a', 'b') to array('a', 'b') 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..9b9a78c349 100644 --- a/src/Theme/ThemeManager.php +++ b/src/Theme/ThemeManager.php @@ -6,6 +6,7 @@ namespace PhpMyAdmin\Theme; use PhpMyAdmin\Config; use PhpMyAdmin\Current; +use PhpMyAdmin\Exceptions\MissingTheme; use function __; use function array_key_exists; @@ -20,7 +21,6 @@ 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 +70,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 +113,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; From 7bbe29327182a0343001f889588a51043546a20c Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Wed, 13 Mar 2024 17:08:58 +0100 Subject: [PATCH 2/3] Refactor to remove double warning on top of error Signed-off-by: Kamil Tekiela --- src/Theme/ThemeManager.php | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/src/Theme/ThemeManager.php b/src/Theme/ThemeManager.php index 9b9a78c349..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_WARNING; use const ROOT_PATH; /** @@ -208,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; } @@ -233,7 +224,6 @@ class ThemeManager $this->themes[$dir] = $newTheme; } - closedir($dirHandle); ksort($this->themes); } @@ -270,7 +260,7 @@ class ThemeManager */ public static function getThemesFsDir(): string { - return ROOT_PATH . 'public/themes' . DIRECTORY_SEPARATOR; + return ROOT_PATH . 'public/themes/'; } /** From 2f7161fff99c92f7c2986abaa728774116425462 Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Wed, 13 Mar 2024 17:15:28 +0100 Subject: [PATCH 3/3] Replace NotAList with assert Signed-off-by: Kamil Tekiela --- src/Config/Form.php | 6 ++---- src/Exceptions/NotAList.php | 11 ----------- 2 files changed, 2 insertions(+), 15 deletions(-) delete mode 100644 src/Exceptions/NotAList.php diff --git a/src/Config/Form.php b/src/Config/Form.php index 972924e401..f306d4fd27 100644 --- a/src/Config/Form.php +++ b/src/Config/Form.php @@ -7,8 +7,8 @@ declare(strict_types=1); namespace PhpMyAdmin\Config; -use PhpMyAdmin\Exceptions\NotAList; use PhpMyAdmin\Exceptions\UndefinedOption; +use Webmozart\Assert\Assert; use function array_combine; use function array_shift; @@ -113,9 +113,7 @@ class Form throw new UndefinedOption($optionPath . ' - select options not defined'); } - if (! is_array($value)) { - throw new NotAList($optionPath . ' - not a static value list'); - } + 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/Exceptions/NotAList.php b/src/Exceptions/NotAList.php deleted file mode 100644 index 11562c179e..0000000000 --- a/src/Exceptions/NotAList.php +++ /dev/null @@ -1,11 +0,0 @@ -