Completed
Push — master ( 1f44ff...9311e4 )
by Emily
02:16
created

ReflectionPropertyFactory   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 168
Duplicated Lines 10.71 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 91.57%

Importance

Changes 0
Metric Value
wmc 22
c 0
b 0
f 0
lcom 1
cbo 7
dl 18
loc 168
ccs 76
cts 83
cp 0.9157
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fromName() 0 7 1
A build() 0 14 1
C setType() 18 72 14
B setConstruct() 0 59 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 <emily@emilyshepherd>
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
class ReflectionPropertyFactory extends ReflectorFactory
30
{
31
    const REFLECTION_OBJECT = ReflectionProperty::class;
32
33
    protected $acceptedParams =
34
    [
35
        'readable' => 'setBool',
36
        'writable' => 'setBool',
37
        'var' => 'setType',
38
        'construct' => 'setConstruct'
39
    ];
40
41
    public static function fromName($class, $property)
42
    {
43
        return new static(new PHPNativeReflectionProperty
44
        (
45
            $class, $property
46
        ));
47
    }
48
49 18
    public function build(ReflectionComposite $parent, $default)
50
    {
51 18
        $this->accessor->setRawValue('owner', $parent);
52 18
        $this->accessor->setRawValue('defaultValue', $default);
53 18
        $this->accessor->setRawValue
54
        (
55 18
            'name',
56 18
            $this->reflector->getName()
57
        );
58
59 18
        $this->parseDocComment();
60
61 18
        return $this->object;
62
    }
63
64 18
    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...
65
    {
66 18 View Code Duplication
        if ($value{0} !== '?')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
        {
68 18
            $nullable = false;
69
        }
70
        else
71
        {
72 16
            $nullable = true;
73 16
            $value = substr($value, 1);
74
        }
75
76 18 View Code Duplication
        if (substr($value, -2) !== '[]')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
        {
78 18
            $collection = false;
79
        }
80
        else
81
        {
82
            $collection = true;
83
            $value = substr($value, 0, -2);
84
        }
85
86
        switch ($value)
87
        {
88 18
            case 'string':
89 17
                $class = new StringType();
90 17
                break;
91 18
            case 'int':
92 18
            case 'integer':
93 1
                $class = new IntegerType();
94 1
                break;
95 18
            case 'bool':
96 18
            case 'boolean':
97 18
                $class = new BooleanType();
98 18
                break;
99 18
            case 'mixed':
100 18
            case '':
101
                $class = new MixedType();
102
                break;
103 18
            case 'null':
104
                $class = new NullType();
105
                break;
106
            default:
107
                $useStatements =
108 18
                    $this->object->owner->namespace->useStatements;
0 ignored issues
show
Documentation introduced by
The property owner does not exist on object<Spaark\CompositeU...l\Reflection\Reflector>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
109
110 18
                if ($useStatements->contains($value))
111
                {
112 17
                    $value = $useStatements[$value]->classname;
113
                }
114
                else
115
                {
116 18
                    $value = $this->object->owner->namespace->namespace
0 ignored issues
show
Documentation introduced by
The property owner does not exist on object<Spaark\CompositeU...l\Reflection\Reflector>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
117 18
                        . '\\' . $value;
118
                }
119
120 18
                $class = new ObjectType($value);
121
        }
122
123 18
        if ($nullable)
124
        {
125 16
            (new RawPropertyAccessor($class))
126 16
                ->setRawValue('nullable', true);
127
        }
128
129 18
        if ($collection)
130
        {
131
            $class = new CollectionType($class);
132
        }
133
134 18
        $this->accessor->setRawValue('type', $class);
135 18
    }
136
137 17
    public 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...
138
    {
139 17
        $value = explode(' ', $value);
140
        $compositeAccessor =
141 17
            new RawPropertyAccessor($this->object->owner);
0 ignored issues
show
Documentation introduced by
The property owner does not exist on object<Spaark\CompositeU...l\Reflection\Reflector>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
142
143 17
        switch ($value[0])
144
        {
145 17
            case 'required':
146 17
                $this->accessor->setRawValue
147
                (
148 17
                    'passedToConstructor',
149 17
                    true
150
                );
151 17
                $this->accessor->setRawValue
152
                (
153 17
                    'requiredInConstructor',
154 17
                    true
155
                );
156 17
                $compositeAccessor->rawAddToValue
157
                (
158 17
                    'requiredProperties',
159 17
                    $this->object
160
                );
161 17
                break;
162 17
            case 'new':
163 17
                $this->accessor->setRawValue
164
                (
165 17
                    'builtInConstructor',
166 17
                    true
167
                );
168 17
                $compositeAccessor->rawAddToValue
169
                (
170 17
                    'builtProperties',
171 17
                    $this->object
172
                );
173 17
                break;
174 17
            case 'optional':
175 17
                $this->accessor->setRawValue
176
                (
177 17
                    'passedToConstructor',
178 17
                    true
179
                );
180 17
                $compositeAccessor->rawAddToValue
181
                (
182 17
                    'optionalProperties',
183 17
                    $this->object
184
                );
185
186 17
                if (isset($value[1]) && $value[1] === 'new')
187
                {
188 17
                    $this->accessor->setRawValue
189
                    (
190 17
                        'builtInConstructor',
191 17
                        true
192
                    );
193
                }
194
        }
195 17
    }
196
}
197
198