Skip to content

Commit

Permalink
Fix some errors found by Psalm
Browse files Browse the repository at this point in the history
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Jul 6, 2021
1 parent c4f663e commit 8e4d622
Show file tree
Hide file tree
Showing 15 changed files with 39 additions and 70 deletions.
1 change: 1 addition & 0 deletions js/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PhpMyAdmin\Controllers\JavaScriptMessagesController;
use PhpMyAdmin\OutputBuffering;

/** @psalm-suppress InvalidGlobal */
global $containerBuilder;

if (! defined('ROOT_PATH')) {
Expand Down
1 change: 1 addition & 0 deletions libraries/classes/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ public function load(?string $source = null): bool

ob_start();
$isConfigLoading = true;
/** @psalm-suppress UnresolvableInclude */
$eval_result = include $this->getSource();
$isConfigLoading = false;
ob_end_clean();
Expand Down
19 changes: 16 additions & 3 deletions libraries/classes/Config/Forms/BaseFormList.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpMyAdmin\Config\ConfigFile;

use function array_merge;
use function class_exists;
use function in_array;

class BaseFormList
Expand Down Expand Up @@ -49,11 +50,15 @@ public static function isValid($name)
* @param string $name Name
*
* @return string|null
* @psalm-return class-string|null
*/
public static function get($name)
{
if (static::isValid($name)) {
return static::$ns . $name . 'Form';
/** @var class-string $class */
$class = static::$ns . $name . 'Form';

return $class;
}

return null;
Expand All @@ -66,7 +71,11 @@ public function __construct(ConfigFile $cf)
{
$this->forms = [];
foreach (static::$all as $form) {
$class = static::get($form);
$class = (string) static::get($form);
if (! class_exists($class)) {
continue;
}

$this->forms[] = new $class($cf);
}
}
Expand Down Expand Up @@ -141,7 +150,11 @@ public static function getFields()
{
$names = [];
foreach (static::$all as $form) {
$class = static::get($form);
$class = (string) static::get($form);
if (! class_exists($class)) {
continue;
}

$names = array_merge($names, $class::getFields());
}

Expand Down
1 change: 1 addition & 0 deletions libraries/classes/Controllers/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,7 @@ public function index(): void
$this->response->addJSON('message', Message::error($msg));
} else {
$active_page = $goto;
/** @psalm-suppress UnresolvableInclude */
include ROOT_PATH . $goto;
}

Expand Down
2 changes: 2 additions & 0 deletions libraries/classes/Controllers/Table/ReplaceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ public function index(): void
return;
}

/** @psalm-suppress UnresolvableInclude */
include ROOT_PATH . Core::securePath((string) $goto_include);

return;
Expand Down Expand Up @@ -656,6 +657,7 @@ public function index(): void
/**
* Load target page.
*/
/** @psalm-suppress UnresolvableInclude */
require ROOT_PATH . Core::securePath((string) $goto_include);
}
}
1 change: 1 addition & 0 deletions libraries/classes/Display/Results.php
Original file line number Diff line number Diff line change
Expand Up @@ -2837,6 +2837,7 @@ private function getRowValues(
&& (trim($row[$i]) != '')
&& ! $_SESSION['tmpval']['hide_transformation']
) {
/** @psalm-suppress UnresolvableInclude */
include_once ROOT_PATH . $this->transformationInfo[$dbLower][$tblLower][$nameLower][0];
$transformationPlugin = new $this->transformationInfo[$dbLower][$tblLower][$nameLower][1](null);

Expand Down
9 changes: 8 additions & 1 deletion libraries/classes/Navigation/NodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class NodeFactory
* @param string $class The class name to be sanitized
*
* @return string
* @psalm-return class-string
*/
private static function sanitizeClass($class)
{
Expand All @@ -57,12 +58,15 @@ private static function sanitizeClass($class)
* @param string $class The class name to check
*
* @return string
* @psalm-return class-string
*/
private static function checkClass($class)
{
/** @var class-string $class */
$class = sprintf(self::$namespace, $class);

if (! class_exists($class)) {
/** @var class-string $class */
$class = sprintf(self::$namespace, 'Node');
trigger_error(
sprintf(
Expand Down Expand Up @@ -92,7 +96,10 @@ public static function getInstance(
): Node {
$class = self::sanitizeClass($class);

return new $class($name, $type, $isGroup);
/** @var Node $node */
$node = new $class($name, $type, $isGroup);

return $node;
}

/**
Expand Down
1 change: 1 addition & 0 deletions libraries/classes/Plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ public static function getAuthPlugin(): AuthenticationPlugin
{
global $cfg;

/** @psalm-var class-string $class */
$class = 'PhpMyAdmin\\Plugins\\Auth\\Authentication' . ucfirst(strtolower($cfg['Server']['auth_type']));

if (! class_exists($class)) {
Expand Down
2 changes: 2 additions & 0 deletions libraries/classes/TwoFactor.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,13 @@ public function getMissingDeps()
* @param string $name Backend name
*
* @return string
* @psalm-return class-string
*/
public function getBackendClass($name)
{
$result = TwoFactorPlugin::class;
if (in_array($name, $this->available)) {
/** @psalm-var class-string $result */
$result = 'PhpMyAdmin\\Plugins\\TwoFactor\\' . ucfirst($name);
} elseif (! empty($name)) {
$result = Invalid::class;
Expand Down
7 changes: 4 additions & 3 deletions libraries/common.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@
use PhpMyAdmin\ThemeManager;
use PhpMyAdmin\Tracker;

global $containerBuilder, $errorHandler, $config, $server, $dbi, $request;
global $lang, $cfg, $isConfigLoading, $auth_plugin, $route, $theme;
global $urlParams, $goto, $back, $db, $table, $sql_query, $token_mismatch;
/** @psalm-suppress InvalidGlobal */
global $containerBuilder, $errorHandler, $config, $server, $dbi, $request,
$lang, $cfg, $isConfigLoading, $auth_plugin, $route, $theme,
$urlParams, $goto, $back, $db, $table, $sql_query, $token_mismatch;

/**
* block attempts to directly run this script
Expand Down
1 change: 1 addition & 0 deletions libraries/config.default.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* an error message if phpMyAdmin cannot auto-detect the correct value.
*
* @global string $cfg['PmaAbsoluteUri']
* @psalm-suppress PossiblyUndefinedGlobalVariable
*/
$cfg['PmaAbsoluteUri'] = '';

Expand Down
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,6 @@ parameters:
count: 1
path: libraries/classes/Config/FormDisplay.php

-
message: "#^Cannot call static method getFields\\(\\) on string\\|null\\.$#"
count: 1
path: libraries/classes/Config/Forms/BaseFormList.php

-
message: "#^Argument of an invalid type array\\|object supplied for foreach, only iterables are supported\\.$#"
count: 1
Expand Down
24 changes: 0 additions & 24 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.8.1@f73f2299dbc59a3e6c4d66cff4605176e728ee69">
<file src="js/messages.php">
<InvalidGlobal occurrences="1">
<code>global $containerBuilder;</code>
</InvalidGlobal>
</file>
<file src="libraries/classes/Bookmark.php">
<RedundantCastGivenDocblockType occurrences="1">
<code>(int) $this-&gt;id</code>
Expand Down Expand Up @@ -187,12 +182,6 @@
<code>$values[$path]</code>
</PossiblyInvalidIterator>
</file>
<file src="libraries/classes/Config/Forms/BaseFormList.php">
<UndefinedClass occurrences="2">
<code>$class::getFields()</code>
<code>new $class($cf)</code>
</UndefinedClass>
</file>
<file src="libraries/classes/Config/PageSettings.php">
<ArgumentTypeCoercion occurrences="2">
<code>$formDisplay</code>
Expand Down Expand Up @@ -1067,14 +1056,6 @@
<code>(string) $child-&gt;name</code>
</RedundantCastGivenDocblockType>
</file>
<file src="libraries/classes/Navigation/NodeFactory.php">
<LessSpecificReturnStatement occurrences="1">
<code>new $class($name, $type, $isGroup)</code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType occurrences="1">
<code>Node</code>
</MoreSpecificReturnType>
</file>
<file src="libraries/classes/Navigation/Nodes/Node.php">
<RedundantCastGivenDocblockType occurrences="4">
<code>(bool) $isGroup</code>
Expand Down Expand Up @@ -2321,11 +2302,6 @@
<code>stdClass|null</code>
</MoreSpecificReturnType>
</file>
<file src="setup/index.php">
<InvalidGlobal occurrences="1">
<code>global $cfg;</code>
</InvalidGlobal>
</file>
<file src="setup/validate.php">
<PossiblyNullArgument occurrences="2">
<code>$ids</code>
Expand Down
34 changes: 0 additions & 34 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,5 @@
<PropertyNotSetInConstructor errorLevel="info"/>
<RedundantCondition errorLevel="info"/>
<DocblockTypeContradiction errorLevel="info"/>

<UnresolvableInclude>
<errorLevel type="suppress">
<file name="libraries/classes/Config.php"/>
<file name="libraries/classes/Controllers/ImportController.php"/>
<file name="libraries/classes/Controllers/Table/ReplaceController.php"/>
<file name="libraries/classes/Display/Results.php"/>
<file name="libraries/classes/Plugins.php"/>
</errorLevel>
</UnresolvableInclude>

<PossiblyUndefinedGlobalVariable>
<errorLevel type="suppress">
<file name="libraries/config.default.php"/>
</errorLevel>
</PossiblyUndefinedGlobalVariable>

<InvalidGlobal>
<errorLevel type="suppress">
<file name="libraries/common.inc.php"/>
</errorLevel>
</InvalidGlobal>

<InvalidStringClass>
<errorLevel type="suppress">
<file name="libraries/classes/Config/Forms/BaseFormList.php"/>
<file name="libraries/classes/Config/PageSettings.php"/>
<file name="libraries/classes/Controllers/Setup/FormController.php"/>
<file name="libraries/classes/Navigation/NodeFactory.php"/>
<file name="libraries/classes/Plugins.php"/>
<file name="libraries/classes/TwoFactor.php"/>
<file name="libraries/common.inc.php"/>
</errorLevel>
</InvalidStringClass>
</issueHandlers>
</psalm>
1 change: 1 addition & 0 deletions setup/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// phpcs:enable
}

/** @psalm-suppress InvalidGlobal */
global $cfg;

require ROOT_PATH . 'setup/lib/common.inc.php';
Expand Down

0 comments on commit 8e4d622

Please sign in to comment.