From 805ef67ca46fc14a6db37f5a42642822e4fe1f6f Mon Sep 17 00:00:00 2001 From: Curtis Conard Date: Sat, 13 Apr 2024 08:02:00 -0400 Subject: [PATCH] add phpstan --- .gitignore | 2 +- composer.json | 4 +- front/ruleimport.form.php | 2 +- front/ruleimport.php | 2 +- inc/config.class.php | 10 ++--- inc/connection.class.php | 5 +++ inc/devicesync.class.php | 2 +- inc/migration.class.php | 14 +----- inc/profile.class.php | 3 +- inc/sync.class.php | 2 +- inc/user_jssaccount.class.php | 3 +- phpstan.neon | 14 ++++++ setup.php | 59 ++++++++++++++------------ tests/units/PluginJamfComputerSync.php | 1 - tests/units/PluginJamfMobileSync.php | 1 - 15 files changed, 68 insertions(+), 56 deletions(-) create mode 100644 phpstan.neon diff --git a/.gitignore b/.gitignore index f0dcfe4..50b321e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ vendor composer.lock -tests/.phpunit.result.cache +.phpunit.result.cache diff --git a/composer.json b/composer.json index 3b6170a..77113af 100644 --- a/composer.json +++ b/composer.json @@ -4,6 +4,7 @@ }, "require-dev": { "glpi-project/tools": "^0.7.2", + "phpstan/phpstan": "^1.10", "phpunit/phpunit": "~9.6" }, "config": { @@ -17,6 +18,7 @@ } }, "scripts": { - "test": "phpunit -c phpunit.xml" + "test": "phpunit -c phpunit.xml", + "phpstan": "phpstan analyse -c phpstan.neon" } } diff --git a/front/ruleimport.form.php b/front/ruleimport.form.php index dac21a0..41fea6f 100644 --- a/front/ruleimport.form.php +++ b/front/ruleimport.form.php @@ -28,6 +28,6 @@ Html::displayNotFoundError(); } -$rulecollection = new PluginJamfRuleImportCollection($_SESSION['glpiactive_entity']); +$rulecollection = new PluginJamfRuleImportCollection(); include(GLPI_ROOT . "/front/rule.common.form.php"); diff --git a/front/ruleimport.php b/front/ruleimport.php index 594636e..937ddd9 100644 --- a/front/ruleimport.php +++ b/front/ruleimport.php @@ -28,6 +28,6 @@ Html::displayNotFoundError(); } -$rulecollection = new PluginJamfRuleImportCollection($_SESSION['glpiactive_entity']); +$rulecollection = new PluginJamfRuleImportCollection(); include(GLPI_ROOT . "/front/rule.common.php"); diff --git a/inc/config.class.php b/inc/config.class.php index 0130489..526ff17 100644 --- a/inc/config.class.php +++ b/inc/config.class.php @@ -33,7 +33,7 @@ class PluginJamfConfig extends CommonDBTM public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) { - if (!$withtemplate && $item->getType() === 'Config') { + if (!$withtemplate && $item::class === Config::class) { return _x('plugin_info', 'Jamf plugin', 'jamf'); } return ''; @@ -41,7 +41,6 @@ public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) public function showForm($ID = -1, array $options = []) { - global $CFG_GLPI; if (!Session::haveRight('config', UPDATE)) { return false; } @@ -51,14 +50,15 @@ public function showForm($ID = -1, array $options = []) 'config' => $config, 'url' => Toolbox::getItemTypeFormURL('Config'), ]); + return true; } public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) { - if ($item->getType() === 'Config') { - $config = new self(); - $config->showForm(); + if ($item::class === Config::class) { + return (new self())->showForm(); } + return false; } public static function undiscloseConfigValue($fields) diff --git a/inc/connection.class.php b/inc/connection.class.php index f73cf9c..5b3e030 100644 --- a/inc/connection.class.php +++ b/inc/connection.class.php @@ -198,6 +198,11 @@ private function fetchBearerToken() */ public function getClient() { + /** + * @var array $CFG_GLPI + */ + global $CFG_GLPI; + if (!isset($this->client)) { if ($this->bearer_token === null) { $this->fetchBearerToken(); diff --git a/inc/devicesync.class.php b/inc/devicesync.class.php index f1fecaf..d1144ae 100644 --- a/inc/devicesync.class.php +++ b/inc/devicesync.class.php @@ -178,7 +178,7 @@ public static function syncAll(): int $volume = 0; $config = PluginJamfConfig::getConfig(); - $valid_sync_interval = isset($config['sync_interval']) && !empty($config['sync_interval']) && ((int) $config['sync_interval'] >= 1); + $valid_sync_interval = isset($config['sync_interval']) && !empty($config['sync_interval']) && ((int)$config['sync_interval'] >= 1); if (!$valid_sync_interval) { $config['sync_interval'] = 8 * 60; } diff --git a/inc/migration.class.php b/inc/migration.class.php index 471c72a..4210ce9 100644 --- a/inc/migration.class.php +++ b/inc/migration.class.php @@ -59,7 +59,7 @@ public function __construct($version) public function applyMigrations() { $rc = new ReflectionClass($this); - $otherMigrationFunctions = array_map(static function ($rm) use ($rc) { + $otherMigrationFunctions = array_map(static function ($rm) { return $rm->getShortName(); }, array_filter($rc->getMethods(), static function ($m) { return preg_match('/(?<=^apply_)(.*)(?=_migration$)/', $m->getShortName()); @@ -473,18 +473,6 @@ public function apply_2_1_0_migration() ]); } - $old_jsspassword = Config::getConfigurationValues('plugin:Jamf', ['jsspassword'])['jsspassword'] ?? ''; - if (!empty($old_jsspassword)) { - $glpi_key = new GLPIKey(); - $this->db->update(Config::getTable(), [ - 'value' => $glpi_key->encrypt(Toolbox::decrypt($old_jsspassword)) - ], [ - 'context' => 'plugin:Jamf', - 'name' => 'jsspassword' - ]); - } - unset($old_jsspassword); - $this->glpiMigration->addRight(PluginJamfComputer::$rightname, ALLSTANDARDRIGHT); } diff --git a/inc/profile.class.php b/inc/profile.class.php index 70f4549..86b4de9 100644 --- a/inc/profile.class.php +++ b/inc/profile.class.php @@ -70,7 +70,8 @@ public function showForm($profiles_id = 0, $openform = true, $closeform = true) echo "
"; $profile = new Profile(); $profile->getFromDB($profiles_id); - if ($openform && ($canedit = Session::haveRightsOr(self::$rightname, [CREATE, UPDATE, PURGE]))) { + $canedit = Session::haveRightsOr(self::$rightname, [CREATE, UPDATE, PURGE]); + if ($openform && $canedit) { echo "
"; } diff --git a/inc/sync.class.php b/inc/sync.class.php index 7c5112c..01c21b5 100644 --- a/inc/sync.class.php +++ b/inc/sync.class.php @@ -115,7 +115,7 @@ abstract class PluginJamfSync * @param CommonDBTM|null $item * @param array $data */ - public function __construct(CommonDBTM $item = null, array $data = []) + final public function __construct(CommonDBTM $item = null, array $data = []) { /** @global DBmysql */ global $DB; diff --git a/inc/user_jssaccount.class.php b/inc/user_jssaccount.class.php index fb51c11..69cd717 100644 --- a/inc/user_jssaccount.class.php +++ b/inc/user_jssaccount.class.php @@ -46,10 +46,11 @@ public static function getTypeName($nb = 0) public function prepareInputForUpdate($input) { global $DB; - if ($input['jssaccounts_id'] == 0) { + if ($input['jssaccounts_id'] === 0) { $DB->delete(self::getTable(), ['id' => $this->fields['id']]); return false; } + return $input; } public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..bffe78f --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,14 @@ +parameters: + parallel: + maximumNumberOfProcesses: 2 + level: 1 + bootstrapFiles: + - ../../inc/includes.php + paths: + - ajax + - front + - inc + - setup.php + - hook.php + stubFiles: + - ../../stubs/glpi_constants.php diff --git a/setup.php b/setup.php index 0ca124a..f95d859 100644 --- a/setup.php +++ b/setup.php @@ -29,36 +29,39 @@ function plugin_init_jamf() global $PLUGIN_HOOKS; $PLUGIN_HOOKS['csrf_compliant']['jamf'] = true; - $PLUGIN_HOOKS['add_javascript']['jamf'][] = 'js/jamf.js'; - Plugin::registerClass('PluginJamfConfig', ['addtabon' => 'Config']); - $PLUGIN_HOOKS['post_item_form']['jamf'] = 'plugin_jamf_showJamfInfoForItem'; - $PLUGIN_HOOKS['pre_item_update']['jamf']['Phone'] = ['PluginJamfMobileDevice', 'preUpdatePhone']; - $PLUGIN_HOOKS['undiscloseConfigValue']['jamf'] = [PluginJamfConfig::class, 'undiscloseConfigValue']; - Plugin::registerClass('PluginJamfRuleImportCollection', ['rulecollections_types' => true]); - Plugin::registerClass('PluginJamfProfile', ['addtabon' => ['Profile']]); - Plugin::registerClass('PluginJamfItem_ExtensionAttribute', ['addtabon' => [ - 'Computer', - 'Phone' - ]]); - Plugin::registerClass('PluginJamfItem_MDMCommand', ['addtabon' => [ - 'Computer', - 'Phone' - ]]); - Plugin::registerClass('PluginJamfUser_JSSAccount', ['addtabon' => ['User']]); - if (Session::haveRight('plugin_jamf_mobiledevice', READ)) { - $PLUGIN_HOOKS['menu_toadd']['jamf'] = ['tools' => 'PluginJamfMenu']; - } - $PLUGIN_HOOKS['post_init']['jamf'] = 'plugin_jamf_postinit'; - $PLUGIN_HOOKS['item_purge']['jamf'] = [ - 'Computer' => ['PluginJamfAbstractDevice', 'plugin_jamf_purgeComputer'], - 'Phone' => ['PluginJamfAbstractDevice', 'plugin_jamf_purgePhone'], - 'Software' => ['PluginJamfSoftware', 'plugin_jamf_purgeSoftware'] - ]; - // Dashboards - $PLUGIN_HOOKS['dashboard_cards']['jamf'] = 'plugin_jamf_dashboardCards'; + if (Plugin::isPluginActive('jamf')) { + $PLUGIN_HOOKS['add_javascript']['jamf'][] = 'js/jamf.js'; + Plugin::registerClass('PluginJamfConfig', ['addtabon' => 'Config']); + $PLUGIN_HOOKS['post_item_form']['jamf'] = 'plugin_jamf_showJamfInfoForItem'; + $PLUGIN_HOOKS['pre_item_update']['jamf']['Phone'] = ['PluginJamfMobileDevice', 'preUpdatePhone']; + $PLUGIN_HOOKS['undiscloseConfigValue']['jamf'] = [PluginJamfConfig::class, 'undiscloseConfigValue']; + Plugin::registerClass('PluginJamfRuleImportCollection', ['rulecollections_types' => true]); + Plugin::registerClass('PluginJamfProfile', ['addtabon' => ['Profile']]); + Plugin::registerClass('PluginJamfItem_ExtensionAttribute', ['addtabon' => [ + 'Computer', + 'Phone' + ]]); + Plugin::registerClass('PluginJamfItem_MDMCommand', ['addtabon' => [ + 'Computer', + 'Phone' + ]]); + Plugin::registerClass('PluginJamfUser_JSSAccount', ['addtabon' => ['User']]); + if (Session::haveRight('plugin_jamf_mobiledevice', READ)) { + $PLUGIN_HOOKS['menu_toadd']['jamf'] = ['tools' => 'PluginJamfMenu']; + } + $PLUGIN_HOOKS['post_init']['jamf'] = 'plugin_jamf_postinit'; + $PLUGIN_HOOKS['item_purge']['jamf'] = [ + 'Computer' => ['PluginJamfAbstractDevice', 'plugin_jamf_purgeComputer'], + 'Phone' => ['PluginJamfAbstractDevice', 'plugin_jamf_purgePhone'], + 'Software' => ['PluginJamfSoftware', 'plugin_jamf_purgeSoftware'] + ]; - $PLUGIN_HOOKS['secured_configs']['Jamf'] = ['jsspassword']; + // Dashboards + $PLUGIN_HOOKS['dashboard_cards']['jamf'] = 'plugin_jamf_dashboardCards'; + + $PLUGIN_HOOKS['secured_configs']['Jamf'] = ['jsspassword']; + } } function plugin_version_jamf() diff --git a/tests/units/PluginJamfComputerSync.php b/tests/units/PluginJamfComputerSync.php index bccfbfd..a17c1a5 100644 --- a/tests/units/PluginJamfComputerSync.php +++ b/tests/units/PluginJamfComputerSync.php @@ -23,7 +23,6 @@ namespace tests\units; -use DbTestCase; use PluginJamfComputer; use PluginJamfComputerTestSync; use PluginJamfExtensionAttribute; diff --git a/tests/units/PluginJamfMobileSync.php b/tests/units/PluginJamfMobileSync.php index 6364a8f..24ca8c6 100644 --- a/tests/units/PluginJamfMobileSync.php +++ b/tests/units/PluginJamfMobileSync.php @@ -23,7 +23,6 @@ namespace tests\units; -use DbTestCase; use Phone; use PluginJamfAbstractDevice; use PluginJamfExtensionAttribute;