1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * This file is part of the league/commonmark package. |
||
7 | * |
||
8 | * (c) Colin O'Dell <[email protected]> |
||
9 | * |
||
10 | * For the full copyright and license information, please view the LICENSE |
||
11 | * file that was distributed with this source code. |
||
12 | */ |
||
13 | |||
14 | namespace League\CommonMark\Node; |
||
15 | |||
16 | use League\CommonMark\Node\Block\AbstractBlock; |
||
17 | |||
18 | /** |
||
19 | * @implements \IteratorAggregate<int, Node> |
||
20 | */ |
||
21 | final class NodeIterator implements \IteratorAggregate |
||
22 | { |
||
23 | public const FLAG_BLOCKS_ONLY = 1; |
||
24 | |||
25 | private Node $node; |
||
26 | private bool $blocksOnly; |
||
27 | |||
28 | 548 | public function __construct(Node $node, int $flags = 0) |
|
29 | { |
||
30 | 548 | $this->node = $node; |
|
31 | 548 | $this->blocksOnly = ($flags & self::FLAG_BLOCKS_ONLY) === self::FLAG_BLOCKS_ONLY; |
|
32 | } |
||
33 | |||
34 | /** |
||
35 | * @return \Generator<int, Node> |
||
36 | */ |
||
37 | 548 | public function getIterator(): \Generator |
|
38 | { |
||
39 | 548 | $stack = [$this->node]; |
|
40 | 548 | $index = 0; |
|
41 | |||
42 | 548 | while ($stack) { |
|
0 ignored issues
–
show
|
|||
43 | 548 | $node = \array_pop($stack); |
|
44 | |||
45 | 548 | yield $index++ => $node; |
|
46 | |||
47 | // Push all children onto the stack in reverse order |
||
48 | 546 | $child = $node->lastChild(); |
|
49 | 546 | while ($child !== null) { |
|
50 | 536 | if (! $this->blocksOnly || $child instanceof AbstractBlock) { |
|
51 | 536 | $stack[] = $child; |
|
52 | } |
||
53 | |||
54 | 536 | $child = $child->previous(); |
|
55 | } |
||
56 | } |
||
57 | } |
||
58 | } |
||
59 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.