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

ConstCollection::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Padawan\Domain\Project\Collection;
4
5
use Padawan\Domain\Project\Node\ClassData;
6
7
class ConstCollection
8
{
9
    /**
10
     * @param ClassData $class
11
     */
12
    public function __construct($class)
13
    {
14
        $this->class = $class;
15
        $this->map['class'] = 'class';
16
    }
17
    public function add($constant)
18
    {
19
        $this->map[$constant] = $constant;
20
    }
21 View Code Duplication
    public function all()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
22
    {
23
        $consts = $this->map;
24
        $parent = $this->class->getParent();
25
        if ($parent instanceof ClassData) {
26
            $consts = array_merge(
27
                $parent->properties->all(),
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...
28
                $consts
29
            );
30
        }
31
        sort($consts);
32
        return $consts;
33
    }
34 View Code Duplication
    public function get($propName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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
    {
36
        if (array_key_exists($propName, $this->map)) {
37
            $const = $this->map[$propName];
38
            return $const;
39
        }
40
        $parent = $this->class->getParent();
41
        if ($parent instanceof ClassData) {
42
            return $parent->properties->get($propName);
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...
43
        }
44
    }
45
46
    private $map = [];
47
    private $class;
48
}
49