From 6fd3b1a0520e3e347b8e8c4207a09db177f7e9c5 Mon Sep 17 00:00:00 2001 From: CoreShop Date: Mon, 27 Jan 2025 02:42:52 +0000 Subject: [PATCH] [CS] Refactor --- .../Component/Pimcore/PriorityQueue.php | 66 +++++++++++-------- .../Component/Pimcore/SplPriorityQueue.php | 30 ++++----- .../CompositeShippableCarrierValidator.php | 2 +- .../Context/CompositeStorageListContext.php | 2 +- .../Store/Context/CompositeStoreContext.php | 2 +- .../RequestBased/CompositeRequestResolver.php | 2 +- 6 files changed, 58 insertions(+), 46 deletions(-) diff --git a/src/CoreShop/Component/Pimcore/PriorityQueue.php b/src/CoreShop/Component/Pimcore/PriorityQueue.php index f66ab555fa..3309e80ced 100644 --- a/src/CoreShop/Component/Pimcore/PriorityQueue.php +++ b/src/CoreShop/Component/Pimcore/PriorityQueue.php @@ -18,17 +18,16 @@ namespace CoreShop\Component\Pimcore; +use function array_map; +use function count; use Countable; +use function is_array; use IteratorAggregate; use ReturnTypeWillChange; use Serializable; -use UnexpectedValueException; - -use function array_map; -use function count; -use function is_array; use function serialize; use function sprintf; +use UnexpectedValueException; use function unserialize; /** @@ -45,13 +44,16 @@ * * @template TValue * @template TPriority of int + * * @implements IteratorAggregate */ class PriorityQueue implements Countable, IteratorAggregate, Serializable { - public const EXTR_DATA = 0x00000001; + public const EXTR_DATA = 0x00000001; + public const EXTR_PRIORITY = 0x00000002; - public const EXTR_BOTH = 0x00000003; + + public const EXTR_BOTH = 0x00000003; /** * Inner queue class to use for iteration @@ -82,17 +84,19 @@ class PriorityQueue implements Countable, IteratorAggregate, Serializable * * @param TValue $data * @param TPriority $priority + * * @return $this */ public function insert($data, $priority = 1) { /** @psalm-var TPriority $priority */ - $priority = (int) $priority; + $priority = (int) $priority; $this->items[] = [ - 'data' => $data, + 'data' => $data, 'priority' => $priority, ]; $this->getQueue()->insert($data, $priority); + return $this; } @@ -114,10 +118,11 @@ public function insert($data, $priority = 1) public function remove(mixed $datum) { $found = false; - $key = null; + $key = null; foreach ($this->items as $key => $item) { if ($item['data'] === $datum) { $found = true; + break; } } @@ -125,14 +130,16 @@ public function remove(mixed $datum) unset($this->items[$key]); $this->queue = null; - if (! $this->isEmpty()) { + if (!$this->isEmpty()) { $queue = $this->getQueue(); foreach ($this->items as $item) { $queue->insert($item['data'], $item['priority']); } } + return true; } + return false; } @@ -178,7 +185,7 @@ public function extract() { $value = $this->getQueue()->extract(); - $keyToRemove = null; + $keyToRemove = null; $highestPriority = null; foreach ($this->items as $key => $item) { if ($item['data'] !== $value) { @@ -187,7 +194,8 @@ public function extract() if (null === $highestPriority) { $highestPriority = $item['priority']; - $keyToRemove = $key; + $keyToRemove = $key; + continue; } @@ -196,7 +204,7 @@ public function extract() } $highestPriority = $item['priority']; - $keyToRemove = $key; + $keyToRemove = $key; } if ($keyToRemove !== null) { @@ -222,6 +230,7 @@ public function extract() public function getIterator() { $queue = $this->getQueue(); + return clone $queue; } @@ -251,20 +260,18 @@ public function __serialize() * Serialization format is compatible with {@link SplPriorityQueue} * * @param string $data - * @return void */ public function unserialize($data) { $toUnserialize = unserialize($data); - if (! is_array($toUnserialize)) { + if (!is_array($toUnserialize)) { throw new UnexpectedValueException(sprintf( 'Cannot deserialize %s instance; corrupt serialization data', - self::class + self::class, )); } /** @psalm-var list $toUnserialize */ - $this->__unserialize($toUnserialize); } @@ -272,7 +279,6 @@ public function unserialize($data) * Magic method used to rebuild an instance. * * @param list $data Data array. - * @return void */ public function __unserialize($data) { @@ -288,7 +294,9 @@ public function __unserialize($data) * the ability to return priorities or both data and priority. * * @param int $flag + * * @return array + * * @psalm-return ($flag is self::EXTR_BOTH * ? list * : $flag is self::EXTR_PRIORITY @@ -300,8 +308,8 @@ public function toArray($flag = self::EXTR_DATA) { return match ($flag) { self::EXTR_BOTH => $this->items, - self::EXTR_PRIORITY => array_map(static fn($item): int => $item['priority'], $this->items), - default => array_map(static fn($item): mixed => $item['data'], $this->items), + self::EXTR_PRIORITY => array_map(static fn ($item): int => $item['priority'], $this->items), + default => array_map(static fn ($item): mixed => $item['data'], $this->items), }; } @@ -312,12 +320,14 @@ public function toArray($flag = self::EXTR_DATA) * internal queue class. The class provided should extend SplPriorityQueue. * * @param class-string<\SplPriorityQueue> $class + * * @return $this */ public function setInternalQueueClass($class) { /** @psalm-suppress RedundantCastGivenDocblockType */ $this->queueClass = (string) $class; + return $this; } @@ -325,6 +335,7 @@ public function setInternalQueueClass($class) * Does the queue contain the given datum? * * @param TValue $datum + * * @return bool */ public function contains($datum) @@ -334,6 +345,7 @@ public function contains($datum) return true; } } + return false; } @@ -341,6 +353,7 @@ public function contains($datum) * Does the queue have an item with the given priority? * * @param TPriority $priority + * * @return bool */ public function hasPriority($priority) @@ -350,6 +363,7 @@ public function hasPriority($priority) return true; } } + return false; } @@ -357,7 +371,9 @@ public function hasPriority($priority) * Get the inner priority queue instance * * @throws \Exception + * * @return \SplPriorityQueue + * * @psalm-assert !null $this->queue */ protected function getQueue() @@ -368,10 +384,10 @@ protected function getQueue() /** @psalm-var \SplPriorityQueue $queue */ $this->queue = $queue; /** @psalm-suppress DocblockTypeContradiction */ - if (! $this->queue instanceof \SplPriorityQueue) { + if (!$this->queue instanceof \SplPriorityQueue) { throw new \Exception(sprintf( 'PriorityQueue expects an internal queue of type SplPriorityQueue; received "%s"', - $queue::class + $queue::class, )); } } @@ -381,8 +397,6 @@ protected function getQueue() /** * Add support for deep cloning - * - * @return void */ public function __clone() { @@ -390,4 +404,4 @@ public function __clone() $this->queue = clone $this->queue; } } -} \ No newline at end of file +} diff --git a/src/CoreShop/Component/Pimcore/SplPriorityQueue.php b/src/CoreShop/Component/Pimcore/SplPriorityQueue.php index 39d3e1fa56..dd2039d3b2 100644 --- a/src/CoreShop/Component/Pimcore/SplPriorityQueue.php +++ b/src/CoreShop/Component/Pimcore/SplPriorityQueue.php @@ -18,19 +18,17 @@ namespace CoreShop\Component\Pimcore; -use ReturnTypeWillChange; -use Serializable; -use UnexpectedValueException; - use function array_key_exists; use function get_debug_type; use function is_array; +use const PHP_INT_MAX; +use ReturnTypeWillChange; +use Serializable; use function serialize; use function sprintf; +use UnexpectedValueException; use function unserialize; -use const PHP_INT_MAX; - /** * Serializable version of SplPriorityQueue * @@ -39,6 +37,7 @@ * * @template TValue * @template TPriority of int + * * @extends \SplPriorityQueue */ class SplPriorityQueue extends \SplPriorityQueue implements Serializable @@ -54,12 +53,11 @@ class SplPriorityQueue extends \SplPriorityQueue implements Serializable * * @param TValue $value * @param TPriority $priority - * @return void */ #[ReturnTypeWillChange] // Inherited return type should be bool public function insert($value, $priority) { - if (! is_array($priority)) { + if (!is_array($priority)) { $priority = [$priority, $this->serial--]; } @@ -79,6 +77,7 @@ public function toArray() foreach (clone $this as $item) { $array[] = $item; } + return $array; } @@ -106,6 +105,7 @@ public function __serialize() foreach ($clone as $item) { $data[] = $item; } + return $data; } @@ -113,15 +113,14 @@ public function __serialize() * Deserialize * * @param string $data - * @return void */ public function unserialize($data) { $toUnserialize = unserialize($data); - if (! is_array($toUnserialize)) { + if (!is_array($toUnserialize)) { throw new UnexpectedValueException(sprintf( 'Cannot deserialize %s instance; corrupt serialization data', - self::class + self::class, )); } @@ -132,25 +131,24 @@ public function unserialize($data) * Magic method used to rebuild an instance. * * @param array $data Data array. - * @return void */ public function __unserialize($data) { $this->serial = PHP_INT_MAX; foreach ($data as $item) { - if (! is_array($item)) { + if (!is_array($item)) { throw new UnexpectedValueException(sprintf( 'Cannot deserialize %s instance: corrupt item; expected array, received %s', self::class, - get_debug_type($item) + get_debug_type($item), )); } - if (! array_key_exists('data', $item)) { + if (!array_key_exists('data', $item)) { throw new UnexpectedValueException(sprintf( 'Cannot deserialize %s instance: corrupt item; missing "data" element', - self::class + self::class, )); } diff --git a/src/CoreShop/Component/Shipping/Validator/CompositeShippableCarrierValidator.php b/src/CoreShop/Component/Shipping/Validator/CompositeShippableCarrierValidator.php index ac898f3eff..1bdbea09aa 100644 --- a/src/CoreShop/Component/Shipping/Validator/CompositeShippableCarrierValidator.php +++ b/src/CoreShop/Component/Shipping/Validator/CompositeShippableCarrierValidator.php @@ -19,9 +19,9 @@ namespace CoreShop\Component\Shipping\Validator; use CoreShop\Component\Address\Model\AddressInterface; +use CoreShop\Component\Pimcore\PriorityQueue; use CoreShop\Component\Shipping\Model\CarrierInterface; use CoreShop\Component\Shipping\Model\ShippableInterface; -use CoreShop\Component\Pimcore\PriorityQueue; class CompositeShippableCarrierValidator implements ShippableCarrierValidatorInterface { diff --git a/src/CoreShop/Component/StorageList/Context/CompositeStorageListContext.php b/src/CoreShop/Component/StorageList/Context/CompositeStorageListContext.php index 0384e15d9f..dade0d6b85 100644 --- a/src/CoreShop/Component/StorageList/Context/CompositeStorageListContext.php +++ b/src/CoreShop/Component/StorageList/Context/CompositeStorageListContext.php @@ -18,8 +18,8 @@ namespace CoreShop\Component\StorageList\Context; -use CoreShop\Component\StorageList\Model\StorageListInterface; use CoreShop\Component\Pimcore\PriorityQueue; +use CoreShop\Component\StorageList\Model\StorageListInterface; class CompositeStorageListContext implements StorageListContextInterface { diff --git a/src/CoreShop/Component/Store/Context/CompositeStoreContext.php b/src/CoreShop/Component/Store/Context/CompositeStoreContext.php index a6cfb1107e..4bac282fd0 100644 --- a/src/CoreShop/Component/Store/Context/CompositeStoreContext.php +++ b/src/CoreShop/Component/Store/Context/CompositeStoreContext.php @@ -18,8 +18,8 @@ namespace CoreShop\Component\Store\Context; -use CoreShop\Component\Store\Model\StoreInterface; use CoreShop\Component\Pimcore\PriorityQueue; +use CoreShop\Component\Store\Model\StoreInterface; final class CompositeStoreContext implements StoreContextInterface { diff --git a/src/CoreShop/Component/Store/Context/RequestBased/CompositeRequestResolver.php b/src/CoreShop/Component/Store/Context/RequestBased/CompositeRequestResolver.php index faef62b9a7..57face1bbc 100644 --- a/src/CoreShop/Component/Store/Context/RequestBased/CompositeRequestResolver.php +++ b/src/CoreShop/Component/Store/Context/RequestBased/CompositeRequestResolver.php @@ -18,9 +18,9 @@ namespace CoreShop\Component\Store\Context\RequestBased; +use CoreShop\Component\Pimcore\PriorityQueue; use CoreShop\Component\Store\Context\StoreNotFoundException; use CoreShop\Component\Store\Model\StoreInterface; -use CoreShop\Component\Pimcore\PriorityQueue; use Symfony\Component\HttpFoundation\Request; final class CompositeRequestResolver implements RequestResolverInterface