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 KeyDescriptor 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 KeyDescriptor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 53 | class KeyDescriptor |
||
|
|
|||
| 54 | { |
||
| 55 | /** |
||
| 56 | * Holds collection of named key values |
||
| 57 | * For e.g. the keypredicate Order_Details(OrderID=10248,ProductID=11) will |
||
| 58 | * stored in this array as: |
||
| 59 | * Array([OrderID] => Array( [0] => 10248 [1] => Object(Int32)), |
||
| 60 | * [ProductID] => Array( [0] => 11 [1] => Object(Int32))) |
||
| 61 | * Note: This is mutually exclusive with $_positionalValues. These values |
||
| 62 | * are not validated against entity's ResourceType, validation will happen |
||
| 63 | * once validate function is called, $_validatedNamedValues will hold |
||
| 64 | * validated values. |
||
| 65 | * |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | private $namedValues = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Holds collection of positional key values |
||
| 72 | * For e.g. the keypredicate Order_Details(10248, 11) will |
||
| 73 | * stored in this array as: |
||
| 74 | * Array([0] => Array( [0] => 10248 [1] => Object(Int32)), |
||
| 75 | * [1] => Array( [0] => 11 [1] => Object(Int32))) |
||
| 76 | * Note: This is mutually exclusive with $_namedValues. These values are not |
||
| 77 | * validated against entity's ResourceType, validation will happen once validate |
||
| 78 | * function is called, $_validatedNamedValues will hold validated values. |
||
| 79 | * |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | private $positionalValues = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Holds collection of positional or named values as named values. The validate |
||
| 86 | * function populates this collection. |
||
| 87 | * |
||
| 88 | * @var array |
||
| 89 | */ |
||
| 90 | private $validatedNamedValues = []; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Creates new instance of KeyDescriptor |
||
| 94 | * Note: The arguments $namedValues and $positionalValues are mutually |
||
| 95 | * exclusive. Either both or one will be empty array. |
||
| 96 | * |
||
| 97 | * @param array $namedValues Collection of named key values |
||
| 98 | * @param array $positionalValues Collection of positional key values |
||
| 99 | */ |
||
| 100 | private function __construct(array $namedValues, array $positionalValues) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param string $keyString |
||
| 123 | * @param bool $isKey |
||
| 124 | * @param KeyDescriptor|null $keyDescriptor |
||
| 125 | * @return bool |
||
| 126 | */ |
||
| 127 | protected static function parseAndVerifyRawKeyPredicate($keyString, $isKey, KeyDescriptor &$keyDescriptor = null) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Gets collection of named key values. |
||
| 142 | * |
||
| 143 | * @return array[] |
||
| 144 | */ |
||
| 145 | public function getNamedValues() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Gets collection of positional key values. |
||
| 152 | * |
||
| 153 | * @return array[] |
||
| 154 | */ |
||
| 155 | public function getPositionalValues() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Gets collection of positional key values by reference. |
||
| 162 | * |
||
| 163 | * @return array[] |
||
| 164 | */ |
||
| 165 | public function &getPositionalValuesByRef() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Gets validated named key values, this array will be populated |
||
| 172 | * in validate function. |
||
| 173 | * |
||
| 174 | * @throws InvalidOperationException If this function invoked before invoking validate function |
||
| 175 | * |
||
| 176 | * @return array[] |
||
| 177 | */ |
||
| 178 | public function getValidatedNamedValues() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Checks whether the key values have name. |
||
| 191 | * |
||
| 192 | * @return bool |
||
| 193 | */ |
||
| 194 | public function areNamedValues() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Check whether this KeyDesciption has any key values. |
||
| 201 | * |
||
| 202 | * @return bool |
||
| 203 | */ |
||
| 204 | public function isEmpty() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Gets number of values in the key. |
||
| 212 | * |
||
| 213 | * @return int |
||
| 214 | */ |
||
| 215 | public function valueCount() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Attempts to parse value(s) of resource key(s) from the given key predicate |
||
| 228 | * and creates instance of KeyDescription representing the same, Once parsing |
||
| 229 | * is done one should call validate function to validate the created |
||
| 230 | * KeyDescription. |
||
| 231 | * |
||
| 232 | * @param string $keyPredicate The predicate to parse |
||
| 233 | * @param KeyDescriptor|null $keyDescriptor On return, Description of key after parsing |
||
| 234 | * |
||
| 235 | * @return bool True if the given values were parsed; false if there was a syntax error |
||
| 236 | */ |
||
| 237 | public static function tryParseKeysFromKeyPredicate( |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Attempt to parse comma separated values representing a skiptoken and creates |
||
| 248 | * instance of KeyDescriptor representing the same. |
||
| 249 | * |
||
| 250 | * @param string $skipToken The skiptoken value to parse |
||
| 251 | * @param KeyDescriptor &$keyDescriptor On return, Description of values |
||
| 252 | * after parsing |
||
| 253 | * |
||
| 254 | * @return bool True if the given values were parsed; false if there was a syntax error |
||
| 255 | */ |
||
| 256 | public static function tryParseValuesFromSkipToken($skipToken, &$keyDescriptor) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Validate this KeyDescriptor, If valid, this function populates |
||
| 265 | * _validatedNamedValues array with key as keyName and value as an array of |
||
| 266 | * key value and key type. |
||
| 267 | * |
||
| 268 | * @param string $segmentAsString The segment in the form identifier |
||
| 269 | * (keyPredicate) which this descriptor |
||
| 270 | * represents |
||
| 271 | * @param ResourceType $resourceType The type of the identifier in the segment |
||
| 272 | * |
||
| 273 | * @throws ODataException If validation fails |
||
| 274 | */ |
||
| 275 | public function validate($segmentAsString, ResourceType $resourceType) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Attempts to parse value(s) of resource key(s) from the key predicate and |
||
| 366 | * creates instance of KeyDescription representing the same, Once parsing is |
||
| 367 | * done, one should call validate function to validate the created KeyDescription. |
||
| 368 | * |
||
| 369 | * @param string $keyPredicate The key predicate to parse |
||
| 370 | * @param bool $allowNamedValues Set to true if parser should accept |
||
| 371 | * named values(Property = KeyValue), |
||
| 372 | * if false then parser will fail on |
||
| 373 | * such constructs |
||
| 374 | * @param bool $allowNull Set to true if parser should accept |
||
| 375 | * null values for positional key |
||
| 376 | * values, if false then parser will |
||
| 377 | * fail on seeing null values |
||
| 378 | * @param KeyDescriptor &$keyDescriptor On return, Description of key after |
||
| 379 | * parsing |
||
| 380 | * |
||
| 381 | * @return bool True if the given values were parsed; false if there was a syntax error |
||
| 382 | */ |
||
| 383 | private static function tryParseKeysFromRawKeyPredicate( |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Get the type of an Astoria URI key value, validate the value against the type. If valid, this function |
||
| 487 | * provides the PHP value equivalent to the Astoria URI key value. |
||
| 488 | * |
||
| 489 | * @param string $value The Astoria URI key value |
||
| 490 | * @param ExpressionTokenId $tokenId The tokenId for $value literal |
||
| 491 | * @param mixed|null &$outValue After the invocation, this parameter holds the PHP equivalent to $value, |
||
| 492 | * if $value is not valid then this parameter will be null |
||
| 493 | * @param IType|null &$outType After the invocation, this parameter holds the type of $value, if $value is |
||
| 494 | * not a valid key value type then this parameter will be null |
||
| 495 | * |
||
| 496 | * @return bool True if $value is a valid type, else false |
||
| 497 | */ |
||
| 498 | private static function getTypeAndValidateKeyValue($value, $tokenId, &$outValue, &$outType) |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Generate relative edit url for this key descriptor and supplied resource set |
||
| 548 | * |
||
| 549 | * @param ResourceSet $resourceSet |
||
| 550 | * |
||
| 551 | * @return string |
||
| 552 | * @throws \InvalidArgumentException |
||
| 553 | */ |
||
| 554 | public function generateRelativeUri(ResourceSet $resourceSet) |
||
| 585 | } |
||
| 586 |