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 |
||
29 | class IncidentsController extends AppController |
||
30 | { |
||
31 | public $uses = array('Incident', 'Notification'); |
||
32 | |||
33 | public $components = array('Mailer'); |
||
34 | |||
35 | 1 | public function create() |
|
36 | { |
||
37 | // Only allow POST requests |
||
38 | 1 | $this->request->allowMethod(['post']); |
|
39 | |||
40 | 1 | $bugReport = $this->request->input('json_decode', true); |
|
41 | 1 | $result = $this->Incidents->createIncidentFromBugReport($bugReport); |
|
42 | |||
43 | 1 | if (count($result['incidents']) > 0 |
|
44 | 1 | && !in_array(false, $result['incidents']) |
|
45 | ) { |
||
46 | $response = array( |
||
47 | 1 | 'success' => true, |
|
48 | 1 | 'message' => 'Thank you for your submission', |
|
49 | 1 | 'incident_id' => $result['incidents'], // Return a list of incident ids. |
|
50 | ); |
||
51 | } else { |
||
52 | $response = array( |
||
53 | 1 | 'success' => false, |
|
54 | 'message' => 'There was a problem with your submission.', |
||
55 | ); |
||
56 | } |
||
57 | 1 | $this->autoRender = false; |
|
58 | 1 | $this->response->header(array( |
|
|
|||
59 | 1 | 'Content-Type' => 'application/json', |
|
60 | 'X-Content-Type-Options' => 'nosniff', |
||
61 | )); |
||
62 | 1 | $this->response->body( |
|
63 | 1 | json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) |
|
64 | ); |
||
65 | |||
66 | // For all the newly added reports, |
||
67 | // send notification emails |
||
68 | 1 | foreach ($result['reports'] as $report_id) { |
|
69 | 1 | $this->_sendNotificationMail($report_id); |
|
70 | } |
||
71 | |||
72 | 1 | return $this->response; |
|
73 | } |
||
74 | |||
75 | 1 | public function json($id) |
|
76 | { |
||
77 | 1 | if (!isset($id) || !$id) { |
|
78 | throw new NotFoundException(__('Invalid Incident')); |
||
79 | } |
||
80 | |||
81 | 1 | $this->Incidents->recursive = -1; |
|
82 | 1 | $incident = $this->Incidents->findById($id)->all()->first(); |
|
83 | 1 | if (!$incident) { |
|
84 | throw new NotFoundException(__('Invalid Incident')); |
||
85 | } |
||
86 | |||
87 | 1 | $incident['full_report'] = |
|
88 | 1 | json_decode($incident['full_report'], true); |
|
89 | 1 | $incident['stacktrace'] = |
|
90 | 1 | json_decode($incident['stacktrace'], true); |
|
91 | |||
92 | 1 | $this->autoRender = false; |
|
93 | 1 | $this->response->body(json_encode($incident, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
|
94 | |||
95 | 1 | return $this->response; |
|
96 | } |
||
97 | |||
98 | 1 | public function view($incidentId) |
|
99 | { |
||
100 | 1 | if (!isset($incidentId) || !$incidentId) { |
|
101 | throw new NotFoundException(__('Invalid Incident')); |
||
102 | } |
||
103 | |||
104 | 1 | $incident = $this->Incidents->findById($incidentId)->all()->first(); |
|
105 | 1 | if (!$incident) { |
|
106 | throw new NotFoundException(__('Invalid Incident')); |
||
107 | } |
||
108 | |||
109 | 1 | $incident['full_report'] = |
|
110 | 1 | json_decode($incident['full_report'], true); |
|
111 | 1 | $incident['stacktrace'] = |
|
112 | 1 | json_decode($incident['stacktrace'], true); |
|
113 | |||
114 | 1 | $this->set('incident', $incident); |
|
115 | 1 | } |
|
116 | |||
117 | 1 | private function _sendNotificationMail($reportId) |
|
136 | |||
137 | 1 | protected function _getSimilarFields($id) |
|
157 | } |
||
158 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.