From eb02e405965f9f2da917ae24c5221c8c138ccde5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20Schl=C3=A4pfer?= Date: Sat, 30 Sep 2023 14:56:58 +0200 Subject: [PATCH] Added local phpstan config file, started rework --- composer.json | 3 +- phpstan.dist.neon | 15 +++++++ src/Api/Admin.php | 67 ++++++++++++++-------------- src/Controller/Admin.php | 93 ++++++++++++++++++++++++++++++--------- src/Controller/Client.php | 49 ++++++++++++++++++--- src/Service.php | 32 +++++++++++--- src/manifest.json | 2 +- 7 files changed, 190 insertions(+), 71 deletions(-) create mode 100644 phpstan.dist.neon diff --git a/composer.json b/composer.json index afe7054..387403b 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,8 @@ "type": "project", "require": { "php": "^8.0|^8.2", - "pve2-api-client/pve2-api-client": "^1.0.2" + "pve2-api-client/pve2-api-client": "^1.0.2", + "symfony/finder": "^6.3" }, "funding": [ { diff --git a/phpstan.dist.neon b/phpstan.dist.neon new file mode 100644 index 0000000..e1d950f --- /dev/null +++ b/phpstan.dist.neon @@ -0,0 +1,15 @@ +parameters: + level: 1 + bootstrapFiles: + - ../../load.php + paths: + - ./src + excludePaths: + analyse: + - ./src/vendor + scanDirectories: + - ../../ + - ./src/vendor + ignoreErrors: + - '#^Function __trans not found\.$#' + - '#^Variable \$proxmox might not be defined\.#' \ No newline at end of file diff --git a/src/Api/Admin.php b/src/Api/Admin.php index 8a0fa96..e94ccfc 100644 --- a/src/Api/Admin.php +++ b/src/Api/Admin.php @@ -29,9 +29,9 @@ class Admin extends \Api_Abstract /** * Get list of servers * - * @return array + * @return array */ - public function server_get_list($data) + public function server_get_list() { // Retrieve all servers from the database $servers = $this->di['db']->find('service_proxmox_server'); @@ -98,11 +98,11 @@ public function server_get_list($data) } /** - * Get list of storage + * Get list of storages * - * @return array + * @return array */ - public function storage_get_list($data) + public function storage_get_list() { $storages = $this->di['db']->find('service_proxmox_storage'); $storages_grouped = array(); @@ -186,8 +186,7 @@ public function storage_get_list($data) * * @return array */ - - public function storageclass_get_list($data) + public function storageclass_get_list() { $storageclasses = $this->di['db']->find('service_proxmox_storageclass'); return $storageclasses; @@ -198,7 +197,7 @@ public function storageclass_get_list($data) * * @return array */ - public function storage_controller_get_list($data) + public function storage_controller_get_list() { // Return Array of storage controllers: // lsi | lsi53c810 | virtio-scsi-pci | virtio-scsi-single | megasas | pvscsi @@ -218,9 +217,9 @@ public function storage_controller_get_list($data) /** * * Get list of Active Services * - * @return array + * @return array */ - public function service_proxmox_get_list($data) + public function service_proxmox_get_list() { $services = $this->di['db']->find('service_proxmox'); return $services; @@ -229,7 +228,7 @@ public function service_proxmox_get_list($data) /** * Create a new storageclass * - * @return array + * @return array */ public function storageclass_create($data) { @@ -242,7 +241,7 @@ public function storageclass_create($data) /** * Retrieve a single storageclass * - * @return array + * @return array */ public function storageclass_get($data) { @@ -296,7 +295,7 @@ public function qemu_templates_on_server($data) /** * Get list of OS types * - * @return array + * @return array */ public function os_get_list() @@ -322,7 +321,7 @@ public function os_get_list() /** * Get list of BIOS types * - * @return array + * @return array */ public function bios_get_list() { @@ -336,7 +335,7 @@ public function bios_get_list() /** * Get list of VNC types * - * @return array + * @return array */ public function lxc_appliance_get_list() { @@ -457,7 +456,7 @@ public function server_create($data) * Get server details * * @param int $id - server id - * @return array + * @return array * * @throws \Box_Exception */ @@ -610,7 +609,7 @@ public function server_get_from_order($data) * Receive Hardware Data from proxmox server * * @param int $server_id - * @return array + * @return array */ public function get_hardware_data($server_id) { @@ -765,7 +764,7 @@ public function test_access($data) /** * Get all available templates from any proxmox server * - * @return array + * @return array * @throws \Box_Exception */ public function pull_lxc_appliances() @@ -812,7 +811,7 @@ public function pull_lxc_appliances() * * @param int $id - storage id * - * @return array + * @return array * @throws \Box_Exception */ public function storage_get($data) @@ -834,7 +833,7 @@ public function storage_get($data) 'free' => $storage->free, 'percent_used' => ($storage->size == 0 || $storage->used == 0 || $storage->free == 0) ? 0 : round($storage->used / $storage->size * 100, 2), // add list of storage classes - 'storageclasses' => $this->storageclass_get_list($data), + 'storageclasses' => $this->storageclass_get_list(), 'server_name' => $server->name, ); @@ -847,7 +846,7 @@ public function storage_get($data) * TODO: Implement & Fix functionality * @param int $id - server id * - * @return array + * @return array * @throws \Box_Exception */ public function storage_update($data) @@ -956,7 +955,7 @@ public function product_update($data) /** * Get list of vm templates * - * @return array + * @return array */ public function service_get_vmtemplates() { @@ -967,7 +966,7 @@ public function service_get_vmtemplates() /** * Get list of qemu templates * - * @return array + * @return array */ public function service_get_qemutemplates() { @@ -978,7 +977,7 @@ public function service_get_qemutemplates() /** * Get list of lxc templates * - * @return array + * @return array */ public function service_get_lxctemplates() { @@ -989,7 +988,7 @@ public function service_get_lxctemplates() /** * Get list of ip ranges * - * @return array + * @return array */ public function service_get_ip_ranges() { @@ -1010,7 +1009,7 @@ public function service_get_ip_adresses() /** * Get list of vlans * - * @return array + * @return array */ public function service_get_vlans() { @@ -1021,7 +1020,7 @@ public function service_get_vlans() /** * Get list of tags by type * - * @return array + * @return array */ public function service_get_tags($data) { @@ -1038,7 +1037,7 @@ public function service_add_tag($data) /** * Get list of tags by storage id * - * @return array + * @return array */ public function service_get_tags_by_storage($data) { @@ -1050,7 +1049,7 @@ public function service_get_tags_by_storage($data) /** * Get a vm configuration templates * - * @return array + * @return array */ public function vm_config_template_get($data) { @@ -1081,7 +1080,7 @@ public function vm_config_template_get($data) /** * Function to get storages for vm config template * - * @return array + * @return array */ public function vm_config_template_get_storages($data) @@ -1101,7 +1100,7 @@ public function vm_config_template_get_storages($data) /** * Get list of lxc configuration templates * - * @return array + * @return array */ public function lxc_config_template_get($data) { @@ -1131,7 +1130,7 @@ public function lxc_config_template_get($data) /** * Get ip range * - * @return array + * @return array */ public function ip_range_get($data) { @@ -1472,7 +1471,7 @@ public function ip_range_update($data) * Delete ip range * * @param int $id - * @return array + * @return array */ public function ip_range_delete($id) { @@ -1591,7 +1590,7 @@ public function proxmox_backup_config($data) /** * List existing Backups * - * @return array + * @return array */ public function proxmox_list_backups() { diff --git a/src/Controller/Admin.php b/src/Controller/Admin.php index 24752a7..0a8314c 100644 --- a/src/Controller/Admin.php +++ b/src/Controller/Admin.php @@ -37,7 +37,7 @@ public function getDi(): ?\Pimple\Container * * @return array */ - public function fetchNavigation() + public function fetchNavigation(): array { return array( 'group' => array( @@ -76,8 +76,10 @@ public function fetchNavigation() /** * Registers the admin area routes * + * @param \Box_App $app + * @return void */ - public function register(\Box_App &$app) + public function register(\Box_App &$app): void { $app->get('/serviceproxmox', 'get_index', null, get_class($this)); $app->get('/serviceproxmox/templates', 'get_templates', null, get_class($this)); @@ -103,8 +105,11 @@ public function register(\Box_App &$app) /** * Renders the admin area index page + * + * @param \Box_App $app + * @return string */ - public function get_index(\Box_App $app) + public function get_index(\Box_App $app):string { $this->di['is_admin_logged']; return $app->render('mod_serviceproxmox_index'); @@ -112,8 +117,11 @@ public function get_index(\Box_App $app) /** * Renders the admin area templates page + * + * @param \Box_App $app + * @return string */ - public function get_templates(\Box_App $app) + public function get_templates(\Box_App $app):string { return $app->render('mod_serviceproxmox_templates'); } @@ -121,8 +129,10 @@ public function get_templates(\Box_App $app) /** * Enables the QEMU Template * + * @param \Box_App $app + * @return void */ - public function enable_template(\Box_App $app, $id) + public function enable_template(\Box_App $app, $id): void { $api = $this->di['api_admin']; $api->Serviceproxmox_vm_config_template_enable(array('id' => $id)); @@ -132,8 +142,10 @@ public function enable_template(\Box_App $app, $id) /** * Disables the QEMU Template * + * @param \Box_App $app + * @return void */ - public function disable_template(\Box_App $app, $id) + public function disable_template(\Box_App $app, $id): void { $api = $this->di['api_admin']; $api->Serviceproxmox_vm_config_template_disable(array('id' => $id)); @@ -141,23 +153,36 @@ public function disable_template(\Box_App $app, $id) } /** * Renders the admin area ipam page + * + * @param \Box_App $app + * @return string */ - public function get_ipam(\Box_App $app) + public function get_ipam(\Box_App $app): string { return $app->render('mod_serviceproxmox_ipam'); } /** - * Handles CRUD for Proxmox Servers + * Handles Updates for Proxmox Servers + * + * @param \Box_App $app + * @param int $id */ - public function get_server(\Box_App $app, $id) + public function get_server(\Box_App $app, $id): string { $api = $this->di['api_admin']; $server = $api->Serviceproxmox_server_get(array('server_id' => $id)); return $app->render('mod_serviceproxmox_server', array('server' => $server)); } - public function get_server_by_group(\Box_App $app, $id) + /** + * Retrieves the server by group ID. + * + * @param \Box_App $app The Box application instance. + * @param int $id The ID of the group. + * @return string The server information. + */ + public function get_server_by_group(\Box_App $app, $id): string { $api = $this->di['api_admin']; $server = $api->Serviceproxmox_servers_in_group(array('group' => $id)); @@ -165,9 +190,14 @@ public function get_server_by_group(\Box_App $app, $id) } /** - * Handles CRUD for Proxmox Storage + * Handles Updates for Proxmox Storage + * + * @param \Box_App $app + * @param int $id + * + * @return string */ - public function get_storage(\Box_App $app, $id) + public function get_storage(\Box_App $app, $id): string { $api = $this->di['api_admin']; $storage = $api->Serviceproxmox_storage_get(array('storage_id' => $id)); @@ -175,9 +205,14 @@ public function get_storage(\Box_App $app, $id) } /** - * Handles CRUD for IP Range + * Handles Updates for IP Range + * + * @param \Box_App $app + * @param int $id + * + * @return string */ - public function get_ip_range(\Box_App $app, $id) + public function get_ip_range(\Box_App $app, $id): string { $api = $this->di['api_admin']; $ip_range = $api->Serviceproxmox_ip_range_get(array('id' => $id)); @@ -185,9 +220,12 @@ public function get_ip_range(\Box_App $app, $id) } /** - * Handles CRUD for Client VLAN + * Handles Updates for Client VLAN + * + * @param \Box_App $app + * @param int $id */ - public function client_vlan(\Box_App $app, $id) + public function client_vlan(\Box_App $app, $id): string { $api = $this->di['api_admin']; $client_vlan = $api->Serviceproxmox_vlan_get(array('id' => $id)); @@ -195,9 +233,12 @@ public function client_vlan(\Box_App $app, $id) } /** - * Handles CRUD for LXC Config Templates + * Handles Updates for LXC Config Templates + * + * @param \Box_App $app + * @param int $id */ - public function get_lxc_config_template(\Box_App $app, $id) + public function get_lxc_config_template(\Box_App $app, $id): string { $api = $this->di['api_admin']; $lxc_config_template = $api->Serviceproxmox_lxc_config_template_get(array('id' => $id)); @@ -205,9 +246,14 @@ public function get_lxc_config_template(\Box_App $app, $id) } /** - * Handles CRUD for VM Config Templates + * Handles Updates for VM Config Templates + * + * @param \Box_App $app + * @param int $id + * + * @return string */ - public function get_vm_config_template(\Box_App $app, $id) + public function get_vm_config_template(\Box_App $app, $id): string { error_log("Controller get_vm_config_template"); $api = $this->di['api_admin']; @@ -217,12 +263,15 @@ public function get_vm_config_template(\Box_App $app, $id) /** * Renders the admin area settings page + * + * @param \Box_App $app + * @return void */ - public function start_backup(\Box_App $app) + public function start_backup(\Box_App $app):void { $api = $this->di['api_admin']; - $backup = $api->Serviceproxmox_proxmox_backup_config('backup'); + $api->Serviceproxmox_proxmox_backup_config('backup'); return $app->redirect('extension/settings/serviceproxmox'); } } diff --git a/src/Controller/Client.php b/src/Controller/Client.php index b7fcdf0..ad3712c 100644 --- a/src/Controller/Client.php +++ b/src/Controller/Client.php @@ -22,7 +22,7 @@ class Client implements \FOSSBilling\InjectionAwareInterface { protected ?\Pimple\Container $di; - public function setDi(\Pimple\Container $di): void + public function setDi(\Pimple\Container|null $di): void { $this->di = $di; } @@ -32,6 +32,11 @@ public function getDi(): ?\Pimple\Container return $this->di; } + /** + * Registers a new client route. + * + * @param \Box_App &$app The Box_App instance. + */ public function register(\Box_App &$app) { // register all routers to load novnc app.js & dependencies from proxmox @@ -40,20 +45,46 @@ public function register(\Box_App &$app) $app->get('/serviceproxmox/novnc/:folder/:subfolder/:filename.:fileending', 'get_novnc_appjs_folder_subfolder_filename', [], static::class); } - // create functions to call get_novnc_appjs with paths - // create function for only filename + + /** + * Returns the filename of the NoVNC app.js file. + * + * @param \Box_App $app The Box_App object. + * @param string $filename The name of the file. + * @param string $fileending The file extension. + * @return string The filename of the NoVNC app.js file. + */ public function get_novnc_appjs_filename(\Box_App $app, $filename, $fileending) { $file_path = $filename . '.' . $fileending; return $this->get_novnc_appjs($app, $file_path); } - // create function for filename and folder + + /** + * Returns the filename of the NoVNC app.js file in a folder. + * + * @param \Box_App $app The Box_App object. + * @param string $folder The name of the folder. + * @param string $filename The name of the file. + * @param string $fileending The file extension. + * @return string The filename of the NoVNC app.js file in a folder. + */ public function get_novnc_appjs_folder_filename(\Box_App $app, $folder, $filename, $fileending) { $file_path = $folder . '/' . $filename . '.' . $fileending; return $this->get_novnc_appjs($app, $file_path); } - // create function for filename, folder and subfolder + + /** + * Returns the filename of the NoVNC app.js file in a subfolder. + * + * @param \Box_App $app The Box_App object. + * @param string $folder The name of the folder. + * @param string $subfolder The name of the subfolder. + * @param string $filename The name of the file. + * @param string $fileending The file extension. + * @return string The filename of the NoVNC app.js file in a subfolder. + */ public function get_novnc_appjs_folder_subfolder_filename(\Box_App $app, $folder, $subfolder, $filename, $fileending) { $file_path = $folder . '/' . $subfolder . '/' . $filename . '.' . $fileending; @@ -61,7 +92,13 @@ public function get_novnc_appjs_folder_subfolder_filename(\Box_App $app, $folder } - // create get_novnc_appjs function + /** + * Returns the contents of the specified JavaScript file for the noVNC app. + * + * @param \Box_App $app The Box application instance. + * @param string $file_path The path to the JavaScript file. + * @return string The contents of the JavaScript file. + */ public function get_novnc_appjs(\Box_App $app, $file_path) { $api = $this->di['api_client']; diff --git a/src/Service.php b/src/Service.php index 1e0dc29..3ad21a6 100644 --- a/src/Service.php +++ b/src/Service.php @@ -39,10 +39,6 @@ public function setDi(\Pimple\Container|null $di): void $this->di = $di; } - public function getDi(): ?\Pimple\Container - { - return $this->di; - } use ProxmoxAuthentication; use ProxmoxServer; use ProxmoxVM; @@ -57,8 +53,10 @@ public function getDi(): ?\Pimple\Container * database table might be enough. * * @return bool + * + * @throws \Box_Exception */ - public function install() + public function install(): bool { // read manifest.json to get current version number $manifest = json_decode(file_get_contents(__DIR__ . '/manifest.json'), true); @@ -208,7 +206,7 @@ public function install() * * @return bool */ - public function uninstall() + public function uninstall(): bool { $this->pmxdbbackup('uninstall'); $this->di['db']->exec("DROP TABLE IF EXISTS `service_proxmox`"); @@ -242,7 +240,7 @@ public function uninstall() * @param string $previous_version * @return bool */ - public function upgrade($previous_version) + public function upgrade($previous_version): bool { // read current module version from manifest.json $manifest = json_decode(file_get_contents(__DIR__ . '/manifest.json'), true); @@ -290,6 +288,26 @@ public function upgrade($previous_version) return true; } + /** + * Method to update module. When you release new version to + * extensions.fossbilling.org then this method will be called + * after the new files are placed. + * + * @param array $manifest - information about the new module version + * + * @return bool + * + * @throws \Box_Exception + */ + public function update(array $manifest): bool + { + // throw new \Box_Exception("Throw exception to terminate module update process with a message", array(), 125); + return true; + } + + + + /** * Method to check if all tables have been migrated to current Module Version. * Not yet used, but will be in the admin settings page for the module diff --git a/src/manifest.json b/src/manifest.json index 5fc02a1..8082ef6 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -9,4 +9,4 @@ "author_url": "https://github.com/FOSSBilling/", "license": "GPLv3", "version": "0.1.0" -} +} \ No newline at end of file