Iterator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 25
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getIterator() 0 3 1
A toArray() 0 13 4
1
<?php
2
/*
3
 * This file is part of the Scrawler package.
4
 *
5
 * (c) Pranjal Pandey <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Scrawler\Arca\Traits\Model;
12
13
use Scrawler\Arca\Collection;
14
use Scrawler\Arca\Model;
15
16
/**
17
 * Iterator trait to provide iterator methods to the model.
18
 */
19
trait Iterator
20
{
21
    /**
22
     * Get all properties in array form.
23
     *
24
     * @return array<mixed>
25
     */
26
    public function toArray(): array
27
    {
28
        $props = $this->getProperties();
0 ignored issues
show
Bug introduced by
It seems like getProperties() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
        /** @scrutinizer ignore-call */ 
29
        $props = $this->getProperties();
Loading history...
29
        foreach ($props as $key => $value) {
30
            if ($value instanceof Model) {
31
                $props[$key] = $value->toArray();
32
            }
33
            if ($value instanceof Collection) {
34
                $props[$key] = $value->toArray();
35
            }
36
        }
37
38
        return $props;
39
    }
40
41
    public function getIterator(): \Traversable
42
    {
43
        return new \ArrayIterator($this->toArray());
44
    }
45
}
46