Completed
Push — master ( 79f24a...4b5497 )
by Emily
02:16
created

ReflectionPropertyFactory::fromName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 2
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 26
    public function build(ReflectionComposite $parent, $default)
72
    {
73 26
        $this->accessor->setRawValue('owner', $parent);
74 26
        $this->accessor->setRawValue('defaultValue', $default);
75 26
        $this->accessor->setRawValue
76
        (
77 26
            'name',
78 26
            $this->reflector->getName()
79
        );
80
81 26
        $this->parseDocComment
82
        ([
83 26
            'readable' => 'setBool',
84
            'writable' => 'setBool',
85
            'var' => 'setType',
86
            'construct' => 'setConstruct'
87
        ]);
88
89 26
        if (!$this->object->type)
90
        {
91
            $this->accessor->setRawValue('type', new MixedType());
92
        }
93
94 26
        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 26
    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 26
        $this->accessor->setRawValue
106
        (
107 26
            'type',
108 26
            (new TypeParser($this->object->owner))->parse($value)
109
        );
110 26
    }
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 25
    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 25
        $value = explode(' ', $value);
122
        $compositeAccessor =
123 25
            new RawPropertyAccessor($this->object->owner);
124
125 25
        switch ($value[0])
126
        {
127 25
            case 'required':
128 25
                $this->accessor->setRawValue
129
                (
130 25
                    'passedToConstructor',
131 25
                    true
132
                );
133 25
                $this->accessor->setRawValue
134
                (
135 25
                    'requiredInConstructor',
136 25
                    true
137
                );
138 25
                $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 25
                    'requiredProperties',
141 25
                    $this->object
142
                );
143 25
                break;
144 24
            case 'new':
145 24
                $this->accessor->setRawValue
146
                (
147 24
                    'builtInConstructor',
148 24
                    true
149
                );
150 24
                $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 24
                    'builtProperties',
153 24
                    $this->object
154
                );
155 24
                break;
156 24
            case 'optional':
157 24
                $this->accessor->setRawValue
158
                (
159 24
                    'passedToConstructor',
160 24
                    true
161
                );
162 24
                $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 24
                    'optionalProperties',
165 24
                    $this->object
166
                );
167
168 24
                if (isset($value[1]) && $value[1] === 'new')
169
                {
170 24
                    $this->accessor->setRawValue
171
                    (
172 24
                        'builtInConstructor',
173 24
                        true
174
                    );
175
                }
176
        }
177 25
    }
178
}
179
180