Completed
Push — master ( aba493...5356ed )
by Ruud
315:38 queued 305:00
created

src/Kunstmaan/FixturesBundle/Parser/Parser.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\FixturesBundle\Parser;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Kunstmaan\FixturesBundle\Loader\Fixture;
7
use Kunstmaan\FixturesBundle\Parser\Property\PropertyParserInterface;
8
use Kunstmaan\FixturesBundle\Parser\Spec\SpecParserInterface;
9
10
class Parser
11
{
12
    /**
13
     * @var PropertyParserInterface[]
14
     */
15
    private $parsers;
16
17
    /**
18
     * @var SpecParserInterface[]
19
     */
20
    private $specParsers;
21
22
    public function __construct()
23
    {
24
        $this->parsers = new ArrayCollection();
25
        $this->specParsers = new ArrayCollection();
26
    }
27
28
    public function parseFixture(Fixture $fixture, $providers, $fixtures = [])
29
    {
30
        $entities = [];
31
        foreach ($fixtures as $key => $ref) {
32
            $entities[$key] = $ref->getEntity();
33
        }
34
35
        $fixture->setProperties($this->parseArray($fixture->getProperties(), $providers, $entities, [
36
            $fixture,
37
            'fixtures' => $fixtures,
38
        ]));
39
40
        $fixture->setParameters($this->parseArray($fixture->getParameters(), $providers, $fixtures, [
41
            $fixture,
42
            'fixtures' => $fixtures,
43
        ]));
44
45
        $fixture->setTranslations($this->parseArray($fixture->getTranslations(), $providers, $fixtures, [
46
            $fixture,
47
            'fixtures' => $fixtures,
48
        ]));
49
    }
50
51
    public function parseEntity($entity, $providers, $fixtures = [], $additional = [])
52
    {
53
        $refl = new \ReflectionClass($entity);
54
        $properties = $refl->getProperties();
55
56
        foreach ($properties as $property) {
57
            $property->setAccessible(true);
58
            $value = $property->getValue($entity);
59
60
            foreach ($this->parsers as $parser) {
61
                if ($parser->canParse($value)) {
62
                    $value = $parser->parse($value, $providers, $fixtures, $additional);
0 ignored issues
show
The call to PropertyParserInterface::parse() has too many arguments starting with $additional.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
63
                    $property->setValue($entity, $value);
64
                }
65
            }
66
        }
67
    }
68
69
    public function parseArray($array, $providers, $fixtures = [], $additional = [])
0 ignored issues
show
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
70
    {
71
        if (empty($array)) {
72
            return $array;
73
        }
74
75
        foreach ($array as $key => $item) {
76
            if (is_array($item)) {
77
                $array[$key] = $this->parseArray($item, $providers, $fixtures, $additional);
78
            } else {
79
                $array[$key] = $this->parse($item, $providers, $fixtures, $additional);
80
            }
81
        }
82
83
        return $array;
84
    }
85
86
    public function parse($value, $providers, $fixtures = [], $additional = [])
87
    {
88
        foreach ($this->parsers as $parser) {
89
            if ($parser->canParse($value)) {
90
                $value = $parser->parse($value, $providers, $fixtures, $additional);
0 ignored issues
show
The call to PropertyParserInterface::parse() has too many arguments starting with $additional.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
91
            }
92
        }
93
94
        return $value;
95
    }
96
97
    public function parseSpec($value, $fixture, $fixtures = [])
98
    {
99
        foreach ($this->specParsers as $parser) {
100
            if ($parser->canParse($value)) {
101
                return $parser->parse($fixture, $fixtures, $value);
102
            }
103
        }
104
105
        $fixtures[$value] = $fixture;
106
107
        return $fixtures;
108
    }
109
110
    public function addParser(PropertyParserInterface $parser, $alias)
111
    {
112
        $this->parsers->set($alias, $parser);
113
114
        return $this;
115
    }
116
117
    public function addSpecParser(SpecParserInterface $parser, $alias)
118
    {
119
        $this->specParsers->set($alias, $parser);
120
121
        return $this;
122
    }
123
}
124