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 |
||
| 3 | class Xhgui_Controller_Run extends Xhgui_Controller |
||
|
|
|||
| 4 | { |
||
| 5 | public function __construct($app, $profiles, $watches) |
||
| 6 | { |
||
| 7 | $this->_app = $app; |
||
| 8 | $this->_profiles = $profiles; |
||
| 9 | $this->_watches = $watches; |
||
| 10 | } |
||
| 11 | |||
| 12 | public function index() |
||
| 13 | { |
||
| 14 | $request = $this->_app->request(); |
||
| 15 | |||
| 16 | $search = array(); |
||
| 17 | $keys = array('date_start', 'date_end', 'url'); |
||
| 18 | foreach ($keys as $key) { |
||
| 19 | if ($request->get($key)) { |
||
| 20 | $search[$key] = $request->get($key); |
||
| 21 | } |
||
| 22 | } |
||
| 23 | $sort = $request->get('sort'); |
||
| 24 | |||
| 25 | $result = $this->_profiles->getAll(array( |
||
| 26 | 'sort' => $sort, |
||
| 27 | 'page' => $request->get('page'), |
||
| 28 | 'direction' => $request->get('direction'), |
||
| 29 | 'perPage' => $this->_app->config('page.limit'), |
||
| 30 | 'conditions' => $search, |
||
| 31 | 'projection' => true, |
||
| 32 | )); |
||
| 33 | |||
| 34 | $title = 'Recent runs'; |
||
| 35 | $titleMap = array( |
||
| 36 | 'wt' => 'Longest wall time', |
||
| 37 | 'cpu' => 'Most CPU time', |
||
| 38 | 'mu' => 'Highest memory use', |
||
| 39 | ); |
||
| 40 | if (isset($titleMap[$sort])) { |
||
| 41 | $title = $titleMap[$sort]; |
||
| 42 | } |
||
| 43 | |||
| 44 | $paging = array( |
||
| 45 | 'total_pages' => $result['totalPages'], |
||
| 46 | 'page' => $result['page'], |
||
| 47 | 'sort' => $sort, |
||
| 48 | 'direction' => $result['direction'] |
||
| 49 | ); |
||
| 50 | |||
| 51 | $this->_template = 'runs/list.twig'; |
||
| 52 | $this->set(array( |
||
| 53 | 'paging' => $paging, |
||
| 54 | 'base_url' => 'home', |
||
| 55 | 'runs' => $result['results'], |
||
| 56 | 'date_format' => $this->_app->config('date.format'), |
||
| 57 | 'search' => $search, |
||
| 58 | 'has_search' => strlen(implode('', $search)) > 0, |
||
| 59 | 'title' => $title |
||
| 60 | )); |
||
| 61 | } |
||
| 62 | |||
| 63 | public function view() |
||
| 98 | |||
| 99 | public function url() |
||
| 149 | |||
| 150 | public function compare() |
||
| 151 | { |
||
| 152 | $request = $this->_app->request(); |
||
| 153 | |||
| 154 | $baseRun = $headRun = $candidates = $comparison = null; |
||
| 155 | $paging = array(); |
||
| 156 | |||
| 157 | if ($request->get('base')) { |
||
| 158 | $baseRun = $this->_profiles->get($request->get('base')); |
||
| 159 | } |
||
| 160 | |||
| 161 | if ($baseRun && !$request->get('head')) { |
||
| 162 | $pagination = array( |
||
| 163 | 'direction' => $request->get('direction'), |
||
| 164 | 'sort' => $request->get('sort'), |
||
| 165 | 'page' => $request->get('page'), |
||
| 166 | 'perPage' => $this->_app->config('page.limit'), |
||
| 167 | ); |
||
| 168 | $candidates = $this->_profiles->getForUrl( |
||
| 169 | $baseRun->getMeta('simple_url'), |
||
| 170 | $pagination |
||
| 171 | ); |
||
| 172 | |||
| 173 | $paging = array( |
||
| 174 | 'total_pages' => $candidates['totalPages'], |
||
| 175 | 'sort' => $pagination['sort'], |
||
| 176 | 'page' => $candidates['page'], |
||
| 177 | 'direction' => $candidates['direction'] |
||
| 178 | ); |
||
| 179 | } |
||
| 180 | |||
| 181 | if ($request->get('head')) { |
||
| 182 | $headRun = $this->_profiles->get($request->get('head')); |
||
| 183 | } |
||
| 184 | |||
| 185 | if ($baseRun && $headRun) { |
||
| 186 | $comparison = $baseRun->compare($headRun); |
||
| 187 | } |
||
| 188 | |||
| 189 | $this->_template = 'runs/compare.twig'; |
||
| 190 | $this->set(array( |
||
| 191 | 'base_url' => 'run.compare', |
||
| 192 | 'base_run' => $baseRun, |
||
| 193 | 'head_run' => $headRun, |
||
| 194 | 'candidates' => $candidates, |
||
| 195 | 'url_params' => $request->get(), |
||
| 196 | 'date_format' => $this->_app->config('date.format'), |
||
| 197 | 'comparison' => $comparison, |
||
| 198 | 'paging' => $paging, |
||
| 199 | 'search' => array( |
||
| 200 | 'base' => $request->get('base'), |
||
| 201 | 'head' => $request->get('head'), |
||
| 202 | ) |
||
| 203 | )); |
||
| 204 | } |
||
| 205 | |||
| 206 | public function symbol() |
||
| 226 | |||
| 227 | public function symbolShort() |
||
| 249 | |||
| 250 | View Code Duplication | public function callgraph() |
|
| 251 | { |
||
| 252 | $request = $this->_app->request(); |
||
| 253 | $profile = $this->_profiles->get($request->get('id')); |
||
| 254 | |||
| 255 | $this->_template = 'runs/callgraph.twig'; |
||
| 256 | $this->set(array( |
||
| 257 | 'profile' => $profile, |
||
| 258 | 'date_format' => $this->_app->config('date.format'), |
||
| 259 | )); |
||
| 260 | } |
||
| 261 | |||
| 262 | View Code Duplication | public function callgraphData() |
|
| 274 | |||
| 275 | View Code Duplication | public function flamegraph() |
|
| 286 | |||
| 287 | View Code Duplication | public function flamegraphData() |
|
| 299 | |||
| 300 | View Code Duplication | public function callgraphDataDot() |
|
| 312 | } |
||
| 313 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.