| Conditions | 8 |
| Paths | 9 |
| Total Lines | 52 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 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 |
||
| 48 | public function loader() |
||
| 49 | { |
||
| 50 | $this->autoRender = false; |
||
| 51 | |||
| 52 | // 必要なパラメータがない場合はエラー |
||
| 53 | if ( |
||
| 54 | empty($this->request->query['model']) || |
||
| 55 | empty($this->request->query['field_name']) |
||
| 56 | ) { |
||
| 57 | throw new NotFoundException('404 error'); |
||
| 58 | } |
||
| 59 | |||
| 60 | //Entityに接続して設定値を取得 |
||
| 61 | $this->baseModel = TableRegistry::get($this->request->query['model']); |
||
| 62 | |||
| 63 | // このレベルで切り出す |
||
| 64 | $fieldName = $this->request->query['field_name']; |
||
| 65 | $filename = ''; |
||
| 66 | if (!empty($this->request->query['tmp_file_name'])) { |
||
| 67 | $filename = $this->request->query['tmp_file_name']; |
||
| 68 | $filepath = $this->{Configure::read('ContentsFile.Setting.type') . 'TmpFilePath'}($filename); |
||
| 69 | Configure::read('ContentsFile.Setting.Normal.tmpDir') . $filename; |
||
| 70 | } elseif (!empty($this->request->query['model_id'])) { |
||
| 71 | //表示条件をチェックする |
||
| 72 | $checkMethodName = 'contentsFileCheck' . Inflector::camelize($fieldName); |
||
| 73 | if (method_exists($this->baseModel, $checkMethodName)) { |
||
| 74 | //エラーなどの処理はTableに任せる |
||
| 75 | $this->baseModel->{$checkMethodName}($this->request->query['model_id']); |
||
| 76 | } |
||
| 77 | //attachementからデータを取得 |
||
| 78 | $attachmentModel = TableRegistry::get('Attachments'); |
||
| 79 | $attachmentData = $attachmentModel->find('all') |
||
| 80 | ->where(['model' => $this->request->query['model']]) |
||
| 81 | ->where(['model_id' => $this->request->query['model_id']]) |
||
| 82 | ->where(['field_name' => $this->request->query['field_name']]) |
||
| 83 | ->first() |
||
| 84 | ; |
||
| 85 | if (empty($attachmentData)) { |
||
| 86 | throw new NotFoundException('404 error'); |
||
| 87 | } |
||
| 88 | $filename = $attachmentData->file_name; |
||
| 89 | $filepath = $this->{Configure::read('ContentsFile.Setting.type') . 'FilePath'}($attachmentData); |
||
| 90 | |||
| 91 | //通常のセットの時のみresize設定があれば見る |
||
| 92 | if (!empty($this->request->query['resize'])) { |
||
| 93 | $filepath = $this->{Configure::read('ContentsFile.Setting.type') . 'ResizeSet'}($filepath, $this->request->query['resize']); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | $this->fileDownloadHeader($filename); |
||
| 98 | $this->{Configure::read('ContentsFile.Setting.type') . 'Loader'}($filename, $filepath); |
||
|
|
|||
| 99 | } |
||
| 100 | |||
| 188 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: