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 |
||
24 | class DeleteController extends Controller |
||
25 | { |
||
26 | /** |
||
27 | * Main controller action |
||
28 | * |
||
29 | * @param Request $request |
||
30 | * @return \Symfony\Component\HttpFoundation\Response |
||
31 | */ |
||
32 | public function indexAction(Request $request) |
||
38 | |||
39 | /** |
||
40 | * Execute delete action. |
||
41 | * |
||
42 | * @param Request $request |
||
43 | * |
||
44 | * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response |
||
45 | */ |
||
46 | public function deleteAction(Request $request) |
||
63 | |||
64 | /** |
||
65 | * Get rate from request |
||
66 | * |
||
67 | * @param Request $request |
||
68 | * |
||
69 | * @return \RunOpenCode\ExchangeRate\Contract\RateInterface |
||
70 | */ |
||
71 | protected function getRateFromRequest(Request $request) |
||
95 | |||
96 | /** |
||
97 | * Save rate. |
||
98 | * |
||
99 | * @param RateInterface $rate |
||
100 | * |
||
101 | * @return TRUE if successful. |
||
102 | */ |
||
103 | View Code Duplication | protected function delete(RateInterface $rate) |
|
114 | |||
115 | /** |
||
116 | * Redirect after success. |
||
117 | * |
||
118 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
119 | */ |
||
120 | protected function redirectAfterSuccess() |
||
124 | } |
||
125 |
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.