From 9a5121ed2737eff1902e805365340201c55fc12b Mon Sep 17 00:00:00 2001 From: Bezpiatov Date: Wed, 14 Oct 2020 17:56:42 +0300 Subject: [PATCH] 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); - } }