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 |
||
21 | class UserTest extends PHPUnit_Framework_TestCase |
||
22 | { |
||
23 | use ValueObjectTestTrait; |
||
24 | |||
25 | /** |
||
26 | * Test a new class and default values on properties. |
||
27 | * |
||
28 | * @covers \eZ\Publish\Core\Repository\Values\User\User::__construct |
||
29 | */ |
||
30 | public function testNewClass() |
||
46 | |||
47 | /** |
||
48 | * Test getName method. |
||
49 | * |
||
50 | * @covers \eZ\Publish\Core\Repository\Values\User\User::getName |
||
51 | */ |
||
52 | View Code Duplication | public function testGetName() |
|
53 | { |
||
54 | $name = 'Translated name'; |
||
55 | $contentMock = Mockery::mock(Content::class); |
||
56 | $versionInfoMock = Mockery::mock(VersionInfo::class); |
||
57 | |||
58 | $contentMock->shouldReceive('getVersionInfo')->andReturn($versionInfoMock); |
||
59 | $versionInfoMock->shouldReceive('getName')->andReturn($name); |
||
60 | |||
61 | $object = new User(['content' => $contentMock]); |
||
62 | |||
63 | $this->assertEquals($name, $object->getName()); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Test retrieving missing property. |
||
68 | * |
||
69 | * @covers \eZ\Publish\API\Repository\Values\User\User::__get |
||
70 | * @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyNotFoundException |
||
71 | */ |
||
72 | public function testMissingProperty() |
||
78 | |||
79 | /** |
||
80 | * @covers \eZ\Publish\Core\Repository\Values\User\User::getProperties |
||
81 | */ |
||
82 | View Code Duplication | public function testObjectProperties() |
|
103 | |||
104 | /** |
||
105 | * Test setting read only property. |
||
106 | * |
||
107 | * @covers \eZ\Publish\API\Repository\Values\User\User::__set |
||
108 | * @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyReadOnlyException |
||
109 | */ |
||
110 | public function testReadOnlyProperty() |
||
116 | |||
117 | /** |
||
118 | * Test if property exists. |
||
119 | * |
||
120 | * @covers \eZ\Publish\API\Repository\Values\User\User::__isset |
||
121 | */ |
||
122 | public function testIsPropertySet() |
||
131 | |||
132 | /** |
||
133 | * Test unsetting a property. |
||
134 | * |
||
135 | * @covers \eZ\Publish\API\Repository\Values\User\User::__unset |
||
136 | * @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyReadOnlyException |
||
137 | */ |
||
138 | public function testUnsetProperty() |
||
144 | } |
||
145 |
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.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.