ReflectionPropertyFactory   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 92.59%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 0
loc 147
ccs 50
cts 54
cp 0.9259
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B build() 0 25 2
A setType() 0 8 1
B setConstruct() 0 59 6
A fromName() 0 7 1
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 21
    public function build(ReflectionComposite $parent, $default)
72
    {
73 21
        $this->accessor->setRawValue('owner', $parent);
74 21
        $this->accessor->setRawValue('defaultValue', $default);
75 21
        $this->accessor->setRawValue
76
        (
77 21
            'name',
78 21
            $this->reflector->getName()
79
        );
80
81 21
        $this->parseDocComment
82
        ([
83 21
            'readable' => 'setBool',
84
            'writable' => 'setBool',
85
            'var' => 'setType',
86
            'construct' => 'setConstruct'
87
        ]);
88
89 21
        if (!$this->object->type)
90
        {
91
            $this->accessor->setRawValue('type', new MixedType());
92
        }
93
94 21
        return $this->object;
95
    }
96
97
    /**
98
     * Sets the property's type by parsing the @type annotation
99
     *
100
     * @param string $name Should be 'var'
101
     * @param string $value The value of the annotation
102
     */
103 21
    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...
104
    {
105 21
        $this->accessor->setRawValue
106
        (
107 21
            'type',
108 21
            (new TypeParser($this->object->owner))->parse($value)
109
        );
110 21
    }
111
112
    /**
113
     * Sets the property's constructor options by parsing the @construct
114
     * annotation
115
     *
116
     * @param string $name Should be 'construct'
117
     * @param string $value The value of the annotation
118
     */
119 20
    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...
120
    {
121 20
        $value = explode(' ', $value);
122
        $compositeAccessor =
123 20
            new RawPropertyAccessor($this->object->owner);
124
125 20
        switch ($value[0])
126
        {
127 20
            case 'required':
128 20
                $this->accessor->setRawValue
129
                (
130 20
                    'passedToConstructor',
131 20
                    true
132
                );
133 20
                $this->accessor->setRawValue
134
                (
135 20
                    'requiredInConstructor',
136 20
                    true
137
                );
138 20
                $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...
139
                (
140 20
                    'requiredProperties',
141 20
                    $this->object
142
                );
143 20
                break;
144 20
            case 'new':
145 20
                $this->accessor->setRawValue
146
                (
147 20
                    'builtInConstructor',
148 20
                    true
149
                );
150 20
                $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...
151
                (
152 20
                    'builtProperties',
153 20
                    $this->object
154
                );
155 20
                break;
156 20
            case 'optional':
157 20
                $this->accessor->setRawValue
158
                (
159 20
                    'passedToConstructor',
160 20
                    true
161
                );
162 20
                $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...
163
                (
164 20
                    'optionalProperties',
165 20
                    $this->object
166
                );
167
168 20
                if (isset($value[1]) && $value[1] === 'new')
169
                {
170 20
                    $this->accessor->setRawValue
171
                    (
172 20
                        'builtInConstructor',
173 20
                        true
174
                    );
175
                }
176
        }
177 20
    }
178
}
179
180