Completed
Push — master ( e41e91...2b4213 )
by Aleh
01:47 queued 01:39
created

PropertiesCollection   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 68
Duplicated Lines 39.71 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 1
cbo 3
dl 27
loc 68
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A add() 0 4 1
B all() 10 26 5
B get() 17 24 5

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
namespace Padawan\Domain\Project\Collection;
4
5
use Padawan\Domain\Project\Node\ClassProperty;
6
use Padawan\Domain\Project\Node\ClassData;
7
8
class PropertiesCollection
9
{
10
    /**
11
     * @param ClassData $class
12
     */
13
    public function __construct($class)
14
    {
15
        $this->class = $class;
16
    }
17
    public function add(ClassProperty $prop)
18
    {
19
        $this->map[$prop->name] = $prop;
20
    }
21
    public function all(Specification $spec = null)
22
    {
23
        if ($spec === null) {
24
            $spec = new Specification;
25
        }
26
        $props = [];
27
        foreach ($this->map as $prop) {
28
            if (!$spec->satisfy($prop)) {
29
                continue;
30
            }
31
            $props[$prop->name] = $prop;
32
        }
33
        $parent = $this->class->getParent();
34 View Code Duplication
        if ($parent instanceof ClassData) {
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...
35
            $props = array_merge(
36
                $parent->properties->all(new Specification(
0 ignored issues
show
Documentation introduced by
The property $properties is declared private in Padawan\Domain\Project\Node\ClassData. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
37
                    $spec->getParentMode(),
38
                    $spec->isStatic(),
39
                    $spec->isMagic()
40
                )),
41
                $props
42
            );
43
        }
44
        ksort($props);
45
        return $props;
46
    }
47
    public function get($propName, Specification $spec = null)
48
    {
49
        if ($spec === null) {
50
            $spec = new Specification('private', 2, false);
0 ignored issues
show
Documentation introduced by
2 is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
51
        }
52 View Code Duplication
        if (array_key_exists($propName, $this->map)) {
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...
53
            $prop = $this->map[$propName];
54
            if ($spec->satisfy($prop)) {
55
                return $prop;
56
            }
57
            return null;
58
        }
59
        $parent = $this->class->getParent();
60 View Code Duplication
        if ($parent instanceof ClassData) {
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...
61
            return $parent->properties->get(
0 ignored issues
show
Documentation introduced by
The property $properties is declared private in Padawan\Domain\Project\Node\ClassData. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
62
                $propName,
63
                new Specification(
64
                    $spec->getParentMode(),
65
                    $spec->isStatic(),
66
                    $spec->isMagic()
67
                )
68
            );
69
        }
70
    }
71
72
    private $map = [];
73
    /** @var ClassData */
74
    private $class;
75
}
76