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 |
||
19 | class Client extends FrameworkClient |
||
20 | { |
||
21 | /** |
||
22 | * @var mixed |
||
23 | */ |
||
24 | private $results; |
||
25 | |||
26 | /** |
||
27 | * @var boolean |
||
28 | */ |
||
29 | private $jsonRequest = true; |
||
30 | |||
31 | /** |
||
32 | * return decoded results from a request |
||
33 | * |
||
34 | * @return mixed |
||
35 | */ |
||
36 | 5 | public function getResults() |
|
40 | |||
41 | /** |
||
42 | * POSTs to an URI. |
||
43 | * |
||
44 | * @param string $uri The URI to fetch |
||
45 | * @param mixed $content The raw body data |
||
46 | * @param array $parameters The Request parameters |
||
47 | * @param array $files The files |
||
48 | * @param array $server The server parameters (HTTP headers are referenced with a HTTP_ prefix as PHP does) |
||
49 | * @param boolean $jsonEncode If $content should be json encoded or not |
||
50 | * |
||
51 | * @return \Symfony\Component\DomCrawler\Crawler|null |
||
52 | * |
||
53 | * @api |
||
54 | */ |
||
55 | View Code Duplication | public function post( |
|
|
|||
56 | $uri, |
||
57 | $content, |
||
58 | array $parameters = array(), |
||
59 | array $files = array(), |
||
60 | array $server = array(), |
||
61 | $jsonEncode = true |
||
62 | ) { |
||
63 | $this->jsonRequest = $jsonEncode; |
||
64 | |||
65 | if ($jsonEncode) { |
||
66 | $content = json_encode($content); |
||
67 | } |
||
68 | |||
69 | return $this->request( |
||
70 | 'POST', |
||
71 | $uri, |
||
72 | $parameters, |
||
73 | $files, |
||
74 | $server, |
||
75 | $content |
||
76 | ); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * PUTs to an URI. |
||
81 | * |
||
82 | * @param string $uri The URI to fetch |
||
83 | * @param mixed $content The raw body data |
||
84 | * @param array $parameters The Request parameters |
||
85 | * @param array $files The files |
||
86 | * @param array $server The server parameters (HTTP headers are referenced with a HTTP_ prefix as PHP does) |
||
87 | * @param boolean $jsonEncode If $content should be json encoded or not |
||
88 | * |
||
89 | * @return \Symfony\Component\DomCrawler\Crawler|null |
||
90 | * |
||
91 | * @api |
||
92 | */ |
||
93 | View Code Duplication | public function put( |
|
94 | $uri, |
||
95 | $content, |
||
96 | array $parameters = array(), |
||
97 | array $files = array(), |
||
98 | array $server = array(), |
||
99 | $jsonEncode = true |
||
100 | ) { |
||
101 | $this->jsonRequest = $jsonEncode; |
||
102 | |||
103 | if ($jsonEncode) { |
||
104 | $content = json_encode($content); |
||
105 | } |
||
106 | |||
107 | return $this->request( |
||
108 | 'PUT', |
||
109 | $uri, |
||
110 | $parameters, |
||
111 | $files, |
||
112 | $server, |
||
113 | $content |
||
114 | ); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * prepare a deserialized copy of a json response |
||
119 | * |
||
120 | * @param object $response Response containing our return value as raw json |
||
121 | * |
||
122 | * @return \Symfony\Component\BrowserKit\Response response |
||
123 | * |
||
124 | * @todo use JMSSerializer for additional JSON validation |
||
125 | */ |
||
126 | 5 | protected function filterResponse($response) |
|
132 | |||
133 | /** |
||
134 | * force all requests to be json like. |
||
135 | * |
||
136 | * Do JSON/RESTful requests using this client if the caller has not specified something else. |
||
137 | * |
||
138 | * @param object $request Request object |
||
139 | * |
||
140 | * @return \Symfony\Component\HttpFoundation\Response request |
||
141 | */ |
||
142 | 5 | protected function doRequest($request) |
|
151 | } |
||
152 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.