-
-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add RecursiveIteratorAggregateIterator #56
add RecursiveIteratorAggregateIterator #56
Conversation
PR Summary
|
hey hi first contributor! Thanks for the PR! Can you please fix the static analysis issue and code style? Once it's done, I'll review it manually. Thanks again! |
$current = end($this->stack)->current(); | ||
|
||
yield $current; | ||
end($this->stack)->next(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
end()
is a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can simple have last $iterator
outside of a $stack
to avoid array operations
|
||
yield $current; | ||
end($this->stack)->next(); | ||
$this->stack[] = self::findIterator($current); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not need to have a static function self::findIterator
, let's remove the static from it.
public function getIterator(): Generator | ||
{ | ||
$iterator = $this->findIterator($this->input); | ||
$this->stack = []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep the object immutable, this should not happen
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can`t have state tracking (getDepth) on immutable iterator, we can add exception here
*/ | ||
public function __construct(private readonly Traversable $input) {} | ||
|
||
public function getDepth(): int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The getDepth
method is not part of the IteratorAggregate
interface, I would remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getDepth method is required for tree travel
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then it should be private.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It`s not used by iterator, its required to detect depth (or move direction) in loop. Like
foreach($it as $val){
echo str_repeat('-', $it->getDepth()).$val."\n";
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes I noticed it after your message. Since that method is not part of the interface, I would make it private.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or more common use case for xml/html/whatever
$prev_depth = 0;
foreach($it as $val){
$next_depth = $it->getDepth();
if ($prev_depth < $next_depth) echo "<tag>\n";
for($i=$prev_depth;$i>$next_depth;$i--) echo "</tag>\n";
echo $val;
$prev_depth = $next_depth;
}
for($i=$prev_depth;$i>0;$i--) echo "</tag>\n";
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private method cannot be called outside of the iterator class (including foreach loop). It`s simple useless
public function getIterator(): Generator | ||
{ | ||
if (null !== $this->stack) { | ||
throw new RuntimeException('Iterator already active '); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This cannot happen. This method always returns a new iterator, ready to be consumed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
foreach($it as $a){ //generator assign stack to iterator
foreach($it as $b){ //same iterator, new stack will replace original, throw exception
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are you trying to tell me here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exception will be thrown in this case
Does this PR is similar to #61 ? |
Yes, #61 has same logic, but for different structeres. this iterator provides recursive iteration over tree-like structure of objects implementing |
Since this pull request has not had any activity within the last 5 days, I have marked it as stale. |
One more missing iterator.
I made it to travel deep tree Traversable structure without RecursiveIterator interface