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

MethodsCollection::all()   C

Complexity

Conditions 8
Paths 54

Size

Total Lines 33
Code Lines 23

Duplication

Lines 6
Ratio 18.18 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 6
loc 33
rs 5.3846
cc 8
eloc 23
nc 54
nop 1
1
<?php
2
3
namespace Padawan\Domain\Project\Collection;
4
5
use Padawan\Domain\Project\Node\MethodData;
6
use Padawan\Domain\Project\Node\ClassData;
7
use Padawan\Domain\Project\Node\InterfaceData;
8
9
class MethodsCollection {
10
    private $methods = [];
11
    /** @property ClassData */
12
    private $class;
13
14
    public function __construct($class) {
15
        $this->class = $class;
16
    }
17
    public function add(MethodData $method) {
18
        $this->methods[$method->name] = $method;
19
    }
20
    public function remove(MethodData $method) {
21
        if (array_key_exists($method->name, $this->methods)) {
22
            unset($this->methods[$method->name]);
23
        }
24
    }
25
    public function get($name, Specification $spec = null) {
26
        if ($spec === null) {
27
            $spec = new Specification('private', 2, true);
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...
28
        }
29 View Code Duplication
        if (array_key_exists($name, $this->methods)) {
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...
30
            $method = $this->methods[$name];
31
            if ($spec->satisfy($method)) {
32
                return $method;
33
            }
34
            return null;
35
        }
36
        $parentSpec = new Specification(
37
            $spec->getParentMode(),
38
            $spec->isStatic(),
39
            $spec->isMagic()
40
        );
41
        if ($this->class instanceof ClassData) {
42
            $parent = $this->class->getParent();
43
            if ($parent instanceof ClassData) {
44
                return $parent->methods->get($name, $parentSpec);
0 ignored issues
show
Documentation introduced by
The property $methods 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...
45
            }
46
        }
47 View Code Duplication
        foreach ($this->class->getInterfaces() as $interface) {
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...
48
            if ($interface instanceof InterfaceData) {
49
                $method = $interface->methods->get($name, $parentSpec);
50
                if ($method instanceof MethodData) {
51
                    return $method;
52
                }
53
            }
54
        }
55
    }
56
    public function all(Specification $spec = null) {
57
        if ($spec === null) {
58
            $spec = new Specification;
59
        }
60
        $methods = [];
61
        foreach ($this->methods AS $method) {
62
            if ($spec->satisfy($method)) {
63
                $methods[$method->name] = $method;
64
            }
65
        }
66
        $parentSpec = new Specification(
67
            $spec->getParentMode(),
68
            $spec->isStatic(),
69
            $spec->isMagic()
70
        );
71
        if ($this->class instanceof ClassData) {
72
            $parent = $this->class->getParent();
73
            if ($parent instanceof ClassData) {
74
                $parentM = $parent->methods->all($parentSpec);
0 ignored issues
show
Documentation introduced by
The property $methods 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...
75
                $methods = array_merge(
76
                    $parentM,
77
                    $methods
78
                );
79
            }
80
        }
81 View Code Duplication
        foreach ($this->class->getInterfaces() as $interface) {
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...
82
            if ($interface instanceof InterfaceData) {
83
                $parentM = $interface->methods->all($parentSpec);
84
                $methods = array_merge($parentM, $methods);
85
            }
86
        }
87
        return $methods;
88
    }
89
}
90