Conditions | 8 |
Paths | 9 |
Total Lines | 53 |
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 |
||
49 | public function loader(): void |
||
50 | { |
||
51 | $this->autoRender = false; |
||
52 | |||
53 | // 必要なパラメータがない場合はエラー |
||
54 | if ( |
||
55 | empty($this->request->getQuery('model')) || |
||
56 | empty($this->request->getQuery('field_name')) |
||
57 | ) { |
||
58 | throw new NotFoundException('404 error'); |
||
59 | } |
||
60 | |||
61 | //Entityに接続して設定値を取得 |
||
62 | $this->baseModel = TableRegistry::getTableLocator()->get($this->request->getQuery('model')); |
||
|
|||
63 | |||
64 | // このレベルで切り出す |
||
65 | $fieldName = $this->request->getQuery('field_name'); |
||
66 | $filename = ''; |
||
67 | $filepath = ''; |
||
68 | if (!empty($this->request->getQuery('tmp_file_name'))) { |
||
69 | $filename = $this->request->getQuery('tmp_file_name'); |
||
70 | $filepath = $this->{Configure::read('ContentsFile.Setting.type') . 'TmpFilePath'}($filename); |
||
71 | Configure::read('ContentsFile.Setting.Normal.tmpDir') . $filename; |
||
72 | } elseif (!empty($this->request->getQuery('model_id'))) { |
||
73 | //表示条件をチェックする |
||
74 | $checkMethodName = 'contentsFileCheck' . Inflector::camelize($fieldName); |
||
75 | if (method_exists($this->baseModel, $checkMethodName)) { |
||
76 | //エラーなどの処理はTableに任せる |
||
77 | $this->baseModel->{$checkMethodName}($this->request->getQuery('model_id')); |
||
78 | } |
||
79 | //attachementからデータを取得 |
||
80 | $attachmentModel = TableRegistry::getTableLocator()->get('Attachments'); |
||
81 | $attachmentData = $attachmentModel->find('all') |
||
82 | ->where(['model' => $this->request->getQuery('model')]) |
||
83 | ->where(['model_id' => $this->request->getQuery('model_id')]) |
||
84 | ->where(['field_name' => $this->request->getQuery('field_name')]) |
||
85 | ->first() |
||
86 | ; |
||
87 | if (empty($attachmentData)) { |
||
88 | throw new NotFoundException('404 error'); |
||
89 | } |
||
90 | $filename = $attachmentData->file_name; |
||
91 | $filepath = $this->{Configure::read('ContentsFile.Setting.type') . 'FilePath'}($attachmentData); |
||
92 | |||
93 | //通常のセットの時のみresize設定があれば見る |
||
94 | if (!empty($this->request->getQuery('resize'))) { |
||
95 | $filepath = $this->{Configure::read('ContentsFile.Setting.type') . 'ResizeSet'}($filepath, $this->request->getQuery('resize')); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | $this->fileDownloadHeader($filename); |
||
100 | $this->{Configure::read('ContentsFile.Setting.type') . 'Loader'}($filename, $filepath); |
||
101 | } |
||
102 | |||
205 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.