Failed Conditions
Push — master ( e87576...01797b )
by Chad
9s
created

MagicPropertyDisabledTraitTest::magicSet()   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
dl 0
loc 4
c 1
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace SubjectivePHPTest\Spl\Traits;
4
5
use SubjectivePHP\Spl\Traits\MagicPropertyDisabledTrait;
6
7
/**
8
 * @coversDefaultClass SubjectivePHP\Spl\Traits\MagicPropertyDisabledTrait
9
 */
10
class MagicPropertyDisabledTraitTest extends \PHPUnit\Framework\TestCase
11
{
12
    /**
13
     * @test
14
     * @covers ::__get
15
     * @expectedException \BadMethodCallException
16
     */
17
    public function magicGet()
18
    {
19
        $value = $this->getObject()->foo;
0 ignored issues
show
Documentation introduced by
The property foo does not exist on object<SubjectivePHPTest...isabledTraitTest.php$0>. Since you implemented __set, maybe consider adding a @property annotation.

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...
Unused Code introduced by
$value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
20
    }
21
22
    /**
23
     * @test
24
     * @covers ::__set
25
     * @expectedException \BadMethodCallException
26
     */
27
    public function magicSet()
28
    {
29
        $this->getObject()->foo = 'bar';
0 ignored issues
show
Documentation introduced by
The property foo does not exist on object<SubjectivePHPTest...isabledTraitTest.php$0>. Since you implemented __set, maybe consider adding a @property annotation.

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...
30
    }
31
32
    public function getObject()
33
    {
34
        return new class
35
        {
36
            use MagicPropertyDisabledTrait;
37
        };
38
    }
39
}
40