Merge pull request #19050 from kamil-tekiela/Errors

Refactor errors
This commit is contained in:
Maurício Meneghini Fauth 2024-03-13 19:39:22 -03:00 committed by GitHub
commit a28cd358e2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 91 additions and 104 deletions

View File

@ -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\\<min, \\-1\\>\\|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

View File

@ -399,10 +399,6 @@
<ReferenceReusedFromConfusingScope>
<code><![CDATA[$name]]></code>
</ReferenceReusedFromConfusingScope>
<UnevaluatedCode>
<code><![CDATA[return [];]]></code>
<code><![CDATA[return [];]]></code>
</UnevaluatedCode>
</file>
<file src="src/Config/FormDisplay.php">
<DeprecatedMethod>
@ -11269,9 +11265,6 @@
<PossiblyUnusedProperty>
<code><![CDATA[$filesizeInfo]]></code>
</PossiblyUnusedProperty>
<UnevaluatedCode>
<code><![CDATA[return false;]]></code>
</UnevaluatedCode>
</file>
<file src="src/Theme/ThemeManager.php">
<DeprecatedMethod>
@ -11306,9 +11299,6 @@
<RiskyTruthyFalsyComparison>
<code><![CDATA[$cookieTheme]]></code>
</RiskyTruthyFalsyComparison>
<UnevaluatedCode>
<code><![CDATA[return false;]]></code>
</UnevaluatedCode>
</file>
<file src="src/Tracking/Tracker.php">
<DeprecatedMethod>

View File

@ -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] === '#') {

View File

@ -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',

View File

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Exceptions;
use Exception;
class InvalidImagePath extends Exception
{
}

View File

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Exceptions;
use Exception;
class MismatchedSessionId extends Exception
{
}

View File

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Exceptions;
use Exception;
class MissingTheme extends Exception
{
}

View File

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Exceptions;
use RuntimeException;
class UndefinedOption extends RuntimeException
{
}

View File

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Exceptions;
use Exception;
class UnsupportedLanguageCode extends Exception
{
}

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace PhpMyAdmin\Http\Middleware;
use PhpMyAdmin\Exceptions\MismatchedSessionId;
use PhpMyAdmin\Sanitize;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
@ -14,9 +15,6 @@ use function __;
use function hash_equals;
use function is_scalar;
use function session_id;
use function trigger_error;
use const E_USER_ERROR;
/**
* Check whether user supplied token is valid, if not remove any possibly
@ -55,11 +53,10 @@ final class TokenRequestParamChecking implements MiddlewareInterface
// Warn in case the mismatch is result of failed setting of session cookie
if (isset($_POST['set_session']) && $_POST['set_session'] !== session_id()) {
trigger_error(
throw new MismatchedSessionId(
__(
'Failed to set session cookie. Maybe you are using HTTP instead of HTTPS to access phpMyAdmin.',
),
E_USER_ERROR,
);
}

View File

@ -4,6 +4,8 @@ declare(strict_types=1);
namespace PhpMyAdmin;
use PhpMyAdmin\Exceptions\UnsupportedLanguageCode;
use function __;
use function closedir;
use function count;
@ -14,12 +16,9 @@ use function opendir;
use function preg_grep;
use function readdir;
use function strtolower;
use function trigger_error;
use function uasort;
use function ucfirst;
use const E_USER_ERROR;
/**
* Language selection manager
*/
@ -957,9 +956,6 @@ class LanguageManager
return;
}
trigger_error(
__('Ignoring unsupported language code.'),
E_USER_ERROR,
);
throw new UnsupportedLanguageCode(__('Ignoring unsupported language code.'));
}
}

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace PhpMyAdmin\Theme;
use PhpMyAdmin\Exceptions\InvalidImagePath;
use PhpMyAdmin\Version;
use Webmozart\Assert\Assert;
use Webmozart\Assert\InvalidArgumentException;
@ -19,12 +20,10 @@ use function is_dir;
use function is_readable;
use function json_decode;
use function sprintf;
use function trigger_error;
use function trim;
use function version_compare;
use const DIRECTORY_SEPARATOR;
use const E_USER_ERROR;
/**
* handles theme
@ -176,16 +175,10 @@ class Theme
return true;
}
// we failed
trigger_error(
sprintf(
__('No valid image path for theme %s found!'),
$this->getName(),
),
E_USER_ERROR,
);
return false;
throw new InvalidImagePath(sprintf(
__('No valid image path for theme %s found!'),
$this->getName(),
));
}
/**

View File

@ -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/';
}
/**