| Conditions | 13 |
| Paths | 58 |
| Total Lines | 73 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| 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 |
||
| 110 | { |
||
| 111 | return Injector::inst()->get(static::class); |
||
| 112 | } |
||
| 113 | |||
| 114 | public function setVerbose(?bool $verbose = true) : self |
||
| 115 | { |
||
| 116 | $this->verbose = $verbose; |
||
| 117 | return $this; |
||
| 118 | } |
||
| 119 | |||
| 120 | public function setDry(?bool $dryRun = true) : self |
||
| 121 | { |
||
| 122 | $this->dryRun = $dryRun; |
||
| 123 | return $this; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * returns the total number deleted. |
||
| 128 | * |
||
| 129 | * @param DataObject $object |
||
| 130 | * @param bool $verbose |
||
| 131 | */ |
||
| 132 | public function deleteSuperfluousVersions($object, ?bool $verbose = false): int |
||
| 133 | { |
||
| 134 | $this->object = $object; |
||
| 135 | $this->verbose = $verbose; |
||
| 136 | if (false === $this->hasStages()) { |
||
| 137 | if ($this->verbose) { |
||
| 138 | DB::alteration_message('... ... ... Error, no stages', 'deleted'); |
||
| 139 | } |
||
| 140 | |||
| 141 | return 0; |
||
| 142 | } |
||
| 143 | |||
| 144 | if (false === $this->object->isLiveVersion()) { |
||
| 145 | if ($this->verbose) { |
||
| 146 | DB::alteration_message('... ... ... Error, not a live version', 'deleted'); |
||
| 147 | } |
||
| 148 | |||
| 149 | return 0; |
||
| 150 | } |
||
| 151 | |||
| 152 | // array of version IDs to delete |
||
| 153 | // IMPORTANT |
||
| 154 | $this->toDelete[$this->getUniqueKey()] = []; |
||
| 155 | |||
| 156 | // Base table has Versioned data |
||
| 157 | $totalDeleted = 0; |
||
| 158 | |||
| 159 | $myTemplates = $this->findBestSuitedTemplates(); |
||
| 160 | foreach ($myTemplates as $className => $options) { |
||
| 161 | $runner = new $className($this->object, $this->toDelete[$this->getUniqueKey()]); |
||
| 162 | if ($this->verbose) { |
||
| 163 | DB::alteration_message('... ... ... Running ' . $runner->getTitle() . ': ' . $runner->getDescription()); |
||
| 164 | } |
||
| 165 | |||
| 166 | foreach ($options as $key => $value) { |
||
| 167 | $method = 'set' . $key; |
||
| 168 | $runner->{$method}($value); |
||
| 169 | } |
||
| 170 | |||
| 171 | $runner->run(); |
||
| 172 | $this->toDelete[$this->getUniqueKey()] = $runner->getToDelete(); |
||
| 173 | |||
| 174 | if ($this->verbose) { |
||
| 175 | DB::alteration_message('... ... ... total versions to delete now ' . count($this->toDelete[$this->getUniqueKey()])); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | if (! count($this->toDelete[$this->getUniqueKey()])) { |
||
| 180 | return 0; |
||
| 181 | } |
||
| 182 | |||
| 183 | // Ugly (borrowed from DataObject::class), but returns all |
||
| 282 |