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 ArticlesController extends AppController |
||
8 | { |
||
9 | /** |
||
10 | * Helpers. |
||
11 | * |
||
12 | * @var array |
||
13 | */ |
||
14 | public $helpers = ['I18n']; |
||
15 | |||
16 | /** |
||
17 | * Display all articles. |
||
18 | * |
||
19 | * @return void |
||
20 | */ |
||
21 | public function index() |
||
50 | |||
51 | /** |
||
52 | * Add an article. |
||
53 | * |
||
54 | * @return \Cake\Network\Response|void |
||
55 | */ |
||
56 | public function add() |
||
82 | |||
83 | /** |
||
84 | * Edit an Article. |
||
85 | * |
||
86 | * @return \Cake\Network\Response|void |
||
87 | */ |
||
88 | public function edit() |
||
89 | { |
||
90 | $this->loadModel('BlogArticles'); |
||
91 | |||
92 | $this->BlogArticles->locale(I18n::defaultLocale()); |
||
93 | $article = $this->BlogArticles |
||
94 | ->find('translations') |
||
95 | ->where([ |
||
96 | 'BlogArticles.id' => $this->request->id |
||
97 | ]) |
||
98 | ->contain([ |
||
99 | 'BlogAttachments', |
||
100 | 'BlogCategories', |
||
101 | 'Users' => function ($q) { |
||
102 | return $q->find('short'); |
||
103 | } |
||
104 | ]) |
||
105 | ->first(); |
||
106 | |||
107 | //Check if the article is found. |
||
108 | if (empty($article)) { |
||
109 | $this->Flash->error(__d('admin', 'This article doesn\'t exist or has been deleted.')); |
||
110 | |||
111 | return $this->redirect(['action' => 'index']); |
||
112 | } |
||
113 | |||
114 | if ($this->request->is('put')) { |
||
115 | $this->BlogArticles->patchEntity($article, $this->request->data()); |
||
116 | $article->setTranslations($this->request->data); |
||
117 | |||
118 | if ($this->BlogArticles->save($article)) { |
||
119 | $this->Flash->success(__d('admin', 'This article has been updated successfully !')); |
||
120 | |||
121 | return $this->redirect(['action' => 'index']); |
||
122 | } |
||
123 | } |
||
124 | |||
125 | $categories = $this->BlogArticles->BlogCategories->find('list'); |
||
126 | $this->set(compact('article', 'categories')); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Delete an Article and all his comments and likes. |
||
131 | * |
||
132 | * @return \Cake\Network\Response |
||
133 | */ |
||
134 | public function delete() |
||
166 | } |
||
167 |
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.