Completed
Push — master ( ba8ed9...770316 )
by Jeroen
06:11
created

Parser::addSpecParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
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();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Doctrine\Common\Collections\ArrayCollection() of type object<Doctrine\Common\C...ctions\ArrayCollection> is incompatible with the declared type array<integer,object<Kun...opertyParserInterface>> of property $parsers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
25
        $this->specParsers = new ArrayCollection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Doctrine\Common\Collections\ArrayCollection() of type object<Doctrine\Common\C...ctions\ArrayCollection> is incompatible with the declared type array<integer,object<Kun...c\SpecParserInterface>> of property $specParsers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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
Unused Code introduced by
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
Documentation introduced by
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
Unused Code introduced by
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);
0 ignored issues
show
Bug introduced by
The method set cannot be called on $this->parsers (of type array<integer,object<Kun...opertyParserInterface>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
113
114
        return $this;
115
    }
116
117
    public function addSpecParser(SpecParserInterface $parser, $alias)
118
    {
119
        $this->specParsers->set($alias, $parser);
0 ignored issues
show
Bug introduced by
The method set cannot be called on $this->specParsers (of type array<integer,object<Kun...c\SpecParserInterface>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
120
121
        return $this;
122
    }
123
}
124