Skip to content

Commit

Permalink
Merge pull request neos#4629 from neos/iteratorRefactoring
Browse files Browse the repository at this point in the history
Iterator refactoring
  • Loading branch information
nezaniel authored Oct 20, 2023
2 parents a6d265d + 02fc7d0 commit 3deee07
Show file tree
Hide file tree
Showing 39 changed files with 183 additions and 264 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ public static function fromArray(array $array): self
public static function fromCollection(
NodeAggregateIdCollection $collection
): self {
return new self(
$collection->getIterator()->getArrayCopy()
);
return new self(iterator_to_array($collection));
}

public function add(
Expand Down Expand Up @@ -99,8 +97,11 @@ public function isEmpty(): bool
return count($this->ids) === 0;
}

/**
* @return \Traversable<string,NodeAggregateId>
*/
public function getIterator(): \Traversable
{
return new \ArrayIterator($this->ids);
yield from $this->ids;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,23 @@
/**
* The node relation anchor points value object collection
*
* @implements \IteratorAggregate<int,NodeRelationAnchorPoint>
* @implements \IteratorAggregate<NodeRelationAnchorPoint>
* @internal
*/
final class NodeRelationAnchorPoints implements \IteratorAggregate, \Countable
final readonly class NodeRelationAnchorPoints implements \IteratorAggregate, \Countable
{
/**
* @var array<int,NodeRelationAnchorPoint>
* @var array<NodeRelationAnchorPoint>
*/
public readonly array $nodeRelationAnchorPoints;
public array $nodeRelationAnchorPoints;

public function __construct(NodeRelationAnchorPoint ...$nodeRelationAnchorPoints)
{
/** @var array<int,NodeRelationAnchorPoint> $nodeRelationAnchorPoints */
$this->nodeRelationAnchorPoints = $nodeRelationAnchorPoints;
}

/**
* @param array<int|string,string|NodeRelationAnchorPoint> $array
* @param array<string|NodeRelationAnchorPoint> $array
*/
public static function fromArray(array $array): self
{
Expand All @@ -46,8 +45,6 @@ public static function fromArray(array $array): self
$values[] = $item;
}
}
/** @var array<int,NodeRelationAnchorPoint $values */

return new self(...$values);
}

Expand Down Expand Up @@ -103,11 +100,11 @@ public function remove(NodeRelationAnchorPoint $nodeRelationAnchorPoint): self
}

/**
* @return \ArrayIterator<int,NodeRelationAnchorPoint>
* @return \Traversable<NodeRelationAnchorPoint>
*/
public function getIterator(): \ArrayIterator
public function getIterator(): \Traversable
{
return new \ArrayIterator($this->nodeRelationAnchorPoints);
yield from $this->nodeRelationAnchorPoints;
}

public function count(): int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected function initializeDimensions(): void
$this->contentDimensions[$rawDimensionId] = new ContentDimension(
$dimensionId,
new ContentDimensionValues($values),
new ContentDimensionValueVariationEdges($variationEdges),
new ContentDimensionValueVariationEdges(...$variationEdges),
$additionalConfiguration
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ public static function createEmpty(): self
}

/**
* @return \ArrayIterator<string,ContentDimensionConstraints>|ContentDimensionConstraints[]
* @return \Traversable<string,ContentDimensionConstraints>
*/
public function getIterator(): \ArrayIterator
public function getIterator(): \Traversable
{
return new \ArrayIterator($this->constraints);
yield from $this->constraints;
}

public function getConstraints(ContentDimensionId $dimensionId): ?ContentDimensionConstraints
Expand All @@ -72,8 +72,7 @@ public function allowsCombinationWith(
ContentDimensionId $contentDimensionId,
ContentDimensionValue $contentDimensionValue
): bool {
return isset($this->constraints[$contentDimensionId->value])
? $this->constraints[$contentDimensionId->value]->allowsCombinationWith($contentDimensionValue)
: true;
return !isset($this->constraints[$contentDimensionId->value])
|| $this->constraints[$contentDimensionId->value]->allowsCombinationWith($contentDimensionValue);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the Neos.ContentRepository.DimensionSpace package.
* This file is part of the Neos.ContentRepository.Core package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
Expand All @@ -17,50 +17,32 @@
/**
* A set of content dimension value variation edges
*
* @implements \IteratorAggregate<int,ContentDimensionValueVariationEdge>
* @implements \IteratorAggregate<ContentDimensionValueVariationEdge>
* @internal
*/
final class ContentDimensionValueVariationEdges implements \IteratorAggregate
{
/**
* @var array<int,ContentDimensionValueVariationEdge>
* @var array<ContentDimensionValueVariationEdge>
*/
private array $edges;

/**
* @var \ArrayIterator<int,ContentDimensionValueVariationEdge>
*/
private \ArrayIterator $iterator;

/**
* @param array<int,ContentDimensionValueVariationEdge> $array
*/
public function __construct(array $array)
public function __construct(ContentDimensionValueVariationEdge ...$edges)
{
foreach ($array as $edge) {
if (!$edge instanceof ContentDimensionValueVariationEdge) {
throw new \InvalidArgumentException(
'ContentDimensionValueVariationEdges may only contain ContentDimensionValueVariationEdge objects',
1639661280
);
}
}

$this->edges = $array;
$this->iterator = new \ArrayIterator($this->edges);
$this->edges = $edges;
}

public static function createEmpty(): self
{
return new self([]);
return new self();
}

/**
* @return \ArrayIterator<int,ContentDimensionValueVariationEdge>|ContentDimensionValueVariationEdge[]
* @return \Traversable<ContentDimensionValueVariationEdge>
*/
public function getIterator(): \ArrayIterator
public function getIterator(): \Traversable
{
return $this->iterator;
yield from $this->edges;
}

public function isEmpty(): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
* @implements \IteratorAggregate<string,ContentDimensionValue>
* @api because used as return value of Dimension Eel helper
*/
final class ContentDimensionValues implements \IteratorAggregate
final readonly class ContentDimensionValues implements \IteratorAggregate
{
/**
* The actual values, indexed by string representation
* @var array<string,ContentDimensionValue>
*/
public readonly array $values;
public array $values;

public readonly ContentDimensionValueSpecializationDepth $maximumDepth;
public ContentDimensionValueSpecializationDepth $maximumDepth;

/**
* @param array<string,ContentDimensionValue> $values
Expand Down Expand Up @@ -60,11 +60,11 @@ public function __construct(array $values)
}

/**
* @return \ArrayIterator<string,ContentDimensionValue>|ContentDimensionValue[]
* @return \Traversable<string,ContentDimensionValue>
*/
public function getIterator(): \ArrayIterator
public function getIterator(): \Traversable
{
return new \ArrayIterator($this->values);
yield from $this->values;
}

public function getValue(string $value): ?ContentDimensionValue
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the Neos.ContentRepository.DimensionSpace package.
* This file is part of the Neos.ContentRepository.Core package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
Expand All @@ -22,7 +22,7 @@
* @implements \ArrayAccess<string,DimensionSpacePoint>
* @api
*/
final class DimensionSpacePointSet implements
final readonly class DimensionSpacePointSet implements
\JsonSerializable,
\IteratorAggregate,
\ArrayAccess,
Expand All @@ -31,12 +31,7 @@ final class DimensionSpacePointSet implements
/**
* @var array<string,DimensionSpacePoint>
*/
public readonly array $points;

/**
* @var \ArrayIterator<string,DimensionSpacePoint>
*/
public readonly \ArrayIterator $iterator;
public array $points;

/**
* @param array<string|int,DimensionSpacePoint|array<string,string>> $pointCandidates
Expand All @@ -56,7 +51,6 @@ public function __construct(array $pointCandidates)
$points[$pointCandidate->hash] = $pointCandidate;
}
$this->points = $points;
$this->iterator = new \ArrayIterator($this->points);
}

/**
Expand Down Expand Up @@ -150,11 +144,11 @@ public function equals(DimensionSpacePointSet $other): bool
}

/**
* @return \ArrayIterator<string,DimensionSpacePoint>|DimensionSpacePoint[]
* @return \Traversable<string,DimensionSpacePoint>
*/
public function getIterator(): \ArrayIterator
public function getIterator(): \Traversable
{
return $this->iterator;
yield from $this->points;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Neos\ContentRepository\Core\DimensionSpace;

/*
* This file is part of the Neos.ContentRepository package.
* This file is part of the Neos.ContentRepository.Core package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
Expand All @@ -31,11 +31,6 @@ final class OriginDimensionSpacePointSet implements \JsonSerializable, \Iterator
*/
private array $points;

/**
* @var \ArrayIterator<string,OriginDimensionSpacePoint>
*/
private \ArrayIterator $iterator;

/**
* @param array<int|string,OriginDimensionSpacePoint|array<string,string>> $points
*/
Expand All @@ -55,7 +50,6 @@ public function __construct(array $points)
}
$this->points[$point->hash] = $point;
}
$this->iterator = new \ArrayIterator($this->points);
}

public static function fromDimensionSpacePointSet(DimensionSpacePointSet $dimensionSpacePointSet): self
Expand Down Expand Up @@ -135,11 +129,11 @@ public function count(): int
}

/**
* @return \ArrayIterator<string,OriginDimensionSpacePoint>
* @return \Traversable<string,OriginDimensionSpacePoint>
*/
public function getIterator(): \ArrayIterator
public function getIterator(): \Traversable
{
return $this->iterator;
yield from $this->points;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion Neos.ContentRepository.Core/Classes/EventStore/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ public static function fromArray(array $events): self
return new self(...$events);
}

/**
* @return \Traversable<EventInterface|DecoratedEvent>
*/
public function getIterator(): \Traversable
{
return new \ArrayIterator($this->events);
yield from $this->events;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

/**
* @implements \IteratorAggregate<string,NodeReferenceSnapshot>
* @internal not yet finished
* @internal todo not yet finished
*/
final class NodeReferencesSnapshot implements \IteratorAggregate, \Countable, \JsonSerializable
{
Expand All @@ -27,18 +27,12 @@ final class NodeReferencesSnapshot implements \IteratorAggregate, \Countable, \J
*/
private array $references;

/**
* @var \ArrayIterator<string,NodeReferenceSnapshot>
*/
protected \ArrayIterator $iterator;

/**
* @param array<string,NodeReferenceSnapshot> $references
*/
private function __construct(array $references)
{
$this->references = $references;
$this->iterator = new \ArrayIterator($references);
}

public function merge(self $other): self
Expand Down Expand Up @@ -80,6 +74,7 @@ public static function fromArray(array $nodeReferences): self

/**
* @todo what is this supposed to do?
* Good question.
*/
public static function fromReferences(References $nodeReferences): self
{
Expand All @@ -89,11 +84,11 @@ public static function fromReferences(References $nodeReferences): self
}

/**
* @return \ArrayIterator<string,NodeReferenceSnapshot>
* @return \Traversable<string,NodeReferenceSnapshot>
*/
public function getIterator(): \ArrayIterator
public function getIterator(): \Traversable
{
return $this->iterator;
yield from $this->references;
}

public function count(): int
Expand Down
Loading

0 comments on commit 3deee07

Please sign in to comment.