From 13699c3b12628d33ee8e105853f5996523fa02f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Wed, 10 Apr 2024 14:34:10 +0200 Subject: [PATCH 1/3] Use bound Closure instead of "$self = $this" ... fn use ($self) --- src/ExtensionInstaller.php | 51 ++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/src/ExtensionInstaller.php b/src/ExtensionInstaller.php index db13674..93fcd6b 100644 --- a/src/ExtensionInstaller.php +++ b/src/ExtensionInstaller.php @@ -60,21 +60,20 @@ public function install(InstalledRepositoryInterface $repo, PackageInterface $pa $this->rcubeVersionCheck($package); - $self = $this; - $postInstall = function() use ($self, $package) { - $config_file = $self->rcubeConfigFile(); - $package_name = $self->getPackageName($package); - $package_dir = $self->getVendorDir() . DIRECTORY_SEPARATOR . $package_name; + $postInstall = function() use ($package) { + $config_file = $this->rcubeConfigFile(); + $package_name = $this->getPackageName($package); + $package_dir = $this->getVendorDir() . DIRECTORY_SEPARATOR . $package_name; $extra = $package->getExtra(); if (is_writeable($config_file) && php_sapi_name() == 'cli' && $this->confirmInstall($package_name)) { - $self->rcubeAlterConfig($package_name); + $this->rcubeAlterConfig($package_name); } // copy config.inc.php.dist -> config.inc.php if (is_file($package_dir . DIRECTORY_SEPARATOR . 'config.inc.php.dist')) { $config_exists = false; - $alt_config_file = $self->rcubeConfigFile($package_name . '.inc.php'); + $alt_config_file = $this->rcubeConfigFile($package_name . '.inc.php'); if (is_file($package_dir . DIRECTORY_SEPARATOR . 'config.inc.php')) { $config_exists = true; @@ -84,7 +83,7 @@ public function install(InstalledRepositoryInterface $repo, PackageInterface $pa } if (!$config_exists && is_writeable($package_dir)) { - $self->io->write("Creating package config file"); + $this->io->write("Creating package config file"); copy($package_dir . DIRECTORY_SEPARATOR . 'config.inc.php.dist', $package_dir . DIRECTORY_SEPARATOR . 'config.inc.php'); } } @@ -92,7 +91,7 @@ public function install(InstalledRepositoryInterface $repo, PackageInterface $pa // initialize database schema if (!empty($extra['roundcube']['sql-dir'])) { if ($sqldir = realpath($package_dir . DIRECTORY_SEPARATOR . $extra['roundcube']['sql-dir'])) { - $self->io->write("Running database initialization script for $package_name"); + $this->io->write("Running database initialization script for $package_name"); $roundcube_version = self::versionNormalize(RCMAIL_VERSION); if (self::versionCompare($roundcube_version, '1.2.0', '>=')) { @@ -106,7 +105,7 @@ public function install(InstalledRepositoryInterface $repo, PackageInterface $pa // run post-install script if (!empty($extra['roundcube']['post-install-script'])) { - $self->rcubeRunScript($extra['roundcube']['post-install-script'], $package); + $this->rcubeRunScript($extra['roundcube']['post-install-script'], $package); } }; @@ -134,21 +133,20 @@ public function update(InstalledRepositoryInterface $repo, PackageInterface $ini $this->rcubeVersionCheck($target); - $self = $this; $extra = $target->getExtra(); $fs = new Filesystem(); // backup persistent files e.g. config.inc.php - $package_name = $self->getPackageName($initial); - $package_dir = $self->getVendorDir() . DIRECTORY_SEPARATOR . $package_name; + $package_name = $this->getPackageName($initial); + $package_dir = $this->getVendorDir() . DIRECTORY_SEPARATOR . $package_name; $temp_dir = $package_dir . '-' . sprintf('%010d%010d', mt_rand(), mt_rand()); // make a backup of existing files (for restoring persistent files) $fs->copy($package_dir, $temp_dir); - $postUpdate = function() use ($self, $target, $extra, $fs, $temp_dir) { - $package_name = $self->getPackageName($target); - $package_dir = $self->getVendorDir() . DIRECTORY_SEPARATOR . $package_name; + $postUpdate = function() use ($target, $extra, $fs, $temp_dir) { + $package_name = $this->getPackageName($target); + $package_dir = $this->getVendorDir() . DIRECTORY_SEPARATOR . $package_name; // restore persistent files $persistent_files = !empty($extra['roundcube']['persistent-files']) ? $extra['roundcube']['persistent-files'] : ['config.inc.php']; @@ -156,7 +154,7 @@ public function update(InstalledRepositoryInterface $repo, PackageInterface $ini $path = $temp_dir . DIRECTORY_SEPARATOR . $file; if (is_readable($path)) { if ($fs->copy($path, $package_dir . DIRECTORY_SEPARATOR . $file)) { - $self->io->write("Restored $package_name/$file"); + $this->io->write("Restored $package_name/$file"); } else { throw new \Exception("Restoring " . $file . " failed."); @@ -169,7 +167,7 @@ public function update(InstalledRepositoryInterface $repo, PackageInterface $ini // update database schema if (!empty($extra['roundcube']['sql-dir'])) { if ($sqldir = realpath($package_dir . DIRECTORY_SEPARATOR . $extra['roundcube']['sql-dir'])) { - $self->io->write("Updating database schema for $package_name"); + $this->io->write("Updating database schema for $package_name"); $roundcube_version = self::versionNormalize(RCMAIL_VERSION); if (self::versionCompare($roundcube_version, '1.2.0', '>=')) { @@ -183,7 +181,7 @@ public function update(InstalledRepositoryInterface $repo, PackageInterface $ini // run post-update script if (!empty($extra['roundcube']['post-update-script'])) { - $self->rcubeRunScript($extra['roundcube']['post-update-script'], $target); + $this->rcubeRunScript($extra['roundcube']['post-update-script'], $target); } }; @@ -209,27 +207,26 @@ public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $ } require_once INSTALL_PATH . 'program/include/iniset.php'; - $self = $this; - $config = $self->composer->getConfig()->get('roundcube'); + $config = $this->composer->getConfig()->get('roundcube'); - $postUninstall = function() use ($self, $package, $config) { + $postUninstall = function() use ($package, $config) { // post-uninstall: deactivate package - $package_name = $self->getPackageName($package); - $package_dir = $self->getVendorDir() . DIRECTORY_SEPARATOR . $package_name; + $package_name = $this->getPackageName($package); + $package_dir = $this->getVendorDir() . DIRECTORY_SEPARATOR . $package_name; - $self->rcubeAlterConfig($package_name, false); + $this->rcubeAlterConfig($package_name, false); // run post-uninstall script $extra = $package->getExtra(); if (!empty($extra['roundcube']['post-uninstall-script'])) { - $self->rcubeRunScript($extra['roundcube']['post-uninstall-script'], $package); + $this->rcubeRunScript($extra['roundcube']['post-uninstall-script'], $package); } // remove package folder if (!empty($config['uninstall-remove-folder'])) { $fs = new Filesystem(); $fs->remove($package_dir); - $self->io->write("Removed $package_name files"); + $this->io->write("Removed $package_name files"); } }; From 6f7ae6d19a8df52bfb8c1266becc586cac084d98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Wed, 10 Apr 2024 14:45:01 +0200 Subject: [PATCH 2/3] Fix errors reported by PHPStan --- phpstan.neon.dist | 28 +--------------------------- src/ExtensionInstaller.php | 21 +++++++++++++-------- src/PluginInstaller.php | 6 +++--- src/SkinInstaller.php | 6 +++--- 4 files changed, 20 insertions(+), 41 deletions(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 2df985e..f27b06b 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -12,34 +12,8 @@ parameters: # relax strict rules - '~^Only booleans are allowed in .+, .+ given( on the (left|right) side)?\.~' - '~^Construct empty\(\) is not allowed\. Use more strict comparison\.~' - - '~^Loose comparison via "[=!]=" is not allowed\.~' - - # TODO - - - message: '~^Anonymous function uses \$this assigned to variable \$self. Use \$this directly in the function body\.$~' - path: 'src/ExtensionInstaller.php' - count: 3 + - message: '~^Constant RCMAIL_VERSION not found\.$~' path: 'src/ExtensionInstaller.php' count: 3 - - - message: '~^Method Roundcube\\Composer\\ExtensionInstaller::install\(\) should return React\\Promise\\PromiseInterface|null but return statement is missing\.$~' - path: 'src/ExtensionInstaller.php' - count: 3 - - - message: '~^Undefined variable: \$rootdir$~' - path: 'src/ExtensionInstaller.php' - count: 1 - - - message: '~^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$~' - path: 'src/ExtensionInstaller.php' - count: 1 - - - message: '~^Call to function in_array\(\) requires parameter #3 to be set\.$~' - path: 'src/PluginInstaller.php' - count: 1 - - - message: '~^Call to function array_search\(\) requires parameter #3 to be set\.$~' - path: 'src/PluginInstaller.php' - count: 1 diff --git a/src/ExtensionInstaller.php b/src/ExtensionInstaller.php index 93fcd6b..2e0f695 100644 --- a/src/ExtensionInstaller.php +++ b/src/ExtensionInstaller.php @@ -66,7 +66,7 @@ public function install(InstalledRepositoryInterface $repo, PackageInterface $pa $package_dir = $this->getVendorDir() . DIRECTORY_SEPARATOR . $package_name; $extra = $package->getExtra(); - if (is_writeable($config_file) && php_sapi_name() == 'cli' && $this->confirmInstall($package_name)) { + if (is_writeable($config_file) && php_sapi_name() === 'cli' && $this->confirmInstall($package_name)) { $this->rcubeAlterConfig($package_name); } @@ -118,6 +118,8 @@ public function install(InstalledRepositoryInterface $repo, PackageInterface $pa // If not, execute the code right away (composer v1, or v2 without async) $postInstall(); + + return null; } /** @@ -194,6 +196,8 @@ public function update(InstalledRepositoryInterface $repo, PackageInterface $ini // If not, execute the code right away (composer v1, or v2 without async) $postUpdate(); + + return null; } /** @@ -239,6 +243,8 @@ public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $ // If not, execute the code right away (composer v1, or v2 without async) $postUninstall(); + + return null; } /** @@ -275,10 +281,6 @@ private function rcubeVersionCheck($package) // read rcube version from iniset $rcubeVersion = self::versionNormalize(RCMAIL_VERSION); - if (empty($rcubeVersion)) { - throw new \Exception("Unable to find a Roundcube installation in $rootdir"); - } - $extra = $package->getExtra(); if (!empty($extra['roundcube'])) { @@ -309,7 +311,10 @@ private function rcubeAlterConfig($package_name, $add = true) } if (!empty($config) && is_writeable($config_file)) { - $config_template = @file_get_contents($config_file) ?: ''; + $config_template = @file_get_contents($config_file); + if ($config_template === false) { + $config_template = ''; + } if ($config = $this->getConfig($package_name, $config, $add)) { list($config_name, $config_val) = $config; @@ -342,7 +347,7 @@ private function rcubeAlterConfig($package_name, $add = true) } } - if ($success && php_sapi_name() == 'cli') { + if ($success && php_sapi_name() === 'cli') { $this->io->write("Updated local config at $config_file"); } @@ -417,7 +422,7 @@ private function rcubeRunScript($script, PackageInterface $package) /** * normalize Roundcube version string */ - private static function versionNormalize($version) + private static function versionNormalize(string $version): string { $parser = new VersionParser; diff --git a/src/PluginInstaller.php b/src/PluginInstaller.php index a73ee9e..de026b6 100644 --- a/src/PluginInstaller.php +++ b/src/PluginInstaller.php @@ -38,14 +38,14 @@ protected function getConfig($package_name, $config, $add ) $cur_config = !empty($config['plugins']) ? ((array) $config['plugins']) : []; $new_config = $cur_config; - if ($add && !in_array($package_name, $new_config)) { + if ($add && !in_array($package_name, $new_config, true)) { $new_config[] = $package_name; } - elseif (!$add && ($i = array_search($package_name, $new_config)) !== false) { + elseif (!$add && ($i = array_search($package_name, $new_config, true)) !== false) { unset($new_config[$i]); } - if ($new_config != $cur_config) { + if ($new_config !== $cur_config) { $config_val = count($new_config) > 0 ? "[\n\t'" . join("',\n\t'", $new_config) . "',\n];" : "[];"; $result = ['plugins', $config_val]; } diff --git a/src/SkinInstaller.php b/src/SkinInstaller.php index b47517b..1b99ab8 100644 --- a/src/SkinInstaller.php +++ b/src/SkinInstaller.php @@ -38,14 +38,14 @@ protected function getConfig($package_name, $config, $add) $cur_config = !empty($config['skin']) ? $config['skin'] : null; $new_config = $cur_config; - if ($add && $new_config != $package_name) { + if ($add && $new_config !== $package_name) { $new_config = $package_name; } - elseif (!$add && $new_config == $package_name) { + elseif (!$add && $new_config === $package_name) { $new_config = null; } - if ($new_config != $cur_config) { + if ($new_config !== $cur_config) { $config_val = !empty($new_config) ? "'$new_config';" : null; $result = ['skin', $config_val]; } From 5f368a76371d7866c555dc36a4513fa48e7e8175 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Wed, 10 Apr 2024 15:00:46 +0200 Subject: [PATCH 3/3] Improve and assert CS --- .php-cs-fixer.dist.php | 37 +------- src/ExtensionInstaller.php | 181 ++++++++++++++++--------------------- src/PluginInstaller.php | 25 ++--- src/RoundcubeInstaller.php | 4 +- src/SkinInstaller.php | 23 ++--- 5 files changed, 94 insertions(+), 176 deletions(-) diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index a975122..e038a4a 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -58,43 +58,8 @@ // also prevent bounding of unwanted variables for GC 'use_arrow_functions' => false, - // TODO - 'align_multiline_comment' => false, - 'array_indentation' => false, - 'binary_operator_spaces' => false, - 'blank_line_after_opening_tag' => false, - 'blank_line_before_statement' => false, - 'concat_space' => false, - 'control_structure_continuation_position' => false, + // disable too destructive formating for now 'declare_strict_types' => false, - 'explicit_string_variable' => false, - 'function_declaration' => false, - 'function_to_constant' => false, - 'general_phpdoc_annotation_remove' => false, - 'include' => false, - 'list_syntax' => false, - 'method_argument_space' => false, - 'native_constant_invocation' => false, - 'new_with_parentheses' => false, - 'no_alias_functions' => false, - 'no_empty_phpdoc' => false, - 'no_spaces_after_function_name' => false, - 'no_superfluous_phpdoc_tags' => false, - 'ordered_imports' => false, - 'phpdoc_indent' => false, - 'phpdoc_no_alias_tag' => false, - 'phpdoc_no_package' => false, - 'phpdoc_separation' => false, - 'phpdoc_summary' => false, - 'single_line_empty_body' => false, - 'single_quote' => false, - 'single_space_around_construct' => false, - 'spaces_inside_parentheses' => false, - 'static_lambda' => false, - 'strict_comparison' => false, - 'strict_param' => false, - 'string_implicit_backslashes' => false, - 'yoda_style' => false, ]) ->setFinder($finder) ->setCacheFile(sys_get_temp_dir() . '/php-cs-fixer.' . md5(__DIR__) . '.cache'); diff --git a/src/ExtensionInstaller.php b/src/ExtensionInstaller.php index 2e0f695..ef34035 100644 --- a/src/ExtensionInstaller.php +++ b/src/ExtensionInstaller.php @@ -3,23 +3,13 @@ namespace Roundcube\Composer; use Composer\Installer\LibraryInstaller; -use Composer\Package\Version\VersionParser; use Composer\Package\PackageInterface; +use Composer\Package\Version\VersionParser; use Composer\Repository\InstalledRepositoryInterface; -use Composer\Util\ProcessExecutor; use Composer\Util\Filesystem; +use Composer\Util\ProcessExecutor; use React\Promise\PromiseInterface; -/** - * @category Plugins - * @package PluginInstaller - * @author Till Klampaeckel - * @author Thomas Bruederli - * @author Philip Weir - * @license GPL-3.0+ - * @version GIT: - * @link https://github.com/roundcube/plugin-installer - */ abstract class ExtensionInstaller extends LibraryInstaller { protected $composer_type; @@ -33,9 +23,6 @@ protected function getRoundcubemailInstallPath(): string return $this->getInstallPath($roundcubemailPackage); } - /** - * {@inheritDoc} - */ public function getInstallPath(PackageInterface $package) { if (!$this->supports($package->getType())) { @@ -47,9 +34,6 @@ public function getInstallPath(PackageInterface $package) return sprintf('%s/%s', $vendorDir, $this->getPackageName($package)); } - /** - * {@inheritDoc} - */ public function install(InstalledRepositoryInterface $repo, PackageInterface $package) { // initialize Roundcube environment @@ -60,45 +44,43 @@ public function install(InstalledRepositoryInterface $repo, PackageInterface $pa $this->rcubeVersionCheck($package); - $postInstall = function() use ($package) { - $config_file = $this->rcubeConfigFile(); + $postInstall = function () use ($package) { + $config_file = $this->rcubeConfigFile(); $package_name = $this->getPackageName($package); - $package_dir = $this->getVendorDir() . DIRECTORY_SEPARATOR . $package_name; - $extra = $package->getExtra(); + $package_dir = $this->getVendorDir() . \DIRECTORY_SEPARATOR . $package_name; + $extra = $package->getExtra(); - if (is_writeable($config_file) && php_sapi_name() === 'cli' && $this->confirmInstall($package_name)) { + if (is_writable($config_file) && \PHP_SAPI === 'cli' && $this->confirmInstall($package_name)) { $this->rcubeAlterConfig($package_name); } // copy config.inc.php.dist -> config.inc.php - if (is_file($package_dir . DIRECTORY_SEPARATOR . 'config.inc.php.dist')) { - $config_exists = false; + if (is_file($package_dir . \DIRECTORY_SEPARATOR . 'config.inc.php.dist')) { + $config_exists = false; $alt_config_file = $this->rcubeConfigFile($package_name . '.inc.php'); - if (is_file($package_dir . DIRECTORY_SEPARATOR . 'config.inc.php')) { + if (is_file($package_dir . \DIRECTORY_SEPARATOR . 'config.inc.php')) { $config_exists = true; - } - elseif (is_file($alt_config_file)) { + } elseif (is_file($alt_config_file)) { $config_exists = true; } - if (!$config_exists && is_writeable($package_dir)) { - $this->io->write("Creating package config file"); - copy($package_dir . DIRECTORY_SEPARATOR . 'config.inc.php.dist', $package_dir . DIRECTORY_SEPARATOR . 'config.inc.php'); + if (!$config_exists && is_writable($package_dir)) { + $this->io->write('Creating package config file'); + copy($package_dir . \DIRECTORY_SEPARATOR . 'config.inc.php.dist', $package_dir . \DIRECTORY_SEPARATOR . 'config.inc.php'); } } // initialize database schema if (!empty($extra['roundcube']['sql-dir'])) { - if ($sqldir = realpath($package_dir . DIRECTORY_SEPARATOR . $extra['roundcube']['sql-dir'])) { - $this->io->write("Running database initialization script for $package_name"); + if ($sqldir = realpath($package_dir . \DIRECTORY_SEPARATOR . $extra['roundcube']['sql-dir'])) { + $this->io->write("Running database initialization script for {$package_name}"); $roundcube_version = self::versionNormalize(RCMAIL_VERSION); if (self::versionCompare($roundcube_version, '1.2.0', '>=')) { \rcmail_utils::db_init($sqldir); - } - else { - throw new \Exception("Database initialization failed. Roundcube 1.2.0 or above required."); + } else { + throw new \Exception('Database initialization failed. Roundcube 1.2.0 or above required.'); } } } @@ -122,9 +104,6 @@ public function install(InstalledRepositoryInterface $repo, PackageInterface $pa return null; } - /** - * {@inheritDoc} - */ public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target) { // initialize Roundcube environment @@ -136,30 +115,29 @@ public function update(InstalledRepositoryInterface $repo, PackageInterface $ini $this->rcubeVersionCheck($target); $extra = $target->getExtra(); - $fs = new Filesystem(); + $fs = new Filesystem(); // backup persistent files e.g. config.inc.php - $package_name = $this->getPackageName($initial); - $package_dir = $this->getVendorDir() . DIRECTORY_SEPARATOR . $package_name; - $temp_dir = $package_dir . '-' . sprintf('%010d%010d', mt_rand(), mt_rand()); + $package_name = $this->getPackageName($initial); + $package_dir = $this->getVendorDir() . \DIRECTORY_SEPARATOR . $package_name; + $temp_dir = $package_dir . '-' . sprintf('%010d%010d', mt_rand(), mt_rand()); // make a backup of existing files (for restoring persistent files) $fs->copy($package_dir, $temp_dir); - $postUpdate = function() use ($target, $extra, $fs, $temp_dir) { + $postUpdate = function () use ($target, $extra, $fs, $temp_dir) { $package_name = $this->getPackageName($target); - $package_dir = $this->getVendorDir() . DIRECTORY_SEPARATOR . $package_name; + $package_dir = $this->getVendorDir() . \DIRECTORY_SEPARATOR . $package_name; // restore persistent files $persistent_files = !empty($extra['roundcube']['persistent-files']) ? $extra['roundcube']['persistent-files'] : ['config.inc.php']; foreach ($persistent_files as $file) { - $path = $temp_dir . DIRECTORY_SEPARATOR . $file; + $path = $temp_dir . \DIRECTORY_SEPARATOR . $file; if (is_readable($path)) { - if ($fs->copy($path, $package_dir . DIRECTORY_SEPARATOR . $file)) { - $this->io->write("Restored $package_name/$file"); - } - else { - throw new \Exception("Restoring " . $file . " failed."); + if ($fs->copy($path, $package_dir . \DIRECTORY_SEPARATOR . $file)) { + $this->io->write("Restored {$package_name}/{$file}"); + } else { + throw new \Exception('Restoring ' . $file . ' failed.'); } } } @@ -168,15 +146,14 @@ public function update(InstalledRepositoryInterface $repo, PackageInterface $ini // update database schema if (!empty($extra['roundcube']['sql-dir'])) { - if ($sqldir = realpath($package_dir . DIRECTORY_SEPARATOR . $extra['roundcube']['sql-dir'])) { - $this->io->write("Updating database schema for $package_name"); + if ($sqldir = realpath($package_dir . \DIRECTORY_SEPARATOR . $extra['roundcube']['sql-dir'])) { + $this->io->write("Updating database schema for {$package_name}"); $roundcube_version = self::versionNormalize(RCMAIL_VERSION); if (self::versionCompare($roundcube_version, '1.2.0', '>=')) { \rcmail_utils::db_update($sqldir, $package_name); - } - else { - throw new \Exception("Database update failed. Roundcube 1.2.0 or above required."); + } else { + throw new \Exception('Database update failed. Roundcube 1.2.0 or above required.'); } } } @@ -200,9 +177,6 @@ public function update(InstalledRepositoryInterface $repo, PackageInterface $ini return null; } - /** - * {@inheritDoc} - */ public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package) { // initialize Roundcube environment @@ -213,10 +187,10 @@ public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $ $config = $this->composer->getConfig()->get('roundcube'); - $postUninstall = function() use ($package, $config) { + $postUninstall = function () use ($package, $config) { // post-uninstall: deactivate package $package_name = $this->getPackageName($package); - $package_dir = $this->getVendorDir() . DIRECTORY_SEPARATOR . $package_name; + $package_dir = $this->getVendorDir() . \DIRECTORY_SEPARATOR . $package_name; $this->rcubeAlterConfig($package_name, false); @@ -230,7 +204,7 @@ public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $ if (!empty($config['uninstall-remove-folder'])) { $fs = new Filesystem(); $fs->remove($package_dir); - $this->io->write("Removed $package_name files"); + $this->io->write("Removed {$package_name} files"); } }; @@ -247,9 +221,6 @@ public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $ return null; } - /** - * {@inheritDoc} - */ public function supports($packageType) { return $packageType === $this->composer_type; @@ -263,18 +234,18 @@ public function supports($packageType) abstract public function getVendorDir(); /** - * Extract the (valid) package name from the package object + * Extract the (valid) package name from the package object. */ protected function getPackageName(PackageInterface $package) { - @list($vendor, $packageName) = explode('/', $package->getPrettyName()); + @[$vendor, $packageName] = explode('/', $package->getPrettyName()); return strtr($packageName, '-', '_'); } /** * Check version requirements from the "extra" block of a package - * against the local Roundcube version + * against the local Roundcube version. */ private function rcubeVersionCheck($package) { @@ -288,7 +259,7 @@ private function rcubeVersionCheck($package) if (!empty($extra['roundcube'][$key])) { $version = self::versionNormalize($extra['roundcube'][$key]); if (!self::versionCompare($rcubeVersion, $version, $operator)) { - throw new \Exception("Version check failed! " . $package->getName() . " requires Roundcube version $operator $version, $rcubeVersion was detected."); + throw new \Exception('Version check failed! ' . $package->getName() . " requires Roundcube version {$operator} {$version}, {$rcubeVersion} was detected."); } } } @@ -301,41 +272,46 @@ private function rcubeVersionCheck($package) private function rcubeAlterConfig($package_name, $add = true) { $config_file = $this->rcubeConfigFile(); - @include($config_file); + @include $config_file; $success = false; $varname = '$config'; if (empty($config) && !empty($rcmail_config)) { - $config = $rcmail_config; + $config = $rcmail_config; $varname = '$rcmail_config'; } - if (!empty($config) && is_writeable($config_file)) { + if (!empty($config) && is_writable($config_file)) { $config_template = @file_get_contents($config_file); if ($config_template === false) { $config_template = ''; } if ($config = $this->getConfig($package_name, $config, $add)) { - list($config_name, $config_val) = $config; + [$config_name, $config_val] = $config; $count = 0; if (empty($config_val)) { $new_config = preg_replace( - "/(\\$varname\['$config_name'\])\s+=\s+(.+);/Uims", - "", - $config_template, -1, $count); - } - else { + "/(\\{$varname}\\['{$config_name}'\\])\\s+=\\s+(.+);/Uims", + '', + $config_template, + -1, + $count + ); + } else { $new_config = preg_replace( - "/(\\$varname\['$config_name'\])\s+=\s+(.+);/Uims", - "\\1 = " . $config_val, - $config_template, -1, $count); + "/(\\{$varname}\\['{$config_name}'\\])\\s+=\\s+(.+);/Uims", + '\\1 = ' . $config_val, + $config_template, + -1, + $count + ); } // config option does not exist yet, add it... if (!$count) { - $var_txt = "\n{$varname}['$config_name'] = $config_val\n"; + $var_txt = "\n{$varname}['{$config_name}'] = {$config_val}\n"; $new_config = str_replace('?>', $var_txt . '?>', $config_template, $count); if (!$count) { @@ -347,41 +323,42 @@ private function rcubeAlterConfig($package_name, $add = true) } } - if ($success && php_sapi_name() === 'cli') { - $this->io->write("Updated local config at $config_file"); + if ($success && \PHP_SAPI === 'cli') { + $this->io->write("Updated local config at {$config_file}"); } return $success; } /** - * Ask the user to confirm installation - */ + * Ask the user to confirm installation. + */ protected function confirmInstall($package_name) { return false; } /** - * Generate Roundcube config entry - */ + * Generate Roundcube config entry. + */ protected function getConfig($package_name, $cur_config, $add) { return false; } /** - * Helper method to get an absolute path to the local Roundcube config file + * Helper method to get an absolute path to the local Roundcube config file. */ private function rcubeConfigFile($file = 'config.inc.php') { $config = new \rcube_config(); - $paths = $config->resolve_paths($file); - $path = $this->getRoundcubemailInstallPath() . '/config/' . $file; + $paths = $config->resolve_paths($file); + $path = $this->getRoundcubemailInstallPath() . '/config/' . $file; foreach ($paths as $fpath) { if ($fpath && is_file($fpath) && is_readable($fpath)) { $path = $fpath; + break; } } @@ -390,52 +367,52 @@ private function rcubeConfigFile($file = 'config.inc.php') } /** - * Run the given script file + * Run the given script file. */ private function rcubeRunScript($script, PackageInterface $package) { $package_name = $this->getPackageName($package); $package_type = $package->getType(); - $package_dir = $this->getVendorDir() . DIRECTORY_SEPARATOR . $package_name; + $package_dir = $this->getVendorDir() . \DIRECTORY_SEPARATOR . $package_name; // check for executable shell script - if (($scriptfile = realpath($package_dir . DIRECTORY_SEPARATOR . $script)) && is_executable($scriptfile)) { + if (($scriptfile = realpath($package_dir . \DIRECTORY_SEPARATOR . $script)) && is_executable($scriptfile)) { $script = $scriptfile; } // run PHP script in Roundcube context if ($scriptfile && preg_match('/\.php$/', $scriptfile)) { $incdir = realpath($this->getRoundcubemailInstallPath() . '/program/include'); - include_once($incdir . '/iniset.php'); - include($scriptfile); + include_once $incdir . '/iniset.php'; + include $scriptfile; } // attempt to execute the given string as shell commands else { $process = new ProcessExecutor($this->io); $exitCode = $process->execute($script, $output, $package_dir); if ($exitCode !== 0) { - throw new \RuntimeException('Error executing script: '. $process->getErrorOutput(), $exitCode); + throw new \RuntimeException('Error executing script: ' . $process->getErrorOutput(), $exitCode); } } } /** - * normalize Roundcube version string + * normalize Roundcube version string. */ private static function versionNormalize(string $version): string { - $parser = new VersionParser; + $parser = new VersionParser(); return $parser->normalize(str_replace('-git', '.999', $version)); } /** - * version_compare() wrapper, originally from composer/semver + * version_compare() wrapper, originally from composer/semver. */ private static function versionCompare($a, $b, $operator, $compareBranches = false) { - $aIsBranch = 'dev-' === substr($a, 0, 4); - $bIsBranch = 'dev-' === substr($b, 0, 4); + $aIsBranch = substr($a, 0, 4) === 'dev-'; + $bIsBranch = substr($b, 0, 4) === 'dev-'; if ($aIsBranch && $bIsBranch) { return $operator === '==' && $a === $b; diff --git a/src/PluginInstaller.php b/src/PluginInstaller.php index de026b6..6bd2c9f 100644 --- a/src/PluginInstaller.php +++ b/src/PluginInstaller.php @@ -2,21 +2,13 @@ namespace Roundcube\Composer; -/** - * @category Plugins - * @package PluginInstaller - * @author Philip Weir - * @license GPL-3.0+ - * @version GIT: - * @link https://github.com/roundcube/plugin-installer - */ class PluginInstaller extends ExtensionInstaller { protected $composer_type = 'roundcube-plugin'; public function getVendorDir() { - return $this->getRoundcubemailInstallPath() . DIRECTORY_SEPARATOR . 'plugins'; + return $this->getRoundcubemailInstallPath() . \DIRECTORY_SEPARATOR . 'plugins'; } protected function confirmInstall($package_name) @@ -25,31 +17,28 @@ protected function confirmInstall($package_name) if (isset($config['enable-plugin'])) { $answer = $config['enable-plugin']; - } - else { - $answer = $this->io->askConfirmation("Do you want to activate the plugin $package_name? [Y|n] ", true); + } else { + $answer = $this->io->askConfirmation("Do you want to activate the plugin {$package_name}? [Y|n] ", true); } return $answer; } - protected function getConfig($package_name, $config, $add ) + protected function getConfig($package_name, $config, $add) { $cur_config = !empty($config['plugins']) ? ((array) $config['plugins']) : []; $new_config = $cur_config; if ($add && !in_array($package_name, $new_config, true)) { $new_config[] = $package_name; - } - elseif (!$add && ($i = array_search($package_name, $new_config, true)) !== false) { + } elseif (!$add && ($i = array_search($package_name, $new_config, true)) !== false) { unset($new_config[$i]); } if ($new_config !== $cur_config) { - $config_val = count($new_config) > 0 ? "[\n\t'" . join("',\n\t'", $new_config) . "',\n];" : "[];"; + $config_val = count($new_config) > 0 ? "[\n\t'" . implode("',\n\t'", $new_config) . "',\n];" : '[];'; $result = ['plugins', $config_val]; - } - else { + } else { $result = false; } diff --git a/src/RoundcubeInstaller.php b/src/RoundcubeInstaller.php index e6abe35..a7f81fe 100644 --- a/src/RoundcubeInstaller.php +++ b/src/RoundcubeInstaller.php @@ -27,7 +27,5 @@ public function deactivate(Composer $composer, IOInterface $io) } } - public function uninstall(Composer $composer, IOInterface $io) - { - } + public function uninstall(Composer $composer, IOInterface $io) {} } diff --git a/src/SkinInstaller.php b/src/SkinInstaller.php index 1b99ab8..5854bf9 100644 --- a/src/SkinInstaller.php +++ b/src/SkinInstaller.php @@ -2,21 +2,13 @@ namespace Roundcube\Composer; -/** - * @category Plugins - * @package PluginInstaller - * @author Philip Weir - * @license GPL-3.0+ - * @version GIT: - * @link https://github.com/roundcube/plugin-installer - */ class SkinInstaller extends ExtensionInstaller { protected $composer_type = 'roundcube-skin'; public function getVendorDir() { - return $this->getRoundcubemailInstallPath() . DIRECTORY_SEPARATOR . 'skins'; + return $this->getRoundcubemailInstallPath() . \DIRECTORY_SEPARATOR . 'skins'; } protected function confirmInstall($package_name) @@ -25,9 +17,8 @@ protected function confirmInstall($package_name) if (isset($config['enable-skin'])) { $answer = $config['enable-skin']; - } - else { - $answer = $this->io->askConfirmation("Do you want to activate the skin $package_name? [Y|n] ", true); + } else { + $answer = $this->io->askConfirmation("Do you want to activate the skin {$package_name}? [Y|n] ", true); } return $answer; @@ -40,16 +31,14 @@ protected function getConfig($package_name, $config, $add) if ($add && $new_config !== $package_name) { $new_config = $package_name; - } - elseif (!$add && $new_config === $package_name) { + } elseif (!$add && $new_config === $package_name) { $new_config = null; } if ($new_config !== $cur_config) { - $config_val = !empty($new_config) ? "'$new_config';" : null; + $config_val = !empty($new_config) ? "'{$new_config}';" : null; $result = ['skin', $config_val]; - } - else { + } else { $result = false; }