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 |
||
27 | class EditController extends Controller |
||
28 | { |
||
29 | /** |
||
30 | * Main controller action |
||
31 | * |
||
32 | * @param Request $request |
||
33 | * |
||
34 | * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response|\Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
35 | */ |
||
36 | 6 | public function indexAction(Request $request) |
|
69 | |||
70 | /** |
||
71 | * Handle form submission. |
||
72 | * |
||
73 | * @param Form $form |
||
74 | * @param Request $request |
||
75 | * @param RateInterface $exchangeRate |
||
76 | * |
||
77 | * @return bool TRUE if successful |
||
78 | */ |
||
79 | 4 | protected function handleForm(Form $form, Request $request, RateInterface $exchangeRate) |
|
99 | |||
100 | /** |
||
101 | * Get FQCN of FormType form. |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | 4 | protected function getFormType() |
|
109 | |||
110 | /** |
||
111 | * Get form. |
||
112 | * |
||
113 | * @return Form |
||
114 | */ |
||
115 | 4 | protected function getForm(RateInterface $rate) |
|
119 | |||
120 | /** |
||
121 | * Save rate. |
||
122 | * |
||
123 | * @param RateInterface $rate |
||
124 | * @return TRUE if successful. |
||
125 | */ |
||
126 | 2 | View Code Duplication | protected function save(RateInterface $rate) |
137 | |||
138 | /** |
||
139 | * Redirect after success. |
||
140 | * |
||
141 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
142 | */ |
||
143 | 1 | protected function redirectAfterSuccess() |
|
147 | |||
148 | /** |
||
149 | * Get Twig template path. |
||
150 | * |
||
151 | * @return string |
||
152 | */ |
||
153 | 4 | protected function getTwigTemplate() |
|
157 | } |
||
158 |
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
.