| Conditions | 20 |
| Paths | 30 |
| Total Lines | 76 |
| Code Lines | 39 |
| Lines | 14 |
| Ratio | 18.42 % |
| 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 |
||
| 112 | private function getDirtyFields(array $newSerializedModel, array $oldSerializedModel, ClassMetadata $classMetadata = null) |
||
| 113 | { |
||
| 114 | $dirtyFields = []; |
||
| 115 | |||
| 116 | foreach ($newSerializedModel as $key => $value) { |
||
| 117 | if (!array_key_exists($key, $oldSerializedModel)) { |
||
| 118 | // a new key has been found, add it to the dirtyFields |
||
| 119 | $dirtyFields[$key] = $value; |
||
| 120 | continue; |
||
| 121 | } |
||
| 122 | |||
| 123 | $oldValue = $oldSerializedModel[$key]; |
||
| 124 | |||
| 125 | $currentRelation = $classMetadata ? $classMetadata->getRelation($key) : null; |
||
| 126 | |||
| 127 | if (!$currentRelation) { |
||
| 128 | if (is_array($value) && !ArrayHelper::arraySame($value, $oldValue) |
||
| 129 | || $value != $oldValue |
||
| 130 | ) { |
||
| 131 | $dirtyFields[$key] = $value; |
||
| 132 | } |
||
| 133 | continue; |
||
| 134 | } |
||
| 135 | |||
| 136 | $currentClassMetadata = $this->mapping->getClassMetadata($currentRelation->getTargetEntity()); |
||
| 137 | |||
| 138 | $idSerializedKey = $currentClassMetadata ? $currentClassMetadata->getIdSerializeKey() : null; |
||
| 139 | |||
| 140 | if ($currentRelation->getType() === Relation::MANY_TO_ONE) { |
||
| 141 | if ($value !== $oldValue) { |
||
| 142 | if (is_string($value) || is_string($oldValue)) { |
||
| 143 | $dirtyFields[$key] = $value; |
||
| 144 | View Code Duplication | } else { |
|
| 145 | $recursiveDiff = $this->getDirtyFields($value, $oldValue, $currentClassMetadata); |
||
| 146 | |||
| 147 | if (!empty($recursiveDiff)) { |
||
| 148 | $recursiveDiff[$idSerializedKey] = static::getEntityId($value, $idSerializedKey); |
||
| 149 | $dirtyFields[$key] = $recursiveDiff; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | continue; |
||
| 155 | } |
||
| 156 | |||
| 157 | // ONE_TO_MANY relation |
||
| 158 | |||
| 159 | if (count($value) != count($oldValue)) { |
||
| 160 | // get all objects ids of new array |
||
| 161 | $dirtyFields[$key] = $this->addIdentifiers($value, [], $idSerializedKey); |
||
| 162 | } |
||
| 163 | |||
| 164 | foreach ($value as $relationKey => $relationValue) { |
||
| 165 | $oldRelationValue = $this->findOldRelation($relationValue, $oldValue, $currentClassMetadata); |
||
| 166 | |||
| 167 | |||
| 168 | if ($relationValue !== $oldRelationValue) { |
||
| 169 | if (is_string($relationValue) || is_string($oldRelationValue)) { |
||
| 170 | $dirtyFields[$key][$relationKey] = $relationValue; |
||
| 171 | } else { |
||
| 172 | $recursiveDiff = $this->getDirtyFields($relationValue, $oldRelationValue, $currentClassMetadata); |
||
| 173 | |||
| 174 | View Code Duplication | if (!empty($recursiveDiff)) { |
|
| 175 | $idSerializedKey = $currentClassMetadata->getIdSerializeKey(); |
||
| 176 | |||
| 177 | $recursiveDiff[$idSerializedKey] = static::getEntityId($relationValue, $idSerializedKey); |
||
| 178 | $dirtyFields[$key][$relationKey] = $recursiveDiff; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | } |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | |||
| 186 | return $dirtyFields; |
||
| 187 | } |
||
| 188 | |||
| 243 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.