Completed
Push — master ( b8da3f...f66325 )
by Emily
10s
created

ReflectionPropertyFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 142
ccs 49
cts 49
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fromName() 0 7 1
A build() 0 20 1
A setType() 0 8 1
B setConstruct() 0 59 6
1
<?php
2
/**
3
 * This file is part of the Composite Utils package.
4
 *
5
 * (c) Emily Shepherd <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the
8
 * LICENSE.md file that was distributed with this source code.
9
 *
10
 * @package spaark/composite-utils
11
 * @author Emily Shepherd <[email protected]>
12
 * @license MIT
13
 */
14
15
namespace Spaark\CompositeUtils\Factory\Reflection;
16
17
use Spaark\CompositeUtils\Model\Reflection\ReflectionComposite;
18
use Spaark\CompositeUtils\Model\Reflection\ReflectionProperty;
19
use Spaark\CompositeUtils\Model\Reflection\ReflectionParameter;
20
use Spaark\CompositeUtils\Model\Reflection\Type\BooleanType;
21
use Spaark\CompositeUtils\Model\Reflection\Type\CollectionType;
22
use Spaark\CompositeUtils\Model\Reflection\Type\IntegerType;
23
use Spaark\CompositeUtils\Model\Reflection\Type\MixedType;
24
use Spaark\CompositeUtils\Model\Reflection\Type\ObjectType;
25
use Spaark\CompositeUtils\Model\Reflection\Type\StringType;
26
use Spaark\CompositeUtils\Service\RawPropertyAccessor;
27
use \ReflectionProperty as PHPNativeReflectionProperty;
28
29
/**
30
 * Builds a ReflectionProperty for a given class and property name
31
 */
32
class ReflectionPropertyFactory extends ReflectorFactory
33
{
34
    const REFLECTION_OBJECT = ReflectionProperty::class;
35
36
    /**
37
     * @var PHPNativeReflectionProperty
38
     */
39
    protected $reflector;
40
41
    /**
42
     * @var ReflectionProperty
43
     */
44
    protected $object;
45
46
    /**
47
     * Returns a new ReflectionPropertyFactory using the given class and
48
     * property names
49
     *
50
     * @param string $class The classname of the property
51
     * @param string $property The property to reflect
52
     * @return ReflectionPropertyFactory
53
     */
54
    public static function fromName($class, $property)
55
    {
56
        return new static(new PHPNativeReflectionProperty
57
        (
58
            $class, $property
59
        ));
60
    }
61
62
    /**
63
     * Builds the ReflectionProperty from the provided parameters,
64
     * linking to a parent ReflectionComposite
65
     *
66
     * @param ReflectionCompostite $parent The reflector for the class
67
     *     this property belongs to
68
     * @param mixed $default This property's default value
69
     * @return ReflectionProperty
70
     */
71 20
    public function build(ReflectionComposite $parent, $default)
72
    {
73 20
        $this->accessor->setRawValue('owner', $parent);
74 20
        $this->accessor->setRawValue('defaultValue', $default);
75 20
        $this->accessor->setRawValue
76
        (
77 20
            'name',
78 20
            $this->reflector->getName()
79
        );
80
81 20
        $this->parseDocComment
82
        ([
83 20
            'readable' => 'setBool',
84
            'writable' => 'setBool',
85
            'var' => 'setType',
86
            'construct' => 'setConstruct'
87
        ]);
88
89 20
        return $this->object;
90
    }
91
92
    /**
93
     * Sets the property's type by parsing the @type annotation
94
     *
95
     * @param string $name Should be 'var'
96
     * @param string $value The value of the annotation
97
     */
98 20
    protected function setType($name, $value)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
99
    {
100 20
        $this->accessor->setRawValue
101
        (
102 20
            'type',
103 20
            (new TypeParser($this->object->owner))->parse($value)
104
        );
105 20
    }
106
107
    /**
108
     * Sets the property's constructor options by parsing the @construct
109
     * annotation
110
     *
111
     * @param string $name Should be 'construct'
112
     * @param string $value The value of the annotation
113
     */
114 19
    protected function setConstruct($name, $value)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
115
    {
116 19
        $value = explode(' ', $value);
117
        $compositeAccessor =
118 19
            new RawPropertyAccessor($this->object->owner);
119
120 19
        switch ($value[0])
121
        {
122 19
            case 'required':
123 19
                $this->accessor->setRawValue
124
                (
125 19
                    'passedToConstructor',
126 19
                    true
127
                );
128 19
                $this->accessor->setRawValue
129
                (
130 19
                    'requiredInConstructor',
131 19
                    true
132
                );
133 19
                $compositeAccessor->rawAddToValue
0 ignored issues
show
Deprecated Code introduced by
The method Spaark\CompositeUtils\Se...cessor::rawAddToValue() has been deprecated.

This method has been deprecated.

Loading history...
134
                (
135 19
                    'requiredProperties',
136 19
                    $this->object
137
                );
138 19
                break;
139 19
            case 'new':
140 19
                $this->accessor->setRawValue
141
                (
142 19
                    'builtInConstructor',
143 19
                    true
144
                );
145 19
                $compositeAccessor->rawAddToValue
0 ignored issues
show
Deprecated Code introduced by
The method Spaark\CompositeUtils\Se...cessor::rawAddToValue() has been deprecated.

This method has been deprecated.

Loading history...
146
                (
147 19
                    'builtProperties',
148 19
                    $this->object
149
                );
150 19
                break;
151 19
            case 'optional':
152 19
                $this->accessor->setRawValue
153
                (
154 19
                    'passedToConstructor',
155 19
                    true
156
                );
157 19
                $compositeAccessor->rawAddToValue
0 ignored issues
show
Deprecated Code introduced by
The method Spaark\CompositeUtils\Se...cessor::rawAddToValue() has been deprecated.

This method has been deprecated.

Loading history...
158
                (
159 19
                    'optionalProperties',
160 19
                    $this->object
161
                );
162
163 19
                if (isset($value[1]) && $value[1] === 'new')
164
                {
165 19
                    $this->accessor->setRawValue
166
                    (
167 19
                        'builtInConstructor',
168 19
                        true
169
                    );
170
                }
171
        }
172 19
    }
173
}
174
175