Skip to content

Commit

Permalink
Merge pull request #2786 from coreshop/coding-standard/refactor-next
Browse files Browse the repository at this point in the history
[CS] Refactor
  • Loading branch information
dpfaffenbauer authored Jan 27, 2025
2 parents ce749e9 + 6fd3b1a commit c2485cc
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 46 deletions.
66 changes: 40 additions & 26 deletions src/CoreShop/Component/Pimcore/PriorityQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -45,13 +44,16 @@
*
* @template TValue
* @template TPriority of int
*
* @implements IteratorAggregate<array-key, TValue>
*/
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
Expand Down Expand Up @@ -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;
}

Expand All @@ -114,25 +118,28 @@ 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;
}
}
if ($found && $key !== null) {
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;
}

Expand Down Expand Up @@ -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) {
Expand All @@ -187,7 +194,8 @@ public function extract()

if (null === $highestPriority) {
$highestPriority = $item['priority'];
$keyToRemove = $key;
$keyToRemove = $key;

continue;
}

Expand All @@ -196,7 +204,7 @@ public function extract()
}

$highestPriority = $item['priority'];
$keyToRemove = $key;
$keyToRemove = $key;
}

if ($keyToRemove !== null) {
Expand All @@ -222,6 +230,7 @@ public function extract()
public function getIterator()
{
$queue = $this->getQueue();

return clone $queue;
}

Expand Down Expand Up @@ -251,28 +260,25 @@ 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<array{data: TValue, priority: TPriority}> $toUnserialize */

$this->__unserialize($toUnserialize);
}

/**
* Magic method used to rebuild an instance.
*
* @param list<array{data: TValue, priority: TPriority}> $data Data array.
* @return void
*/
public function __unserialize($data)
{
Expand All @@ -288,7 +294,9 @@ public function __unserialize($data)
* the ability to return priorities or both data and priority.
*
* @param int $flag
*
* @return array<array-key, mixed>
*
* @psalm-return ($flag is self::EXTR_BOTH
* ? list<array{data: TValue, priority: TPriority}>
* : $flag is self::EXTR_PRIORITY
Expand All @@ -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),
};
}

Expand All @@ -312,19 +320,22 @@ 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;
}

/**
* Does the queue contain the given datum?
*
* @param TValue $datum
*
* @return bool
*/
public function contains($datum)
Expand All @@ -334,13 +345,15 @@ public function contains($datum)
return true;
}
}

return false;
}

/**
* Does the queue have an item with the given priority?
*
* @param TPriority $priority
*
* @return bool
*/
public function hasPriority($priority)
Expand All @@ -350,14 +363,17 @@ public function hasPriority($priority)
return true;
}
}

return false;
}

/**
* Get the inner priority queue instance
*
* @throws \Exception
*
* @return \SplPriorityQueue<TPriority, TValue>
*
* @psalm-assert !null $this->queue
*/
protected function getQueue()
Expand All @@ -368,10 +384,10 @@ protected function getQueue()
/** @psalm-var \SplPriorityQueue<TPriority, TValue> $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,
));
}
}
Expand All @@ -381,13 +397,11 @@ protected function getQueue()

/**
* Add support for deep cloning
*
* @return void
*/
public function __clone()
{
if (null !== $this->queue) {
$this->queue = clone $this->queue;
}
}
}
}
30 changes: 14 additions & 16 deletions src/CoreShop/Component/Pimcore/SplPriorityQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -39,6 +37,7 @@
*
* @template TValue
* @template TPriority of int
*
* @extends \SplPriorityQueue<TPriority, TValue>
*/
class SplPriorityQueue extends \SplPriorityQueue implements Serializable
Expand All @@ -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--];
}

Expand All @@ -79,6 +77,7 @@ public function toArray()
foreach (clone $this as $item) {
$array[] = $item;
}

return $array;
}

Expand Down Expand Up @@ -106,22 +105,22 @@ public function __serialize()
foreach ($clone as $item) {
$data[] = $item;
}

return $data;
}

/**
* 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,
));
}

Expand All @@ -132,25 +131,24 @@ public function unserialize($data)
* Magic method used to rebuild an instance.
*
* @param array<array-key, mixed> $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,
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Loading

0 comments on commit c2485cc

Please sign in to comment.