| Conditions | 10 |
| Paths | 145 |
| Total Lines | 64 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 1 |
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 |
||
| 89 | private function processFile(ObjectId $fileId) |
||
| 90 | { |
||
| 91 | $database = $this->database; |
||
| 92 | $bucketName = $this->bucketName; |
||
| 93 | |||
| 94 | $fcol = $database->selectCollection($bucketName.'.files'); |
||
| 95 | $oldMeta = $fcol->findOne(['_id'=>$fileId]); |
||
| 96 | if(is_null($oldMeta)){ |
||
| 97 | return; |
||
| 98 | } |
||
| 99 | |||
| 100 | $metaMap = [ |
||
| 101 | 'user' => 'metadata.user', |
||
| 102 | 'permissions' => 'metadata.permissions', |
||
| 103 | 'mimetype' => 'metadata.contentType', |
||
| 104 | 'belongsTo' => 'metadata.belongsTo', |
||
| 105 | 'key' => 'metadata.key', |
||
| 106 | 'md5' => 'metadata.md5', |
||
| 107 | 'filename' => 'metadata.name', |
||
| 108 | ]; |
||
| 109 | |||
| 110 | $options = []; |
||
| 111 | $set = []; |
||
| 112 | $unset = []; |
||
| 113 | |||
| 114 | foreach($metaMap as $from => $to){ |
||
| 115 | $exp = explode('.', $from); |
||
| 116 | $fromKey = $exp[0]; |
||
| 117 | $value = $this->getNamespacedValue($from, $oldMeta); |
||
| 118 | if(isset($oldMeta[$from])){ |
||
| 119 | $set[$to] = $value; |
||
| 120 | $unset[$fromKey] = true; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | if(!is_null($oldMeta['name'])){ |
||
| 125 | $set['metadata.name'] = $oldMeta['name']; |
||
| 126 | } |
||
| 127 | |||
| 128 | //'uploadedDate' => 'uploadDate', |
||
| 129 | //'dateuploaded.date' => 'uploadDate', |
||
| 130 | //'dateUploaded.date' => 'uploadDate' |
||
| 131 | $dateMap = [ |
||
| 132 | 'uploadedDate', |
||
| 133 | 'dateuploaded', |
||
| 134 | 'dateUploaded' |
||
| 135 | ]; |
||
| 136 | foreach($dateMap as $key){ |
||
| 137 | if(!is_null($date = $oldMeta[$key])){ |
||
| 138 | $set['uploadDate'] = $date; |
||
| 139 | $unset[$key] = true; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | if(!empty($set)){ |
||
| 144 | $options['$set'] = $set; |
||
| 145 | } |
||
| 146 | if(!empty($unset)){ |
||
| 147 | $options['$unset'] = $unset; |
||
| 148 | } |
||
| 149 | if(!empty($options)){ |
||
| 150 | $fcol->updateOne( |
||
| 151 | ['_id' => $fileId], |
||
| 152 | $options |
||
| 153 | ); |
||
| 156 | } |