Completed
Push — master ( 7c0e42...8c347f )
by André
14:17
created

SectionTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 77
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 77
loc 77
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testNewClass() 13 13 1
A testMissingProperty() 6 6 1
A testReadOnlyProperty() 6 6 1
A testIsPropertySet() 9 9 1
A testUnsetProperty() 6 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\API\Repository\Tests\Values\Content;
10
11
use eZ\Publish\API\Repository\Tests\Values\ValueObjectTestTrait;
12
use eZ\Publish\API\Repository\Values\Content\Section;
13
use PHPUnit_Framework_TestCase;
14
15 View Code Duplication
class SectionTest extends PHPUnit_Framework_TestCase
0 ignored issues
show
Duplication introduced by
This class 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...
16
{
17
    use ValueObjectTestTrait;
18
19
    /**
20
     * Test a new class and default values on properties.
21
     *
22
     * @covers \eZ\Publish\API\Repository\Values\Content\Section::__construct
23
     */
24
    public function testNewClass()
25
    {
26
        $section = new Section();
27
28
        $this->assertPropertiesCorrect(
29
            [
30
                'id' => null,
31
                'identifier' => null,
32
                'name' => null,
33
            ],
34
            $section
35
        );
36
    }
37
38
    /**
39
     * Test retrieving missing property.
40
     *
41
     * @covers \eZ\Publish\API\Repository\Values\Content\Section::__get
42
     * @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyNotFoundException
43
     */
44
    public function testMissingProperty()
45
    {
46
        $section = new Section();
47
        $value = $section->notDefined;
0 ignored issues
show
Documentation introduced by
The property notDefined does not exist on object<eZ\Publish\API\Re...Values\Content\Section>. 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...
48
        self::fail('Succeeded getting non existing property');
49
    }
50
51
    /**
52
     * Test setting read only property.
53
     *
54
     * @covers \eZ\Publish\API\Repository\Values\Content\Section::__set
55
     * @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyReadOnlyException
56
     */
57
    public function testReadOnlyProperty()
58
    {
59
        $section = new Section();
60
        $section->id = 22;
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\Content\Section. Since you implemented __set(), maybe consider adding a @property or @property-write 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...
61
        self::fail('Succeeded setting read only property');
62
    }
63
64
    /**
65
     * Test if property exists.
66
     *
67
     * @covers \eZ\Publish\API\Repository\Values\Content\Section::__isset
68
     */
69
    public function testIsPropertySet()
70
    {
71
        $section = new Section();
72
        $value = isset($section->notDefined);
73
        self::assertEquals(false, $value);
74
75
        $value = isset($section->id);
76
        self::assertEquals(true, $value);
77
    }
78
79
    /**
80
     * Test unsetting a property.
81
     *
82
     * @covers \eZ\Publish\API\Repository\Values\Content\Section::__unset
83
     * @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyReadOnlyException
84
     */
85
    public function testUnsetProperty()
86
    {
87
        $section = new Section(['id' => 1]);
88
        unset($section->id);
89
        self::fail('Unsetting read-only property succeeded');
90
    }
91
}
92