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 LegacyDFSClusterTest extends PHPUnit_Framework_TestCase |
||
18 | { |
||
19 | /** @var \eZ\Publish\Core\IO\IOMetadataHandler|\PHPUnit_Framework_MockObject_MockObject */ |
||
20 | private $handler; |
||
21 | |||
22 | /** @var \Doctrine\DBAL\Connection|\PHPUnit_Framework_MockObject_MockObject */ |
||
23 | private $dbalMock; |
||
24 | |||
25 | /** @var \eZ\Publish\Core\IO\UrlDecorator|\PHPUnit_Framework_MockObject_MockObject */ |
||
26 | private $urlDecoratorMock; |
||
27 | |||
28 | public function setUp() |
||
39 | |||
40 | public function providerCreate() |
||
48 | |||
49 | /** |
||
50 | * @dataProvider providerCreate |
||
51 | */ |
||
52 | public function testCreate($id, $mimeType, $size, $mtime, $mtimeExpected) |
||
53 | { |
||
54 | $this->dbalMock |
||
55 | ->expects($this->once()) |
||
56 | ->method('prepare') |
||
57 | ->with($this->anything()) |
||
58 | ->will($this->returnValue($this->createDbalStatementMock())); |
||
59 | |||
60 | $spiCreateStruct = new SPIBinaryFileCreateStruct(); |
||
61 | $spiCreateStruct->id = $id; |
||
62 | $spiCreateStruct->mimeType = $mimeType; |
||
63 | $spiCreateStruct->size = $size; |
||
64 | $spiCreateStruct->mtime = $mtime; |
||
65 | |||
66 | $spiBinary = $this->handler->create($spiCreateStruct); |
||
67 | |||
68 | $this->assertInstanceOf('eZ\Publish\SPI\IO\BinaryFile', $spiBinary); |
||
69 | |||
70 | $this->assertEquals($mtimeExpected, $spiBinary->mtime); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
75 | */ |
||
76 | public function testCreateInvalidArgument() |
||
77 | { |
||
78 | $this->dbalMock |
||
79 | ->expects($this->never()) |
||
80 | ->method('prepare'); |
||
81 | |||
82 | $spiCreateStruct = new SPIBinaryFileCreateStruct(); |
||
83 | $spiCreateStruct->id = 'prefix/my/file.png'; |
||
84 | $spiCreateStruct->mimeType = 'image/png'; |
||
85 | $spiCreateStruct->size = 123; |
||
86 | $spiCreateStruct->mtime = 1307155242; // Invalid, should be a DateTime |
||
|
|||
87 | |||
88 | $this->handler->create($spiCreateStruct); |
||
89 | } |
||
90 | |||
91 | View Code Duplication | public function testDelete() |
|
107 | |||
108 | /** |
||
109 | * @expectedException \eZ\Publish\Core\IO\Exception\BinaryFileNotFoundException |
||
110 | */ |
||
111 | View Code Duplication | public function testDeleteNotFound() |
|
127 | |||
128 | public function testLoad() |
||
158 | |||
159 | /** |
||
160 | * @expectedException \eZ\Publish\Core\IO\Exception\BinaryFileNotFoundException |
||
161 | */ |
||
162 | View Code Duplication | public function testLoadNotFound() |
|
178 | |||
179 | View Code Duplication | public function testExists() |
|
195 | |||
196 | View Code Duplication | public function testExistsNot() |
|
212 | |||
213 | View Code Duplication | public function testDeletedirectory() |
|
229 | |||
230 | /** |
||
231 | * @return \PHPUnit_Framework_MockObject_MockObject |
||
232 | */ |
||
233 | protected function createDbalStatementMock() |
||
237 | } |
||
238 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..