| Conditions | 15 |
| Paths | 72 |
| Total Lines | 80 |
| Code Lines | 42 |
| 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 |
||
| 84 | public function flush() |
||
| 85 | { |
||
| 86 | $insert = array(); |
||
| 87 | $update = array(); |
||
| 88 | $delete = array(); |
||
| 89 | |||
| 90 | // fill batch objects |
||
| 91 | foreach ($this->pool as $document) { |
||
| 92 | /* @var $document \Sokil\Mongo\Document */ |
||
| 93 | |||
| 94 | // collection |
||
| 95 | $collection = $document->getCollection(); |
||
| 96 | $collectionName = $collection->getName(); |
||
| 97 | |||
| 98 | // persisting |
||
| 99 | switch ($this->pool->offsetGet($document)) { |
||
| 100 | case self::STATE_SAVE: |
||
| 101 | if ($document->isStored()) { |
||
| 102 | if (!isset($update[$collectionName])) { |
||
| 103 | $update[$collectionName] = new \MongoUpdateBatch($collection->getMongoCollection()); |
||
| 104 | } |
||
| 105 | $update[$collectionName]->add(array( |
||
| 106 | 'q' => array( |
||
| 107 | '_id' => $document->getId(), |
||
| 108 | ), |
||
| 109 | 'u' => $document->getOperator()->toArray(), |
||
| 110 | )); |
||
| 111 | } else { |
||
| 112 | if (!isset($insert[$collectionName])) { |
||
| 113 | $insert[$collectionName] = new \MongoInsertBatch($collection->getMongoCollection()); |
||
| 114 | } |
||
| 115 | $insert[$collectionName]->add($document->toArray()); |
||
| 116 | } |
||
| 117 | break; |
||
| 118 | |||
| 119 | case self::STATE_REMOVE: |
||
| 120 | // delete document form db |
||
| 121 | if ($document->isStored()) { |
||
| 122 | if (!isset($delete[$collectionName])) { |
||
| 123 | $delete[$collectionName] = new \MongoDeleteBatch($collection->getMongoCollection()); |
||
| 124 | } |
||
| 125 | $delete[$collectionName]->add(array( |
||
| 126 | 'q' => array( |
||
| 127 | '_id' => $document->getId(), |
||
| 128 | ), |
||
| 129 | 'limit' => 1, |
||
| 130 | )); |
||
| 131 | } |
||
| 132 | // remove link form pool |
||
| 133 | $this->detach($document); |
||
| 134 | break; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | // write operations |
||
| 139 | $writeOptions = array('w' => 1); |
||
| 140 | |||
| 141 | // execute batch insert operations |
||
| 142 | if ($insert) { |
||
|
|
|||
| 143 | foreach ($insert as $collectionName => $collectionInsert) { |
||
| 144 | $collectionInsert->execute($writeOptions); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | // execute batch update operations |
||
| 149 | if ($update) { |
||
| 150 | foreach ($update as $collectionName => $collectionUpdate) { |
||
| 151 | $collectionUpdate->execute($writeOptions); |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | // execute batch delete operations |
||
| 156 | if ($delete) { |
||
| 157 | foreach ($delete as $collectionName => $collectionDelete) { |
||
| 158 | $collectionDelete->execute($writeOptions); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | return $this; |
||
| 163 | } |
||
| 164 | |||
| 189 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.