From 9a5121ed2737eff1902e805365340201c55fc12b Mon Sep 17 00:00:00 2001 From: Bezpiatov Date: Wed, 14 Oct 2020 17:56:42 +0300 Subject: [PATCH 1/3] Refactor Operation classes to use constants instead of strings for comparison --- .../Operation/InstallOperation.php | 42 +----------- .../Operation/MarkAliasInstalledOperation.php | 34 +--------- .../MarkAliasUninstalledOperation.php | 34 +--------- .../Operation/Operation.php | 68 +++++++++++++++++++ .../Operation/UninstallOperation.php | 42 +----------- .../Operation/UpdateOperation.php | 26 ++----- 6 files changed, 83 insertions(+), 163 deletions(-) create mode 100644 src/Composer/DependencyResolver/Operation/Operation.php diff --git a/src/Composer/DependencyResolver/Operation/InstallOperation.php b/src/Composer/DependencyResolver/Operation/InstallOperation.php index a9f808101..4e04485fb 100644 --- a/src/Composer/DependencyResolver/Operation/InstallOperation.php +++ b/src/Composer/DependencyResolver/Operation/InstallOperation.php @@ -19,39 +19,9 @@ use Composer\Package\PackageInterface; * * @author Konstantin Kudryashov */ -class InstallOperation implements OperationInterface +class InstallOperation extends Operation implements OperationInterface { - protected $package; - - /** - * Initializes operation. - * - * @param PackageInterface $package package instance - */ - public function __construct(PackageInterface $package) - { - $this->package = $package; - } - - /** - * Returns package instance. - * - * @return PackageInterface - */ - public function getPackage() - { - return $this->package; - } - - /** - * Returns operation type. - * - * @return string - */ - public function getOperationType() - { - return 'install'; - } + const TYPE = 'install'; /** * {@inheritDoc} @@ -65,12 +35,4 @@ class InstallOperation implements OperationInterface { return ($lock ? 'Locking ' : 'Installing ').''.$package->getPrettyName().' ('.$package->getFullPrettyVersion().')'; } - - /** - * {@inheritDoc} - */ - public function __toString() - { - return $this->show(false); - } } diff --git a/src/Composer/DependencyResolver/Operation/MarkAliasInstalledOperation.php b/src/Composer/DependencyResolver/Operation/MarkAliasInstalledOperation.php index 9e3a9f17d..5a3378e3e 100644 --- a/src/Composer/DependencyResolver/Operation/MarkAliasInstalledOperation.php +++ b/src/Composer/DependencyResolver/Operation/MarkAliasInstalledOperation.php @@ -20,9 +20,9 @@ use Composer\Package\PackageInterface; * * @author Nils Adermann */ -class MarkAliasInstalledOperation implements OperationInterface +class MarkAliasInstalledOperation extends Operation implements OperationInterface { - protected $package; + const TYPE = 'markAliasInstalled'; /** * Initializes operation. @@ -31,27 +31,7 @@ class MarkAliasInstalledOperation implements OperationInterface */ public function __construct(AliasPackage $package) { - $this->package = $package; - } - - /** - * Returns package instance. - * - * @return PackageInterface - */ - public function getPackage() - { - return $this->package; - } - - /** - * Returns operation type. - * - * @return string - */ - public function getOperationType() - { - return 'markAliasInstalled'; + parent::__construct($package); } /** @@ -61,12 +41,4 @@ class MarkAliasInstalledOperation implements OperationInterface { return 'Marking '.$this->package->getPrettyName().' ('.$this->package->getFullPrettyVersion().') as installed, alias of '.$this->package->getAliasOf()->getPrettyName().' ('.$this->package->getAliasOf()->getFullPrettyVersion().')'; } - - /** - * {@inheritDoc} - */ - public function __toString() - { - return $this->show(false); - } } diff --git a/src/Composer/DependencyResolver/Operation/MarkAliasUninstalledOperation.php b/src/Composer/DependencyResolver/Operation/MarkAliasUninstalledOperation.php index 16ca62ad9..08a9e9087 100644 --- a/src/Composer/DependencyResolver/Operation/MarkAliasUninstalledOperation.php +++ b/src/Composer/DependencyResolver/Operation/MarkAliasUninstalledOperation.php @@ -20,9 +20,9 @@ use Composer\Package\PackageInterface; * * @author Nils Adermann */ -class MarkAliasUninstalledOperation implements OperationInterface +class MarkAliasUninstalledOperation extends Operation implements OperationInterface { - protected $package; + const TYPE = 'markAliasUninstalled'; /** * Initializes operation. @@ -31,27 +31,7 @@ class MarkAliasUninstalledOperation implements OperationInterface */ public function __construct(AliasPackage $package) { - $this->package = $package; - } - - /** - * Returns package instance. - * - * @return PackageInterface - */ - public function getPackage() - { - return $this->package; - } - - /** - * Returns operation type. - * - * @return string - */ - public function getOperationType() - { - return 'markAliasUninstalled'; + parent::__construct($package); } /** @@ -61,12 +41,4 @@ class MarkAliasUninstalledOperation implements OperationInterface { return 'Marking '.$this->package->getPrettyName().' ('.$this->package->getFullPrettyVersion().') as uninstalled, alias of '.$this->package->getAliasOf()->getPrettyName().' ('.$this->package->getAliasOf()->getFullPrettyVersion().')'; } - - /** - * {@inheritDoc} - */ - public function __toString() - { - return $this->show(false); - } } diff --git a/src/Composer/DependencyResolver/Operation/Operation.php b/src/Composer/DependencyResolver/Operation/Operation.php new file mode 100644 index 000000000..c7ccce803 --- /dev/null +++ b/src/Composer/DependencyResolver/Operation/Operation.php @@ -0,0 +1,68 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\DependencyResolver\Operation; + +use Composer\Package\PackageInterface; + +/** + * Abstract operation class. + * + * @author Aleksandr Bezpiatov + */ +abstract class Operation implements OperationInterface +{ + const TYPE = null; + + /** + * @var PackageInterface + */ + protected $package; + + /** + * Initializes operation. + * + * @param PackageInterface $package package instance + */ + public function __construct(PackageInterface $package) + { + $this->package = $package; + } + + /** + * Returns package instance. + * + * @return PackageInterface + */ + public function getPackage() + { + return $this->package; + } + + /** + * Returns operation type. + * + * @return string + */ + public function getOperationType() + { + return static::TYPE; + } + + /** + * {@inheritDoc} + */ + public function __toString() + { + return $this->show(false); + } +} diff --git a/src/Composer/DependencyResolver/Operation/UninstallOperation.php b/src/Composer/DependencyResolver/Operation/UninstallOperation.php index 4fb39a6f6..2f82389f5 100644 --- a/src/Composer/DependencyResolver/Operation/UninstallOperation.php +++ b/src/Composer/DependencyResolver/Operation/UninstallOperation.php @@ -19,39 +19,9 @@ use Composer\Package\PackageInterface; * * @author Konstantin Kudryashov */ -class UninstallOperation implements OperationInterface +class UninstallOperation extends Operation implements OperationInterface { - protected $package; - - /** - * Initializes operation. - * - * @param PackageInterface $package package instance - */ - public function __construct(PackageInterface $package) - { - $this->package = $package; - } - - /** - * Returns package instance. - * - * @return PackageInterface - */ - public function getPackage() - { - return $this->package; - } - - /** - * Returns operation type. - * - * @return string - */ - public function getOperationType() - { - return 'uninstall'; - } + const TYPE = 'uninstall'; /** * {@inheritDoc} @@ -65,12 +35,4 @@ class UninstallOperation implements OperationInterface { return 'Removing '.$package->getPrettyName().' ('.$package->getFullPrettyVersion().')'; } - - /** - * {@inheritDoc} - */ - public function __toString() - { - return $this->show(false); - } } diff --git a/src/Composer/DependencyResolver/Operation/UpdateOperation.php b/src/Composer/DependencyResolver/Operation/UpdateOperation.php index 9e2fa99e5..c73e6166d 100644 --- a/src/Composer/DependencyResolver/Operation/UpdateOperation.php +++ b/src/Composer/DependencyResolver/Operation/UpdateOperation.php @@ -20,8 +20,10 @@ use Composer\Package\Version\VersionParser; * * @author Konstantin Kudryashov */ -class UpdateOperation implements OperationInterface +class UpdateOperation extends Operation implements OperationInterface { + const TYPE = 'update'; + protected $initialPackage; protected $targetPackage; @@ -34,7 +36,7 @@ class UpdateOperation implements OperationInterface public function __construct(PackageInterface $initial, PackageInterface $target) { $this->initialPackage = $initial; - $this->targetPackage = $target; + parent::__construct($target); } /** @@ -54,17 +56,7 @@ class UpdateOperation implements OperationInterface */ public function getTargetPackage() { - return $this->targetPackage; - } - - /** - * Returns operation type. - * - * @return string - */ - public function getOperationType() - { - return 'update'; + return $this->getPackage(); } /** @@ -92,12 +84,4 @@ class UpdateOperation implements OperationInterface return $actionName.' '.$initialPackage->getPrettyName().' ('.$fromVersion.' => '.$toVersion.')'; } - - /** - * {@inheritDoc} - */ - public function __toString() - { - return $this->show(false); - } } From 140de5480d7a5c2e0415ae409dc44b1f3b7307a3 Mon Sep 17 00:00:00 2001 From: Bezpiatov Date: Wed, 14 Oct 2020 17:58:39 +0300 Subject: [PATCH 2/3] Fixed variable usage --- src/Composer/DependencyResolver/Operation/UpdateOperation.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Composer/DependencyResolver/Operation/UpdateOperation.php b/src/Composer/DependencyResolver/Operation/UpdateOperation.php index c73e6166d..d73f00d7a 100644 --- a/src/Composer/DependencyResolver/Operation/UpdateOperation.php +++ b/src/Composer/DependencyResolver/Operation/UpdateOperation.php @@ -25,7 +25,6 @@ class UpdateOperation extends Operation implements OperationInterface const TYPE = 'update'; protected $initialPackage; - protected $targetPackage; /** * Initializes update operation. @@ -64,7 +63,7 @@ class UpdateOperation extends Operation implements OperationInterface */ public function show($lock) { - return self::format($this->initialPackage, $this->targetPackage, $lock); + return self::format($this->initialPackage, $this->package, $lock); } public static function format(PackageInterface $initialPackage, PackageInterface $targetPackage, $lock = false) From 8d0596163cabca4f9729a6e92adc14f044777dae Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 14 Oct 2020 17:51:03 +0200 Subject: [PATCH 3/3] Rename abstract class back to the previously present SolverOperation, mark it internal, reintroduce some duplication --- .../Operation/InstallOperation.php | 22 ++++++++++++++- .../Operation/MarkAliasInstalledOperation.php | 20 ++++++++++---- .../MarkAliasUninstalledOperation.php | 20 ++++++++++---- .../{Operation.php => SolverOperation.php} | 27 +------------------ .../Operation/UninstallOperation.php | 22 ++++++++++++++- .../Operation/UpdateOperation.php | 18 ++++++++----- 6 files changed, 85 insertions(+), 44 deletions(-) rename src/Composer/DependencyResolver/Operation/{Operation.php => SolverOperation.php} (60%) diff --git a/src/Composer/DependencyResolver/Operation/InstallOperation.php b/src/Composer/DependencyResolver/Operation/InstallOperation.php index 4e04485fb..6eefbd5bb 100644 --- a/src/Composer/DependencyResolver/Operation/InstallOperation.php +++ b/src/Composer/DependencyResolver/Operation/InstallOperation.php @@ -19,10 +19,30 @@ use Composer\Package\PackageInterface; * * @author Konstantin Kudryashov */ -class InstallOperation extends Operation implements OperationInterface +class InstallOperation extends SolverOperation implements OperationInterface { const TYPE = 'install'; + /** + * @var PackageInterface + */ + protected $package; + + public function __construct(PackageInterface $package) + { + $this->package = $package; + } + + /** + * Returns package instance. + * + * @return PackageInterface + */ + public function getPackage() + { + return $this->package; + } + /** * {@inheritDoc} */ diff --git a/src/Composer/DependencyResolver/Operation/MarkAliasInstalledOperation.php b/src/Composer/DependencyResolver/Operation/MarkAliasInstalledOperation.php index 5a3378e3e..4c70257a3 100644 --- a/src/Composer/DependencyResolver/Operation/MarkAliasInstalledOperation.php +++ b/src/Composer/DependencyResolver/Operation/MarkAliasInstalledOperation.php @@ -20,18 +20,28 @@ use Composer\Package\PackageInterface; * * @author Nils Adermann */ -class MarkAliasInstalledOperation extends Operation implements OperationInterface +class MarkAliasInstalledOperation extends SolverOperation implements OperationInterface { const TYPE = 'markAliasInstalled'; /** - * Initializes operation. - * - * @param AliasPackage $package package instance + * @var AliasPackage */ + protected $package; + public function __construct(AliasPackage $package) { - parent::__construct($package); + $this->package = $package; + } + + /** + * Returns package instance. + * + * @return AliasPackage + */ + public function getPackage() + { + return $this->package; } /** diff --git a/src/Composer/DependencyResolver/Operation/MarkAliasUninstalledOperation.php b/src/Composer/DependencyResolver/Operation/MarkAliasUninstalledOperation.php index 08a9e9087..08ed93ac3 100644 --- a/src/Composer/DependencyResolver/Operation/MarkAliasUninstalledOperation.php +++ b/src/Composer/DependencyResolver/Operation/MarkAliasUninstalledOperation.php @@ -20,18 +20,28 @@ use Composer\Package\PackageInterface; * * @author Nils Adermann */ -class MarkAliasUninstalledOperation extends Operation implements OperationInterface +class MarkAliasUninstalledOperation extends SolverOperation implements OperationInterface { const TYPE = 'markAliasUninstalled'; /** - * Initializes operation. - * - * @param AliasPackage $package package instance + * @var AliasPackage */ + protected $package; + public function __construct(AliasPackage $package) { - parent::__construct($package); + $this->package = $package; + } + + /** + * Returns package instance. + * + * @return AliasPackage + */ + public function getPackage() + { + return $this->package; } /** diff --git a/src/Composer/DependencyResolver/Operation/Operation.php b/src/Composer/DependencyResolver/Operation/SolverOperation.php similarity index 60% rename from src/Composer/DependencyResolver/Operation/Operation.php rename to src/Composer/DependencyResolver/Operation/SolverOperation.php index c7ccce803..e1a78da84 100644 --- a/src/Composer/DependencyResolver/Operation/Operation.php +++ b/src/Composer/DependencyResolver/Operation/SolverOperation.php @@ -19,35 +19,10 @@ use Composer\Package\PackageInterface; * * @author Aleksandr Bezpiatov */ -abstract class Operation implements OperationInterface +abstract class SolverOperation implements OperationInterface { const TYPE = null; - /** - * @var PackageInterface - */ - protected $package; - - /** - * Initializes operation. - * - * @param PackageInterface $package package instance - */ - public function __construct(PackageInterface $package) - { - $this->package = $package; - } - - /** - * Returns package instance. - * - * @return PackageInterface - */ - public function getPackage() - { - return $this->package; - } - /** * Returns operation type. * diff --git a/src/Composer/DependencyResolver/Operation/UninstallOperation.php b/src/Composer/DependencyResolver/Operation/UninstallOperation.php index 2f82389f5..585fdb1e5 100644 --- a/src/Composer/DependencyResolver/Operation/UninstallOperation.php +++ b/src/Composer/DependencyResolver/Operation/UninstallOperation.php @@ -19,10 +19,30 @@ use Composer\Package\PackageInterface; * * @author Konstantin Kudryashov */ -class UninstallOperation extends Operation implements OperationInterface +class UninstallOperation extends SolverOperation implements OperationInterface { const TYPE = 'uninstall'; + /** + * @var PackageInterface + */ + protected $package; + + public function __construct(PackageInterface $package) + { + $this->package = $package; + } + + /** + * Returns package instance. + * + * @return PackageInterface + */ + public function getPackage() + { + return $this->package; + } + /** * {@inheritDoc} */ diff --git a/src/Composer/DependencyResolver/Operation/UpdateOperation.php b/src/Composer/DependencyResolver/Operation/UpdateOperation.php index d73f00d7a..543c8e783 100644 --- a/src/Composer/DependencyResolver/Operation/UpdateOperation.php +++ b/src/Composer/DependencyResolver/Operation/UpdateOperation.php @@ -20,22 +20,28 @@ use Composer\Package\Version\VersionParser; * * @author Konstantin Kudryashov */ -class UpdateOperation extends Operation implements OperationInterface +class UpdateOperation extends SolverOperation implements OperationInterface { const TYPE = 'update'; + /** + * @var PackageInterface + */ protected $initialPackage; /** - * Initializes update operation. - * + * @var PackageInterface + */ + protected $targetPackage; + + /** * @param PackageInterface $initial initial package * @param PackageInterface $target target package (updated) */ public function __construct(PackageInterface $initial, PackageInterface $target) { $this->initialPackage = $initial; - parent::__construct($target); + $this->targetPackage = $target; } /** @@ -55,7 +61,7 @@ class UpdateOperation extends Operation implements OperationInterface */ public function getTargetPackage() { - return $this->getPackage(); + return $this->targetPackage; } /** @@ -63,7 +69,7 @@ class UpdateOperation extends Operation implements OperationInterface */ public function show($lock) { - return self::format($this->initialPackage, $this->package, $lock); + return self::format($this->initialPackage, $this->targetPackage, $lock); } public static function format(PackageInterface $initialPackage, PackageInterface $targetPackage, $lock = false)