Conditions | 8 |
Paths | 23 |
Total Lines | 51 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
112 | public function meta(?string $id = null): void |
||
113 | { |
||
114 | $id = (int)$id; |
||
115 | $isEdit = !empty($id); |
||
116 | $pid = $this->getRequest()->getQuery('pid', null); |
||
117 | $isAnswer = !empty($pid); |
||
118 | |||
119 | if ($isAnswer) { |
||
120 | /** @var PostingInterface */ |
||
121 | $parent = $this->Entries->get($pid); |
||
122 | |||
123 | // Don't leak content of forbidden categories |
||
124 | if ($parent->isAnsweringForbidden()) { |
||
125 | throw new SaitoForbiddenException( |
||
126 | 'Access to parent in PostingsController:meta() forbidden.', |
||
127 | ['CurrentUser' => $this->CurrentUser] |
||
128 | ); |
||
129 | } |
||
130 | |||
131 | $this->set('parent', $parent); |
||
132 | } |
||
133 | |||
134 | if ($isEdit) { |
||
135 | /** @var PostingInterface */ |
||
136 | $posting = $this->Entries->get($id); |
||
137 | if (!$posting->isEditingAllowed()) { |
||
138 | throw new SaitoForbiddenException( |
||
139 | 'Access to posting in PostingsController:meta() forbidden.', |
||
140 | ['CurrentUser' => $this->CurrentUser] |
||
141 | ); |
||
142 | } |
||
143 | $this->set('posting', $posting); |
||
144 | } else { |
||
145 | /// We currently don't save drafts for edits |
||
146 | $where = ['user_id' => $this->CurrentUser->getId()]; |
||
147 | ($pid) ? $where['pid'] = $pid : $where[] = 'pid IS NULL'; |
||
148 | $draft = $this->Entries->Drafts->find()->where($where)->first(); |
||
149 | |||
150 | if ($draft) { |
||
151 | $this->set('draft', $draft); |
||
152 | } |
||
153 | } |
||
154 | |||
155 | $settings = Configure::read('Saito.Settings'); |
||
156 | |||
157 | $this->set(compact('isAnswer', 'isEdit', 'settings')); |
||
158 | |||
159 | $action = $isAnswer ? 'answer' : 'thread'; |
||
160 | $categories = $this->CurrentUser->getCategories()->getAll($action, 'list'); |
||
161 | $this->set('categories', $categories); |
||
162 | } |
||
163 | } |
||
164 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.