MagicAccessorTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 27
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 0 4 2
A __set() 0 4 1
1
<?php
2
3
namespace SubjectivePHP\Spl\Traits;
4
5
trait MagicAccessorTrait
6
{
7
    /**
8
     * Access an internal array element as a property
9
     *
10
     * @param string $name The name of class the property.
11
     *
12
     * @return mixed The class property value.
13
     */
14
    public function __get($name)
15
    {
16
        return array_key_exists($name, $this->container) ? $this->container[$name] : null;
0 ignored issues
show
Documentation introduced by
The property container does not exist on object<SubjectivePHP\Spl...its\MagicAccessorTrait>. 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...
17
    }
18
19
    /**
20
     * Set an internal array element as a property
21
     *
22
     * @param string $name  The name of class the property.
23
     * @param mixed  $value The class property value.
24
     *
25
     * @return void
26
     */
27
    public function __set($name, $value)
28
    {
29
        $this->container[$name] = $value;
0 ignored issues
show
Documentation introduced by
The property container does not exist on object<SubjectivePHP\Spl...its\MagicAccessorTrait>. 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...
30
    }
31
}
32