Completed
Push — master ( 1f9a78...118fca )
by Emily
02:07
created

ReflectionPropertyFactory   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 102
Duplicated Lines 17.65 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 84.78%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 7
dl 18
loc 102
ccs 39
cts 46
cp 0.8478
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fromName() 0 7 1
A build() 0 14 1
C setType() 18 67 14

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 licence 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
    ];
39
40
    public static function fromName($class, $property)
41
    {
42
        return new static(new PHPNativeReflectionProperty
43
        (
44
            $class, $property
45
        ));
46
    }
47
48 10
    public function build(ReflectionComposite $parent, $default)
49
    {
50 10
        $this->accessor->setRawValue('owner', $parent);
51 10
        $this->accessor->setRawValue('defaultValue', $default);
52 10
        $this->accessor->setRawValue
53
        (
54 10
            'name',
55 10
            $this->reflector->getName()
56
        );
57
58 10
        $this->parseDocComment();
59
60 10
        return $this->object;
61
    }
62
63 10
    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...
64
    {
65 10 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...
66
        {
67 10
            $nullable = false;
68
        }
69
        else
70
        {
71 9
            $nullable = true;
72 9
            $value = substr($value, 1);
73
        }
74
75 10 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...
76
        {
77 10
            $collection = false;
78
        }
79
        else
80
        {
81
            $collection = true;
82
            $value = substr($value, 0, -2);
83
        }
84
85
        switch ($value)
86
        {
87 10
            case 'string':
88 10
                $class = new StringType();
89 10
                break;
90 10
            case 'int':
91 10
            case 'integer':
92 1
                $class = new IntegerType();
93 1
                break;
94 10
            case 'bool':
95 10
            case 'boolean':
96 1
                $class = new BooleanType();
97 1
                break;
98 10
            case 'mixed':
99 10
            case '':
100
                $class = new MixedType();
101
                break;
102 10
            case 'null':
103
                $class = new NullType();
104
                break;
105
            default:
106
                $useStatements =
107 10
                    $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...
108
109 10
                if ($useStatements->contains($value))
110
                {
111 9
                    $value = $useStatements[$value]->classname;
112
                }
113
114 10
                $class = new ObjectType($value);
115
        }
116
117 10
        if ($nullable)
118
        {
119 9
            (new RawPropertyAccessor($class))
120 9
                ->setRawValue('nullable', true);
121
        }
122
123 10
        if ($collection)
124
        {
125
            $class = new CollectionType($class);
126
        }
127
128 10
        $this->accessor->setRawValue('type', $class);
129 10
    }
130
}
131
132