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) |
||
| 11 | |||
| 12 | public function index() |
||
| 62 | |||
| 63 | public function view() |
||
| 64 | { |
||
| 65 | $request = $this->_app->request(); |
||
| 66 | $detailCount = $this->_app->config('detail.count'); |
||
| 67 | $result = $this->_profiles->get($request->get('id')); |
||
| 68 | |||
| 69 | $result->calculateSelf(); |
||
| 70 | |||
| 71 | // Self wall time graph |
||
| 72 | $timeChart = $result->extractDimension('ewt', $detailCount); |
||
| 73 | |||
| 74 | // Memory Block |
||
| 75 | $memoryChart = $result->extractDimension('emu', $detailCount); |
||
| 76 | |||
| 77 | // Watched Functions Block |
||
| 78 | $watchedFunctions = array(); |
||
| 79 | foreach ($this->_watches->getAll() as $watch) { |
||
| 80 | $matches = $result->getWatched($watch['name']); |
||
| 81 | if ($matches) { |
||
| 82 | $watchedFunctions = array_merge($watchedFunctions, $matches); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | $profile = $result->sort('ewt', $result->getProfile()); |
||
| 87 | |||
| 88 | $this->_template = 'runs/view.twig'; |
||
| 89 | $this->set(array( |
||
| 90 | 'profile' => $profile, |
||
| 91 | 'result' => $result, |
||
| 92 | 'wall_time' => $timeChart, |
||
| 93 | 'memory' => $memoryChart, |
||
| 94 | 'watches' => $watchedFunctions, |
||
| 95 | 'date_format' => $this->_app->config('date.format'), |
||
| 96 | )); |
||
| 97 | } |
||
| 98 | |||
| 99 | public function url() |
||
| 100 | { |
||
| 101 | $request = $this->_app->request(); |
||
| 102 | $pagination = array( |
||
| 103 | 'sort' => $request->get('sort'), |
||
| 104 | 'direction' => $request->get('direction'), |
||
| 105 | 'page' => $request->get('page'), |
||
| 106 | 'perPage' => $this->_app->config('page.limit'), |
||
| 107 | ); |
||
| 108 | |||
| 109 | $search = array(); |
||
| 110 | $keys = array('date_start', 'date_end', 'limit', 'limit_custom'); |
||
| 111 | foreach ($keys as $key) { |
||
| 112 | $search[$key] = $request->get($key); |
||
| 113 | } |
||
| 114 | |||
| 115 | $runs = $this->_profiles->getForUrl( |
||
| 116 | $request->get('url'), |
||
| 117 | $pagination, |
||
| 118 | $search |
||
| 119 | ); |
||
| 120 | |||
| 121 | if (isset($search['limit_custom']) && strlen($search['limit_custom']) > 0 && $search['limit_custom'][0] == 'P') { |
||
| 122 | $search['limit'] = $search['limit_custom']; |
||
| 123 | } |
||
| 124 | |||
| 125 | $chartData = $this->_profiles->getPercentileForUrl( |
||
| 126 | 90, |
||
| 127 | $request->get('url'), |
||
| 128 | $search |
||
| 129 | ); |
||
| 130 | |||
| 131 | $paging = array( |
||
| 132 | 'total_pages' => $runs['totalPages'], |
||
| 133 | 'sort' => $pagination['sort'], |
||
| 134 | 'page' => $runs['page'], |
||
| 135 | 'direction' => $runs['direction'] |
||
| 136 | ); |
||
| 137 | |||
| 138 | $this->_template = 'runs/url.twig'; |
||
| 139 | $this->set(array( |
||
| 140 | 'paging' => $paging, |
||
| 141 | 'base_url' => 'url.view', |
||
| 142 | 'runs' => $runs['results'], |
||
| 143 | 'url' => $request->get('url'), |
||
| 144 | 'chart_data' => $chartData, |
||
| 145 | 'date_format' => $this->_app->config('date.format'), |
||
| 146 | 'search' => array_merge($search, array('url' => $request->get('url'))), |
||
| 147 | )); |
||
| 148 | } |
||
| 149 | |||
| 150 | public function compare() |
||
| 205 | |||
| 206 | public function symbol() |
||
| 207 | { |
||
| 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 | } |
||
| 314 |
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.