From e75205edf8910c04ac2fd10ee9d9e7db0b949312 Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Fri, 3 Nov 2023 08:18:06 +0100 Subject: [PATCH] Add PHPStan checks (level 1) --- .github/workflows/continuous-integration.yml | 3 + ajax/dropdownAuthorization.php | 1 + composer.json | 10 +- composer.lock | 172 ++++++++++++++++++- front/application.form.php | 5 +- inc/application.class.php | 6 +- inc/authorization.class.php | 6 +- inc/mailcollectorfeature.class.php | 11 +- phpstan.neon | 19 ++ setup.php | 1 + 10 files changed, 215 insertions(+), 19 deletions(-) create mode 100644 phpstan.neon diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index b9611e6..2b8b949 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -60,6 +60,9 @@ jobs: - name: "PHP_CodeSniffer" run: | vendor/bin/phpcs -q --report=checkstyle | vendor/bin/cs2pr + - name: "PHPStan checks" + run: | + vendor/bin/phpstan analyze --ansi --memory-limit=1G --no-interaction --no-progress --error-format=checkstyle | vendor/bin/cs2pr - name: "Check for missing/outdated headers" run: | vendor/bin/licence-headers-check --ansi --no-interaction diff --git a/ajax/dropdownAuthorization.php b/ajax/dropdownAuthorization.php index 56cdbd9..c3a54bd 100644 --- a/ajax/dropdownAuthorization.php +++ b/ajax/dropdownAuthorization.php @@ -34,6 +34,7 @@ Session::checkLoginUser(); +/** @var \DBmysql $DB */ global $DB; $iterator = $DB->request( diff --git a/composer.json b/composer.json index 391cd96..55d4811 100644 --- a/composer.json +++ b/composer.json @@ -3,8 +3,11 @@ "php": ">=7.4" }, "require-dev": { - "glpi-project/tools": "^0.6", + "glpi-project/tools": "^0.7.1", "php-parallel-lint/php-parallel-lint": "^1.3", + "phpstan/extension-installer": "^1.3", + "phpstan/phpstan": "^1.10", + "phpstan/phpstan-deprecation-rules": "^1.1", "squizlabs/php_codesniffer": "^3.6", "staabm/annotate-pull-request-from-checkstyle": "^1.8" }, @@ -16,6 +19,9 @@ "platform": { "php": "7.4.0" }, - "sort-packages": true + "sort-packages": true, + "allow-plugins": { + "phpstan/extension-installer": true + } } } diff --git a/composer.lock b/composer.lock index b613ddb..308a87e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,27 +4,31 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "e7e4613a37d7d5fd1b71f81038c1bd60", + "content-hash": "7e13965c1eef693b508c95a190883724", "packages": [], "packages-dev": [ { "name": "glpi-project/tools", - "version": "0.6.4", + "version": "0.7.1", "source": { "type": "git", "url": "https://github.com/glpi-project/tools.git", - "reference": "8ef917fa2967e716eaed198bb803f418a80cd621" + "reference": "4bc5a725d9f4da0ee946ad3cbdd54a782d2f40fb" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/glpi-project/tools/zipball/8ef917fa2967e716eaed198bb803f418a80cd621", - "reference": "8ef917fa2967e716eaed198bb803f418a80cd621", + "url": "https://api.github.com/repos/glpi-project/tools/zipball/4bc5a725d9f4da0ee946ad3cbdd54a782d2f40fb", + "reference": "4bc5a725d9f4da0ee946ad3cbdd54a782d2f40fb", "shasum": "" }, "require": { "symfony/console": "^5.4 || ^6.0", "twig/twig": "^3.3" }, + "require-dev": { + "nikic/php-parser": "^4.13", + "phpstan/phpstan-src": "^1.10" + }, "bin": [ "bin/extract-locales", "bin/licence-headers-check", @@ -33,7 +37,7 @@ "type": "library", "autoload": { "psr-4": { - "Glpi\\": "src/" + "GlpiProject\\Tools\\": "src/" } }, "notification-url": "https://packagist.org/downloads/", @@ -57,7 +61,7 @@ "issues": "https://github.com/glpi-project/tools/issues", "source": "https://github.com/glpi-project/tools" }, - "time": "2023-07-27T12:32:25+00:00" + "time": "2023-10-16T11:40:35+00:00" }, { "name": "php-parallel-lint/php-parallel-lint", @@ -116,6 +120,160 @@ }, "time": "2022-02-21T12:50:22+00:00" }, + { + "name": "phpstan/extension-installer", + "version": "1.3.1", + "source": { + "type": "git", + "url": "https://github.com/phpstan/extension-installer.git", + "reference": "f45734bfb9984c6c56c4486b71230355f066a58a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/extension-installer/zipball/f45734bfb9984c6c56c4486b71230355f066a58a", + "reference": "f45734bfb9984c6c56c4486b71230355f066a58a", + "shasum": "" + }, + "require": { + "composer-plugin-api": "^2.0", + "php": "^7.2 || ^8.0", + "phpstan/phpstan": "^1.9.0" + }, + "require-dev": { + "composer/composer": "^2.0", + "php-parallel-lint/php-parallel-lint": "^1.2.0", + "phpstan/phpstan-strict-rules": "^0.11 || ^0.12 || ^1.0" + }, + "type": "composer-plugin", + "extra": { + "class": "PHPStan\\ExtensionInstaller\\Plugin" + }, + "autoload": { + "psr-4": { + "PHPStan\\ExtensionInstaller\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Composer plugin for automatic installation of PHPStan extensions", + "support": { + "issues": "https://github.com/phpstan/extension-installer/issues", + "source": "https://github.com/phpstan/extension-installer/tree/1.3.1" + }, + "time": "2023-05-24T08:59:17+00:00" + }, + { + "name": "phpstan/phpstan", + "version": "1.10.40", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan.git", + "reference": "93c84b5bf7669920d823631e39904d69b9c7dc5d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/93c84b5bf7669920d823631e39904d69b9c7dc5d", + "reference": "93c84b5bf7669920d823631e39904d69b9c7dc5d", + "shasum": "" + }, + "require": { + "php": "^7.2|^8.0" + }, + "conflict": { + "phpstan/phpstan-shim": "*" + }, + "bin": [ + "phpstan", + "phpstan.phar" + ], + "type": "library", + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPStan - PHP Static Analysis Tool", + "keywords": [ + "dev", + "static analysis" + ], + "support": { + "docs": "https://phpstan.org/user-guide/getting-started", + "forum": "https://github.com/phpstan/phpstan/discussions", + "issues": "https://github.com/phpstan/phpstan/issues", + "security": "https://github.com/phpstan/phpstan/security/policy", + "source": "https://github.com/phpstan/phpstan-src" + }, + "funding": [ + { + "url": "https://github.com/ondrejmirtes", + "type": "github" + }, + { + "url": "https://github.com/phpstan", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/phpstan/phpstan", + "type": "tidelift" + } + ], + "time": "2023-10-30T14:48:31+00:00" + }, + { + "name": "phpstan/phpstan-deprecation-rules", + "version": "1.1.4", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan-deprecation-rules.git", + "reference": "089d8a8258ed0aeefdc7b68b6c3d25572ebfdbaa" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan-deprecation-rules/zipball/089d8a8258ed0aeefdc7b68b6c3d25572ebfdbaa", + "reference": "089d8a8258ed0aeefdc7b68b6c3d25572ebfdbaa", + "shasum": "" + }, + "require": { + "php": "^7.2 || ^8.0", + "phpstan/phpstan": "^1.10.3" + }, + "require-dev": { + "php-parallel-lint/php-parallel-lint": "^1.2", + "phpstan/phpstan-php-parser": "^1.1", + "phpstan/phpstan-phpunit": "^1.0", + "phpunit/phpunit": "^9.5" + }, + "type": "phpstan-extension", + "extra": { + "phpstan": { + "includes": [ + "rules.neon" + ] + } + }, + "autoload": { + "psr-4": { + "PHPStan\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPStan rules for detecting usage of deprecated classes, methods, properties, constants and traits.", + "support": { + "issues": "https://github.com/phpstan/phpstan-deprecation-rules/issues", + "source": "https://github.com/phpstan/phpstan-deprecation-rules/tree/1.1.4" + }, + "time": "2023-08-05T09:02:04+00:00" + }, { "name": "psr/container", "version": "1.1.2", diff --git a/front/application.form.php b/front/application.form.php index a843f8b..6d6c558 100644 --- a/front/application.form.php +++ b/front/application.form.php @@ -30,6 +30,9 @@ include('../../../inc/includes.php'); +/** @var array $_UPOST */ +global $_UPOST; + $dropdown = new PluginOauthimapApplication(); if (isset($_POST['id']) && isset($_POST['request_authorization'])) { @@ -38,7 +41,7 @@ } else { Html::requireJs('clipboard'); - if (array_key_exists('client_secret', $_POST)) { + if (array_key_exists('client_secret', $_POST) && array_key_exists('client_secret', $_UPOST)) { // Client secret must not be altered. $_POST['client_secret'] = $_UPOST['client_secret']; } diff --git a/inc/application.class.php b/inc/application.class.php index fe2447a..93816ae 100644 --- a/inc/application.class.php +++ b/inc/application.class.php @@ -501,6 +501,7 @@ private static function getSupportedProviders(): array */ public function getProvider(): ?AbstractProvider { + /** @var array $CFG_GLPI */ global $CFG_GLPI; if (!$this->areCredentialsValid()) { @@ -619,7 +620,7 @@ public function cleanDBonPurge() */ public static function install(Migration $migration) { - + /** @var \DBmysql $DB */ global $DB; $default_charset = DBConnection::getDefaultCharset(); @@ -650,7 +651,8 @@ public static function install(Migration $migration) KEY `date_mod` (`date_mod`) ) ENGINE=InnoDB DEFAULT CHARSET={$default_charset} COLLATE={$default_collation} ROW_FORMAT=DYNAMIC; SQL; - $DB->query($query) or die($DB->error()); + $method = version_compare(GLPI_VERSION, '10.0.11', '>=') ? 'doQueryOrDie' : 'queryOrDie'; + $DB->$method($query); } // Add display preferences diff --git a/inc/authorization.class.php b/inc/authorization.class.php index cf9179c..252b381 100644 --- a/inc/authorization.class.php +++ b/inc/authorization.class.php @@ -79,6 +79,7 @@ public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $ return; } + /** @var \DBmysql $DB */ global $DB; $iterator = $DB->request( @@ -555,7 +556,7 @@ public function post_purgeItem() */ public static function install(Migration $migration) { - + /** @var \DBmysql $DB */ global $DB; $default_charset = DBConnection::getDefaultCharset(); @@ -585,7 +586,8 @@ public static function install(Migration $migration) UNIQUE KEY `unicity` (`$application_fkey`,`email`) ) ENGINE=InnoDB DEFAULT CHARSET={$default_charset} COLLATE={$default_collation} ROW_FORMAT=DYNAMIC; SQL; - $DB->query($query) or die($DB->error()); + $method = version_compare(GLPI_VERSION, '10.0.11', '>=') ? 'doQueryOrDie' : 'queryOrDie'; + $DB->$method($query); } else { if (!$DB->fieldExists($table, 'refresh_token')) { // V1.3.1: add new refresh_token field diff --git a/inc/mailcollectorfeature.class.php b/inc/mailcollectorfeature.class.php index ee84790..7542697 100644 --- a/inc/mailcollectorfeature.class.php +++ b/inc/mailcollectorfeature.class.php @@ -58,7 +58,6 @@ public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) } return CommonGLPI::createTabEntry(MailCollector::getTypeName(Session::getPluralNumber()), $count); - break; } return ''; @@ -68,7 +67,7 @@ public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $ { switch ($item->getType()) { case PluginOauthimapApplication::class: - MailCollectorFeature::showMailCollectorsForApplication($item, $tabnum); + MailCollectorFeature::showMailCollectorsForApplication($item); break; } @@ -297,9 +296,10 @@ public static function updateMailCollectorOnAuthorizationCallback( PluginOauthimapAuthorization $authorization, array $params = [] ): void { + $mailcollector = new MailCollector(); + $redirect = $mailcollector->getSearchURL(); if ($success) { // Store authorized email into MailCollector - $mailcollector = new MailCollector(); $mailcollector_id = $params[$mailcollector->getForeignKeyField()] ?? null; if ($mailcollector_id !== null && $mailcollector->getFromDB($mailcollector_id)) { $mailcollector->update( @@ -308,10 +308,11 @@ public static function updateMailCollectorOnAuthorizationCallback( 'login' => $authorization->fields['email'], ] ); + $redirect = $mailcollector->getLinkURL(); } } - Html::redirect($mailcollector->getLinkURL()); + Html::redirect($redirect); } /** @@ -412,7 +413,7 @@ private static function deactivateMailCollectors(string $protocol_type, ?string * @param string $login * @param bool $only_active * - * @return void + * @return array */ private static function getAssociatedMailCollectors( string $protocol_type, diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..8feb506 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,19 @@ +parameters: + parallel: + maximumNumberOfProcesses: 2 + level: 1 + bootstrapFiles: + - ../../inc/based_config.php + paths: + - inc + - front + - ajax + - hook.php + - setup.php + scanDirectories: + - ../../inc + - ../../src + stubFiles: + - ../../stubs/glpi_constants.php +rules: + - GlpiProject\Tools\PHPStan\Rules\GlobalVarTypeRule diff --git a/setup.php b/setup.php index d688739..5f1c27e 100644 --- a/setup.php +++ b/setup.php @@ -41,6 +41,7 @@ function plugin_init_oauthimap() { + /** @var array $PLUGIN_HOOKS */ global $PLUGIN_HOOKS; $PLUGIN_HOOKS['csrf_compliant']['oauthimap'] = true;