| Conditions | 29 |
| Paths | 66 |
| Total Lines | 82 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 81 | public function revertEntityToTimestamp(AbstractDBElement $element, \DateTime $timestamp, array $reverted_elements = []): void |
||
| 82 | { |
||
| 83 | if (! $element instanceof TimeStampableInterface) { |
||
| 84 | throw new \InvalidArgumentException('$element must have a Timestamp!'); |
||
| 85 | } |
||
| 86 | |||
| 87 | if ($timestamp > new \DateTime('now')) { |
||
| 88 | throw new \InvalidArgumentException('You can not travel to the future (yet)...'); |
||
| 89 | } |
||
| 90 | |||
| 91 | //Skip this process if already were reverted... |
||
| 92 | if (in_array($element, $reverted_elements, true)) { |
||
| 93 | return; |
||
| 94 | } |
||
| 95 | $reverted_elements[] = $element; |
||
| 96 | |||
| 97 | $history = $this->repo->getTimetravelDataForElement($element, $timestamp); |
||
| 98 | |||
| 99 | /* |
||
| 100 | if (!$this->repo->getElementExistedAtTimestamp($element, $timestamp)) { |
||
| 101 | $element = null; |
||
| 102 | return; |
||
| 103 | }*/ |
||
| 104 | |||
| 105 | foreach ($history as $logEntry) { |
||
| 106 | if ($logEntry instanceof ElementEditedLogEntry) { |
||
| 107 | $this->applyEntry($element, $logEntry); |
||
| 108 | } |
||
| 109 | if ($logEntry instanceof CollectionElementDeleted) { |
||
| 110 | //Undelete element and add it to collection again |
||
| 111 | $undeleted = $this->undeleteEntity( |
||
| 112 | $logEntry->getDeletedElementClass(), |
||
| 113 | $logEntry->getDeletedElementID() |
||
| 114 | ); |
||
| 115 | if ($this->repo->getElementExistedAtTimestamp($undeleted, $timestamp)) { |
||
| 116 | $this->revertEntityToTimestamp($undeleted, $timestamp, $reverted_elements); |
||
| 117 | $collection = $this->getField($element, $logEntry->getCollectionName()); |
||
| 118 | if ($collection instanceof Collection) { |
||
| 119 | $collection->add($undeleted); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | // Revert any of the associated elements |
||
| 126 | $metadata = $this->em->getClassMetadata(get_class($element)); |
||
| 127 | $associations = $metadata->getAssociationMappings(); |
||
|
|
|||
| 128 | foreach ($associations as $field => $mapping) { |
||
| 129 | if ( |
||
| 130 | ($element instanceof AbstractStructuralDBElement && ('parts' === $field || 'children' === $field)) |
||
| 131 | || ($element instanceof AttachmentType && 'attachments' === $field) |
||
| 132 | ) { |
||
| 133 | continue; |
||
| 134 | } |
||
| 135 | |||
| 136 | //Revert many to one association (one element in property) |
||
| 137 | if ( |
||
| 138 | ClassMetadata::MANY_TO_ONE === $mapping['type'] |
||
| 139 | || ClassMetadata::ONE_TO_ONE === $mapping['type'] |
||
| 140 | ) { |
||
| 141 | $target_element = $this->getField($element, $field); |
||
| 142 | if (null !== $target_element && $element->getLastModified() > $timestamp) { |
||
| 143 | $this->revertEntityToTimestamp($target_element, $timestamp, $reverted_elements); |
||
| 144 | } |
||
| 145 | } elseif ( //Revert *_TO_MANY associations (collection properties) |
||
| 146 | (ClassMetadata::MANY_TO_MANY === $mapping['type'] |
||
| 147 | || ClassMetadata::ONE_TO_MANY === $mapping['type']) |
||
| 148 | && false === $mapping['isOwningSide'] |
||
| 149 | ) { |
||
| 150 | $target_elements = $this->getField($element, $field); |
||
| 151 | if (null === $target_elements || count($target_elements) > 10) { |
||
| 152 | continue; |
||
| 153 | } |
||
| 154 | foreach ($target_elements as $target_element) { |
||
| 155 | if (null !== $target_element && $element->getLastModified() >= $timestamp) { |
||
| 156 | //Remove the element from collection, if it did not existed at $timestamp |
||
| 157 | if (! $this->repo->getElementExistedAtTimestamp($target_element, $timestamp)) { |
||
| 158 | if ($target_elements instanceof Collection) { |
||
| 159 | $target_elements->removeElement($target_element); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | $this->revertEntityToTimestamp($target_element, $timestamp, $reverted_elements); |
||
| 163 | } |
||
| 234 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.