| Conditions | 10 |
| Paths | 14 |
| Total Lines | 60 |
| 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 |
||
| 50 | public function afterSave(Event $event, EntityInterface $entity, ArrayObject $options): bool |
||
| 51 | { |
||
| 52 | //設定値をentityから取得 |
||
| 53 | $contentsFileConfig = $entity->getContentsFileSettings(); |
||
| 54 | $attachmentModel = TableRegistry::getTableLocator()->get('ContentsFile.Attachments'); |
||
| 55 | foreach ($contentsFileConfig['fields'] as $field => $fieldSettings) { |
||
| 56 | // ファイルの削除を最初に確認 |
||
| 57 | if ($entity->{'delete_' . $field} == true) { |
||
| 58 | // 該当フィールドを削除 |
||
| 59 | if (!$this->fileDelete($entity, [$field])) { |
||
| 60 | return false; |
||
| 61 | } |
||
| 62 | // ファイルの削除に成功したら保存処理は飛ばす |
||
| 63 | continue; |
||
| 64 | } |
||
| 65 | //contents_file_の方に入ったentityをベースに処理する |
||
| 66 | $fileInfo = $entity->{'contents_file_' . $field}; |
||
| 67 | if ( |
||
| 68 | !empty($fileInfo) && |
||
| 69 | //tmp_file_nameがある=アップロードしたファイルがある |
||
| 70 | array_key_exists('tmp_file_name', $fileInfo) |
||
| 71 | ) { |
||
| 72 | // ファイルの削除 |
||
| 73 | $attachmentSaveData = [ |
||
| 74 | 'model' => $fileInfo['model'], |
||
| 75 | 'model_id' => $entity->id, |
||
| 76 | 'field_name' => $fileInfo['field_name'], |
||
| 77 | 'file_name' => $fileInfo['file_name'], |
||
| 78 | 'file_content_type' => $fileInfo['file_content_type'], |
||
| 79 | 'file_size' => $fileInfo['file_size'], |
||
| 80 | ]; |
||
| 81 | if (Configure::read('ContentsFile.Setting.randomFile') === true) { |
||
| 82 | $attachmentSaveData['file_random_path'] = $this->makeRandomPath(); |
||
| 83 | } |
||
| 84 | $attachmentEntity = $attachmentModel->newEntity($attachmentSaveData); |
||
| 85 | //元のデータがあるかfind(あれば元のファイルを消す) |
||
| 86 | $oldAttachmentData = $attachmentModel->find('all') |
||
| 87 | ->where(['model' => $fileInfo['model']]) |
||
| 88 | ->where(['model_id' => $entity->id]) |
||
| 89 | ->where(['field_name' => $fileInfo['field_name']]) |
||
| 90 | ->first(); |
||
| 91 | |||
| 92 | // 通常とS3で画像保存方法の切り替え |
||
| 93 | if (!$this->{Configure::read('ContentsFile.Setting.type') . 'FileSave'}($fileInfo, $fieldSettings, $attachmentSaveData, $oldAttachmentData)) { |
||
| 94 | return false; |
||
| 95 | } |
||
| 96 | |||
| 97 | //元のデータがあれば更新にする |
||
| 98 | if (!empty($oldAttachmentData)) { |
||
| 99 | $attachmentEntity->id = $oldAttachmentData->id; |
||
| 100 | } |
||
| 101 | if (!$attachmentModel->save($attachmentEntity)) { |
||
| 102 | return false; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | return true; |
||
| 108 | |||
| 109 | } |
||
| 110 | |||
| 237 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.