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 |
||
7 | class CategoriesController extends AppController |
||
8 | { |
||
9 | |||
10 | /** |
||
11 | * Display all categories. |
||
12 | * |
||
13 | * @return void |
||
14 | */ |
||
15 | public function index() |
||
32 | |||
33 | /** |
||
34 | * Add a category. |
||
35 | * |
||
36 | * @return \Cake\Network\Response|void |
||
37 | */ |
||
38 | public function add() |
||
57 | |||
58 | /** |
||
59 | * Edit a category. |
||
60 | * |
||
61 | * @return \Cake\Network\Response|void |
||
62 | */ |
||
63 | public function edit() |
||
64 | { |
||
65 | $this->loadModel('BlogCategories'); |
||
66 | |||
67 | $this->BlogCategories->locale(I18n::defaultLocale()); |
||
68 | $category = $this->BlogCategories |
||
69 | ->find('translations') |
||
70 | ->where([ |
||
71 | 'BlogCategories.id' => $this->request->id |
||
72 | ]) |
||
73 | ->first(); |
||
74 | |||
75 | //Check if the category is found. |
||
76 | if (empty($category)) { |
||
77 | $this->Flash->error(__d('admin', 'This category doesn\'t exist or has been deleted.')); |
||
78 | |||
79 | return $this->redirect(['action' => 'index']); |
||
80 | } |
||
81 | |||
82 | View Code Duplication | if ($this->request->is('put')) { |
|
83 | $this->BlogCategories->patchEntity($category, $this->request->getParsedBody()); |
||
84 | $category->setTranslations($this->request->getParsedBody()); |
||
85 | |||
86 | if ($this->BlogCategories->save($category)) { |
||
87 | $this->Flash->success(__d('admin', 'This category has been updated successfully !')); |
||
88 | |||
89 | return $this->redirect(['action' => 'index']); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | $this->set(compact('category')); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Delete a category and all his comments and likes. |
||
98 | * |
||
99 | * @return \Cake\Network\Response |
||
100 | */ |
||
101 | public function delete() |
||
136 | } |
||
137 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.