Completed
Push — master ( 46b7df...4cf19c )
by Emily
02:25
created

ConditionalPropertyAccessor::setAnyValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Spaark\CompositeUtils\Service;
4
5
use Spaark\CompositeUtils\Exception\PropertyNotWritableException;
6
use Spaark\CompositeUtils\Exception\PropertyNotReadableException;
7
8
class ConditionalPropertyAccessor extends PropertyAccessor
9
{
10 2
    protected function setAnyValue($property, $value)
11
    {
12 2
        if ($property->writable)
13
        {
14 1
            parent::setAnyValue($property, $value);
15
        }
16
        else
17
        {
18 1
            throw new PropertyNotWritableException
19
            (
20 1
                get_class($this->object),
21 1
                $property->name
22
            );
23
        }
24 1
    }
25
26 2
    public function getValue($property)
27
    {
28 2
        if (!$this->reflect->properties->contains($property))
0 ignored issues
show
Documentation introduced by
The property $properties is declared protected in Spaark\CompositeUtils\Mo...ion\ReflectionComposite. 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...
29
        {
30
            throw new CannotReadPropertyException
31
            (
32
                get_class($this->object),
33
                $property
34
            );
35
        }
36 2
        elseif (!$this->reflect->properties[$property]->readable)
0 ignored issues
show
Documentation introduced by
The property $properties is declared protected in Spaark\CompositeUtils\Mo...ion\ReflectionComposite. 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
        {
38 1
            throw new PropertyNotReadableException
39
            (
40 1
                get_class($this->object),
41
                $property
42
            );
43
        }
44
45 1
        return parent::getValue($property);
46
    }
47
}
48