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 |
||
| 16 | class SectionInputTest extends BaseTest |
||
|
|
|||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Tests the SectionInput parser. |
||
| 20 | */ |
||
| 21 | public function testParse() |
||
| 22 | { |
||
| 23 | $inputArray = array( |
||
| 24 | '_identifier' => 'Section', |
||
| 25 | 'values' => [ |
||
| 26 | 'ref' => [ |
||
| 27 | [ |
||
| 28 | '_href' => '/content/section/2', |
||
| 29 | '_media-type' => "application/vnd.ez.api.Section+json", |
||
| 30 | ], |
||
| 31 | ], |
||
| 32 | ], |
||
| 33 | ); |
||
| 34 | |||
| 35 | $this->getRequestParserMock() |
||
| 36 | ->expects($this->once()) |
||
| 37 | ->method('parseHref') |
||
| 38 | ->with('/content/section/2', 'sectionId') |
||
| 39 | ->will($this->returnValue(2)); |
||
| 40 | |||
| 41 | $result = $this->getParser()->parse($inputArray, $this->getParsingDispatcherMock()); |
||
| 42 | |||
| 43 | $this->assertEquals( |
||
| 44 | new SectionLimitation(['limitationValues' => [2]]), |
||
| 45 | $result, |
||
| 46 | 'SectionLimitation not created correctly.' |
||
| 47 | ); |
||
| 48 | } |
||
| 49 | |||
| 50 | public function getParseHrefExpectationsMap() |
||
| 51 | { |
||
| 52 | return [ |
||
| 53 | ['/content/section/2', 'sectionId', 2] |
||
| 54 | ]; |
||
| 55 | } |
||
| 56 | |||
| 57 | |||
| 58 | /** |
||
| 59 | * Returns the section input parser. |
||
| 60 | * |
||
| 61 | * @return \eZ\Publish\Core\REST\Server\Input\Parser\SectionInput |
||
| 62 | */ |
||
| 63 | protected function internalGetParser() |
||
| 64 | { |
||
| 65 | return new Parser($this->getSectionServiceMock()); |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Get the section service mock object. |
||
| 70 | * |
||
| 71 | * @return \eZ\Publish\API\Repository\SectionService |
||
| 72 | */ |
||
| 73 | View Code Duplication | protected function getSectionServiceMock() |
|
| 74 | { |
||
| 75 | $sectionServiceMock = $this->getMock( |
||
| 76 | 'eZ\\Publish\\Core\\Repository\\SectionService', |
||
| 77 | array(), |
||
| 78 | array(), |
||
| 79 | '', |
||
| 80 | false |
||
| 81 | ); |
||
| 82 | |||
| 83 | $sectionServiceMock->expects($this->any()) |
||
| 84 | ->method('newSectionCreateStruct') |
||
| 85 | ->will( |
||
| 86 | $this->returnValue(new SectionCreateStruct()) |
||
| 87 | ); |
||
| 88 | |||
| 89 | return $sectionServiceMock; |
||
| 90 | } |
||
| 91 | } |
||
| 92 |
This check looks for classes that have been defined more than once.
If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.
This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.