| Total Complexity | 42 |
| Total Lines | 137 |
| Duplicated Lines | 10.22 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Operations often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Operations, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Operations |
||
| 10 | { |
||
| 11 | const KEY_PROPERTIES = 'properties'; |
||
| 12 | const KEY_TYPE = 'type'; |
||
| 13 | |||
| 14 | public static function merge(MappingInterface ...$mappings): Mapping |
||
| 15 | { |
||
| 16 | $target = new Mapping(); |
||
| 17 | foreach ($mappings as $source) { |
||
| 18 | if ($source->getType()) { |
||
|
|
|||
| 19 | $target->setType($source->getType()); |
||
| 20 | } |
||
| 21 | foreach ($source->getParameters() as $parameter => $value) { |
||
| 22 | $target->setParameter($parameter, $value); |
||
| 23 | } |
||
| 24 | foreach ($source->getProperties() as $property => $mapping) { |
||
| 25 | $candidate = $target->getProperty($property); |
||
| 26 | $target->setProperty($property, $candidate ? static::merge($candidate, $mapping) : $mapping); |
||
| 27 | } |
||
| 28 | } |
||
| 29 | return $target; |
||
| 30 | } |
||
| 31 | |||
| 32 | public static function from(MappingInterface $mapping): Mapping |
||
| 33 | { |
||
| 34 | return static::merge($mapping); |
||
| 35 | } |
||
| 36 | public static function conflict(MappingInterface ...$mappings): bool |
||
| 37 | { |
||
| 38 | $mappings = array_values($mappings); |
||
| 39 | for ($i = 0; $i < sizeof($mappings) - 1; $i++) { |
||
| 40 | $subject = $mappings[$i]; |
||
| 41 | for ($j = $i + 1; $j < sizeof($mappings); $j++) { |
||
| 42 | if (static::haveConflicts($subject, $mappings[$j])) { |
||
| 43 | return true; |
||
| 44 | } |
||
| 45 | } |
||
| 46 | } |
||
| 47 | return false; |
||
| 48 | } |
||
| 49 | |||
| 50 | private static function haveConflicts(MappingInterface $subject, MappingInterface $opponent): bool |
||
| 51 | { |
||
| 52 | if (static::haveTypeConflict($subject, $opponent)) { |
||
| 53 | return true; |
||
| 54 | } |
||
| 55 | if (static::haveParameterConflicts($subject, $opponent)) { |
||
| 56 | return true; |
||
| 57 | } |
||
| 58 | return static::havePropertyConflicts($subject, $opponent); |
||
| 59 | } |
||
| 60 | |||
| 61 | private static function haveTypeConflict(MappingInterface $subject, MappingInterface $opponent): bool |
||
| 64 | } |
||
| 65 | |||
| 66 | private static function haveParameterConflicts(MappingInterface $subject, MappingInterface $opponent): bool |
||
| 67 | { |
||
| 74 | } |
||
| 75 | |||
| 76 | private static function havePropertyConflicts(MappingInterface $subject, MappingInterface $opponent): bool |
||
| 84 | } |
||
| 85 | |||
| 86 | public static function toArray(MappingInterface $mapping): array |
||
| 103 | } |
||
| 104 | |||
| 105 | public static function toStdObject(MappingInterface $mapping) |
||
| 106 | { |
||
| 107 | $target = new stdClass(); |
||
| 108 | foreach ($mapping->getParameters() as $parameter => $value) { |
||
| 109 | $target->$parameter = $value; |
||
| 110 | } |
||
| 111 | View Code Duplication | if (!empty($mapping->getProperties())) { |
|
| 112 | $properties = []; |
||
| 113 | foreach ($mapping->getProperties() as $name => $property) { |
||
| 114 | $properties[$name] = static::toStdObject($property); |
||
| 115 | } |
||
| 116 | $target->properties = $properties; |
||
| 117 | } |
||
| 118 | if ($mapping->getType()) { |
||
| 119 | $target->type = $mapping->getType(); |
||
| 120 | } |
||
| 121 | return $target; |
||
| 122 | } |
||
| 123 | |||
| 124 | public static function fromArray(array $source): Mapping |
||
| 146 | } |
||
| 147 | } |
||
| 148 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: