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 |
||
22 | class ContentDecoderJmsSerializerCompatibilityTest extends \PHPUnit_Framework_TestCase |
||
23 | { |
||
24 | /** |
||
25 | * @var ContentDecoder |
||
26 | */ |
||
27 | private $contentDecoder; |
||
28 | |||
29 | /** |
||
30 | * @var Serializer |
||
31 | */ |
||
32 | private $serializer; |
||
33 | |||
34 | /** |
||
35 | * Create serializer |
||
36 | */ |
||
37 | protected function setUp() |
||
45 | |||
46 | /** |
||
47 | * @test |
||
48 | */ |
||
49 | View Code Duplication | public function canDeserializeIntoObject() |
|
50 | { |
||
51 | $content = [ |
||
52 | 'foo' => 'bar' |
||
53 | ]; |
||
54 | $request = new Request([], [], [], [], [], [], json_encode($content)); |
||
55 | $request->headers->set('Content-Type', 'application/json'); |
||
56 | |||
57 | |||
58 | $operationDefinition = (object)[ |
||
59 | 'parameters' => [ |
||
60 | (object)[ |
||
61 | "in" => "body", |
||
62 | "name" => "body", |
||
63 | "schema" => (object)[ |
||
64 | '$ref' => "#/definitions/JmsAnnotatedResourceStub" |
||
65 | ] |
||
66 | ] |
||
67 | ] |
||
68 | ]; |
||
69 | |||
70 | $operationObject = OperationObject::createFromOperationDefinition((object)$operationDefinition); |
||
71 | |||
72 | $actual = $this->contentDecoder->decodeContent($request, $operationObject); |
||
73 | |||
74 | $className = 'KleijnWeb\SwaggerBundle\Tests\Request\ContentDecoder\JmsAnnotatedResourceStub'; |
||
75 | $expected = (new $className)->setFoo('bar'); |
||
76 | |||
77 | $this->assertEquals($expected, $actual); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @test |
||
82 | * |
||
83 | * @expectedException \KleijnWeb\SwaggerBundle\Exception\MalformedContentException |
||
84 | */ |
||
85 | View Code Duplication | public function willThrowMalformedContentExceptionWhenDecodingFails() |
|
94 | |||
95 | /** |
||
96 | * @test |
||
97 | * @dataProvider contentTypeProvider |
||
98 | * |
||
99 | * @param string $contentType |
||
100 | */ |
||
101 | View Code Duplication | public function willAlwaysDecodeJson($contentType) |
|
126 | |||
127 | /** |
||
128 | * @return array |
||
129 | */ |
||
130 | public static function contentTypeProvider() |
||
137 | } |
||
138 |
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..