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 |
||
5 | class Xhgui_Controller_Run extends Xhgui_Controller |
||
6 | { |
||
7 | /** |
||
8 | * @var Xhgui_Profiles |
||
9 | */ |
||
10 | private $profiles; |
||
11 | |||
12 | /** |
||
13 | * @var \Xhgui_WatchedFunctionsStorageInterface |
||
14 | */ |
||
15 | private $watches; |
||
16 | |||
17 | /** |
||
18 | * Xhgui_Controller_Run constructor. |
||
19 | * @param Slim $app |
||
20 | * @param Xhgui_Profiles $profiles |
||
21 | * @param Xhgui_WatchedFunctionsStorageInterface $watches |
||
22 | */ |
||
23 | public function __construct(Slim $app, Xhgui_Profiles $profiles, \Xhgui_WatchedFunctionsStorageInterface $watches) |
||
24 | { |
||
25 | $this->app = $app; |
||
26 | $this->profiles = $profiles; |
||
27 | $this->watches = $watches; |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * |
||
32 | */ |
||
33 | public function index() |
||
34 | { |
||
35 | $request = $this->app->request(); |
||
36 | |||
37 | $filter = Xhgui_Storage_Filter::fromRequest($request); |
||
38 | |||
39 | $result = $this->profiles->getAll($filter); |
||
40 | $title = 'Recent runs'; |
||
41 | $titleMap = array( |
||
42 | 'wt' => 'Longest wall time', |
||
43 | 'cpu' => 'Most CPU time', |
||
44 | 'mu' => 'Highest memory use', |
||
45 | ); |
||
46 | if (isset($titleMap[$filter->getSort()])) { |
||
47 | $title = $titleMap[$filter->getSort()]; |
||
48 | } |
||
49 | $paging = array( |
||
50 | 'total_pages' => $result['totalPages'], |
||
51 | 'page' => $result['page'], |
||
52 | 'sort' => $filter->getSort(), |
||
53 | 'direction' => $result['direction'] |
||
54 | ); |
||
55 | |||
56 | $this->_template = 'runs/list.twig'; |
||
57 | |||
58 | $this->set(array( |
||
59 | 'paging' => $paging, |
||
60 | 'base_url' => 'home', |
||
61 | 'runs' => $result['results'], |
||
62 | 'date_format' => $this->app->config('date.format'), |
||
63 | 'search' => $filter->toArray(), |
||
64 | 'title' => $title |
||
65 | )); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * |
||
70 | */ |
||
71 | public function view() |
||
72 | { |
||
73 | $request = $this->app->request(); |
||
74 | $detailCount = $this->app->config('detail.count'); |
||
75 | $result = $this->profiles->get($request->get('id')); |
||
76 | |||
77 | $result->calculateSelf(); |
||
78 | |||
79 | // Self wall time graph |
||
80 | $timeChart = $result->extractDimension('ewt', $detailCount); |
||
81 | |||
82 | // Memory Block |
||
83 | $memoryChart = $result->extractDimension('emu', $detailCount); |
||
84 | |||
85 | // Watched Functions Block |
||
86 | $watchedFunctions = array(); |
||
87 | foreach ($this->watches->getWatchedFunctions() as $watch) { |
||
88 | $matches = $result->getWatched($watch['name']); |
||
89 | |||
90 | if ($matches) { |
||
91 | $watchedFunctions = array_merge($watchedFunctions, $matches); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | $profile = $result->sort('ewt', $result->getProfile()); |
||
96 | |||
97 | $this->_template = 'runs/view.twig'; |
||
98 | $this->set(array( |
||
99 | 'profile' => $profile, |
||
100 | 'result' => $result, |
||
101 | 'wall_time' => $timeChart, |
||
102 | 'memory' => $memoryChart, |
||
103 | 'watches' => $watchedFunctions, |
||
104 | 'date_format' => $this->app->config('date.format'), |
||
105 | )); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @throws Exception |
||
110 | */ |
||
111 | public function deleteForm() |
||
128 | |||
129 | /** |
||
130 | * @throws Exception |
||
131 | */ |
||
132 | public function deleteSubmit() |
||
152 | |||
153 | /** |
||
154 | * |
||
155 | */ |
||
156 | public function deleteAllForm() |
||
160 | |||
161 | /** |
||
162 | * |
||
163 | */ |
||
164 | public function deleteAllSubmit() |
||
175 | |||
176 | /** |
||
177 | * |
||
178 | */ |
||
179 | public function url() |
||
211 | |||
212 | /** |
||
213 | * |
||
214 | */ |
||
215 | public function compare() |
||
265 | |||
266 | /** |
||
267 | * |
||
268 | */ |
||
269 | public function symbol() |
||
289 | |||
290 | /** |
||
291 | * |
||
292 | */ |
||
293 | public function symbolShort() |
||
315 | |||
316 | /** |
||
317 | * |
||
318 | */ |
||
319 | public function callgraph() |
||
330 | |||
331 | /** |
||
332 | * @return string |
||
333 | * @throws Exception |
||
334 | */ |
||
335 | View Code Duplication | public function callgraphData() |
|
347 | |||
348 | /** |
||
349 | * @return string |
||
350 | */ |
||
351 | View Code Duplication | public function callgraphDataDot() |
|
363 | } |
||
364 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.