Iterator::toArray()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 13
rs 10
1
<?php
2
3
/*
4
 * This file is part of the Scrawler package.
5
 *
6
 * (c) Pranjal Pandey <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Scrawler\Arca\Traits\Model;
13
14
use Scrawler\Arca\Collection;
15
use Scrawler\Arca\Model;
16
17
/**
18
 * Iterator trait to provide iterator methods to the model.
19
 */
20
trait Iterator
21
{
22
    /**
23
     * Get all properties in array form.
24
     *
25
     * @return array<mixed>
26
     */
27
    public function toArray(): array
28
    {
29
        $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

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