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 |
||
17 | class UserMetadataTest extends BaseTest |
||
18 | { |
||
19 | public function testParseProvider() |
||
36 | |||
37 | /** |
||
38 | * Tests the UserMetadata parser. |
||
39 | * |
||
40 | * @dataProvider testParseProvider |
||
41 | */ |
||
42 | public function testParse($data, $expected) |
||
53 | |||
54 | /** |
||
55 | * Test UserMetadata parser throwing exception on invalid UserMetadataCriterion format. |
||
56 | * |
||
57 | * @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser |
||
58 | * @expectedExceptionMessage Invalid <UserMetadataCriterion> format |
||
59 | */ |
||
60 | public function testParseExceptionOnInvalidCriterionFormat() |
||
69 | |||
70 | /** |
||
71 | * Test UserMetadata parser throwing exception on invalid target format. |
||
72 | * |
||
73 | * @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser |
||
74 | * @expectedExceptionMessage Invalid <Target> format |
||
75 | */ |
||
76 | View Code Duplication | public function testParseExceptionOnInvalidTargetFormat() |
|
88 | |||
89 | /** |
||
90 | * Test UserMetadata parser throwing exception on invalid value format. |
||
91 | * |
||
92 | * @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser |
||
93 | * @expectedExceptionMessage Invalid <Value> format |
||
94 | */ |
||
95 | View Code Duplication | public function testParseExceptionOnInvalidValueFormat() |
|
107 | |||
108 | /** |
||
109 | * Test UserMetadata parser throwing exception on wrong type of value format. |
||
110 | * |
||
111 | * @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser |
||
112 | * @expectedExceptionMessage Invalid <Value> format |
||
113 | */ |
||
114 | View Code Duplication | public function testParseExceptionOnWrongValueType() |
|
126 | |||
127 | /** |
||
128 | * Returns the UserMetadata criterion parser. |
||
129 | * |
||
130 | * @return \eZ\Publish\Core\REST\Server\Input\Parser\Criterion\UserMetadata |
||
131 | */ |
||
132 | protected function internalGetParser() |
||
136 | } |
||
137 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.