Total Complexity | 62 |
Total Lines | 262 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
Complex classes like EdmElementComparer 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 EdmElementComparer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | abstract class EdmElementComparer |
||
31 | { |
||
32 | /** |
||
33 | * Returns true if the compared type is semantically equivalent to this type. |
||
34 | * |
||
35 | * @param IEdmElement|null $thisType type being compared |
||
36 | * @param IEdmElement|null $otherType type being compared to |
||
37 | * @return bool equivalence of the two types |
||
38 | */ |
||
39 | public static function isEquivalentTo(?IEdmElement $thisType, ?IEdmElement $otherType): bool |
||
40 | { |
||
41 | if (null === $thisType || null === $otherType) { |
||
42 | return false; |
||
43 | } |
||
44 | |||
45 | $equivalent = true; |
||
46 | $interfaces = class_implements($thisType); |
||
47 | $interfaces = array_filter($interfaces, function ($value) { |
||
48 | return false !== strpos($value, 'AlgoWeb\\ODataMetadata'); |
||
49 | }); |
||
50 | |||
51 | foreach ($interfaces as $rawInterface) { |
||
52 | $bitz = explode('\\', $rawInterface); |
||
53 | $interface = end($bitz); |
||
54 | $methodName = 'is' . $interface . 'EquivalentTo'; |
||
55 | if (!method_exists(self::class, $methodName)) { |
||
56 | continue; |
||
57 | } |
||
58 | if (!in_array($rawInterface, class_implements($otherType))) { |
||
59 | return false; |
||
60 | } |
||
61 | $equivalent &= self::{$methodName}($thisType, $otherType); |
||
62 | } |
||
63 | return boolval($equivalent); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Returns true if the compared type is semantically equivalent to this type. |
||
68 | * Schema types (ISchemaType) are compared by their object refs. |
||
69 | * |
||
70 | * @param IType $thisType type being compared |
||
71 | * @param IType $otherType type being compared to |
||
72 | * @return bool equivalence of the two types |
||
73 | */ |
||
74 | protected static function isITypeEquivalentTo(IType $thisType, IType $otherType): bool |
||
99 | } |
||
100 | |||
101 | protected static function isITypeReferenceEquivalentTo(ITypeReference $thisType, ITypeReference $otherType): bool |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Returns true if function signatures are semantically equivalent. |
||
121 | * Signature includes function name (INamedElement) and its parameter types. |
||
122 | * |
||
123 | * @param IFunctionBase|null $thisFunction reference to the calling object |
||
124 | * @param IFunctionBase|null $otherFunction function being compared to |
||
125 | * @return bool equivalence of signatures of the two functions |
||
126 | */ |
||
127 | public static function isFunctionSignatureEquivalentTo(?IFunctionBase $thisFunction, ?IFunctionBase $otherFunction): bool |
||
128 | { |
||
129 | if (null === $thisFunction || null === $otherFunction) { |
||
130 | return false; |
||
131 | } |
||
132 | |||
133 | if ($thisFunction === $otherFunction) { |
||
134 | return true; |
||
135 | } |
||
136 | |||
137 | if ($thisFunction->getName() != $otherFunction->getName()) { |
||
138 | return false; |
||
139 | } |
||
140 | |||
141 | if (!self::isEquivalentTo($thisFunction->getReturnType(), $otherFunction->getReturnType())) { |
||
142 | return false; |
||
143 | } |
||
144 | |||
145 | $thisTypeKeys = array_keys($thisFunction->getParameters()); |
||
146 | $otherTypeKeys = array_keys($otherFunction->getParameters()); |
||
147 | $keyCount = count($thisTypeKeys); |
||
148 | for ($i = 0; $i < $keyCount; ++$i) { |
||
149 | if (!self::isEquivalentTo( |
||
150 | $thisFunction->getParameters()[$thisTypeKeys[$i]], |
||
151 | $otherFunction->getParameters()[$otherTypeKeys[$i]] |
||
152 | ) |
||
153 | ) { |
||
154 | return false; |
||
155 | } |
||
156 | } |
||
157 | |||
158 | return true; |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Returns true if the compared function parameter is semantically equivalent to this function parameter. |
||
163 | * |
||
164 | * @param IFunctionParameter $thisParameter reference to the calling object |
||
165 | * @param IFunctionParameter $otherParameter function parameter being compared to |
||
166 | * @return bool equivalence of the two function parameters |
||
167 | */ |
||
168 | protected static function IsIFunctionParameterEquivalentTo(IFunctionParameter $thisParameter, IFunctionParameter $otherParameter): bool |
||
169 | { |
||
170 | if ($thisParameter === $otherParameter) { |
||
171 | return true; |
||
172 | } |
||
173 | |||
174 | return $thisParameter->getName() == $otherParameter->getName() && |
||
175 | $thisParameter->getMode()->equals($otherParameter->getMode()) && |
||
176 | self::isEquivalentTo($thisParameter->getType(), $otherParameter->getType()); |
||
177 | } |
||
178 | |||
179 | protected static function isIPrimitiveTypeEquivalentTo(IPrimitiveType $thisType, IPrimitiveType $otherType): bool |
||
180 | { |
||
181 | // OdataMetadata creates one-off instances of primitive type definitions that match by name and kind, but have |
||
182 | // different object refs. So we can't use object ref comparison here like for other ISchemaType objects. |
||
183 | return $thisType->getPrimitiveKind() === $otherType->getPrimitiveKind() && |
||
184 | $thisType->FullName() === $otherType->FullName(); |
||
185 | } |
||
186 | |||
187 | protected static function isISchemaTypeEquivalentTo(ISchemaType $thisType, ISchemaType $otherType): bool |
||
188 | { |
||
189 | return $thisType === $otherType; |
||
190 | } |
||
191 | |||
192 | protected static function isICollectionTypeEquivalentTo(ICollectionType $thisType, ICollectionType $otherType): bool |
||
193 | { |
||
194 | return self::isEquivalentTo($thisType->getElementType(), $otherType->getElementType()); |
||
195 | } |
||
196 | |||
197 | protected static function isIEntityReferenceTypeEquivalentTo(IEntityReferenceType $thisType, IEntityReferenceType $otherType): bool |
||
198 | { |
||
199 | return self::isEquivalentTo($thisType->getEntityType(), $otherType->getEntityType()); |
||
200 | } |
||
201 | |||
202 | protected static function isIRowTypeEquivalentTo(IRowType $thisType, IRowType $otherType): bool |
||
203 | { |
||
204 | if (count($thisType->getDeclaredProperties()) != count($otherType->getDeclaredProperties())) { |
||
205 | return false; |
||
206 | } |
||
207 | |||
208 | $thisTypeKeys = array_keys($thisType->getDeclaredProperties()); |
||
209 | $otherTypeKeys = array_keys($otherType->getDeclaredProperties()); |
||
210 | $keyCount = count($thisTypeKeys); |
||
211 | for ($i = 0; $i < $keyCount; ++$i) { |
||
212 | if (!self::isEquivalentTo( |
||
213 | $thisType->getDeclaredProperties()[$thisTypeKeys[$i]], |
||
214 | $thisType->getDeclaredProperties()[$otherTypeKeys[$i]] |
||
215 | ) |
||
216 | ) { |
||
217 | return false; |
||
218 | } |
||
219 | } |
||
220 | return true; |
||
221 | } |
||
222 | |||
223 | protected static function isIStructuralPropertyEquivalentTo(IStructuralProperty $thisProp, IStructuralProperty $otherProp): bool |
||
224 | { |
||
225 | if ($thisProp === $otherProp) { |
||
226 | return true; |
||
227 | } |
||
228 | |||
229 | return $thisProp->getName() == $otherProp->getName() && |
||
230 | self::isEquivalentTo($thisProp->getType(), $otherProp->getType()); |
||
231 | } |
||
232 | |||
233 | |||
234 | protected static function isIPrimitiveTypeReferenceEquivalentTo(IPrimitiveTypeReference $thisType, IPrimitiveTypeReference $otherType): bool |
||
235 | { |
||
236 | $thisTypePrimitiveKind = $thisType->PrimitiveKind(); |
||
237 | if (!$thisTypePrimitiveKind->equals($otherType->PrimitiveKind())) { |
||
238 | return false; |
||
239 | } |
||
240 | |||
241 | if ($thisTypePrimitiveKind->isAnyOf( |
||
242 | PrimitiveTypeKind::Binary(), |
||
243 | PrimitiveTypeKind::Decimal(), |
||
244 | PrimitiveTypeKind::String(), |
||
245 | PrimitiveTypeKind::Time(), |
||
246 | PrimitiveTypeKind::DateTime(), |
||
247 | PrimitiveTypeKind::DateTimeOffset() |
||
248 | ) || |
||
249 | $thisTypePrimitiveKind->IsSpatial()) { |
||
250 | return $thisType->getNullable() === $otherType->getNullable() && |
||
251 | self::isEquivalentTo($thisType->getDefinition(), $otherType->getDefinition()); |
||
252 | } |
||
253 | return true; |
||
254 | } |
||
255 | |||
256 | protected static function isIBinaryTypeReferenceEquivalentTo(IBinaryTypeReference $thisType, IBinaryTypeReference $otherType): bool |
||
257 | { |
||
258 | return $thisType->getNullable() === $otherType->getNullable() && |
||
259 | $thisType->isFixedLength() === $otherType->isFixedLength() && |
||
260 | $thisType->isUnbounded() === $otherType->isUnbounded() && |
||
261 | $thisType->getMaxLength() === $otherType->getMaxLength(); |
||
262 | } |
||
263 | |||
264 | protected static function isIDecimalTypeReferenceEquivalentTo(IDecimalTypeReference $thisType, IDecimalTypeReference $otherType): bool |
||
265 | { |
||
266 | return $thisType->getNullable() === $otherType->getNullable() && |
||
267 | $thisType->getPrecision() === $otherType->getPrecision() && |
||
268 | $thisType->getScale() === $otherType->getScale(); |
||
269 | } |
||
270 | |||
271 | protected static function isITemporalTypeReferenceEquivalentTo(ITemporalTypeReference $thisType, ITemporalTypeReference $otherType): bool |
||
276 | } |
||
277 | |||
278 | protected static function isIStringTypeReferenceEquivalentTo(IStringTypeReference $thisType, IStringTypeReference $otherType): bool |
||
279 | { |
||
280 | return $thisType->getNullable() === $otherType->getNullable() && |
||
281 | $thisType->isFixedLength() === $otherType->isFixedLength() && |
||
282 | $thisType->isUnbounded() === $otherType->isUnbounded() && |
||
283 | $thisType->getMaxLength() === $otherType->getMaxLength() && |
||
284 | $thisType->isUnicode() === $otherType->isUnicode() && |
||
285 | $thisType->getCollation() === $otherType->getCollation(); |
||
286 | } |
||
287 | |||
288 | protected static function isISpatialTypeReferenceEquivalentTo(ISpatialTypeReference $thisType, ISpatialTypeReference $otherType): bool |
||
292 | } |
||
293 | } |
||
294 |