Complex classes like StandardVariableProvider 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 StandardVariableProvider, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class StandardVariableProvider implements VariableProviderInterface |
||
| 14 | { |
||
| 15 | const ACCESSOR_ARRAY = 'array'; |
||
| 16 | const ACCESSOR_GETTER = 'getter'; |
||
| 17 | const ACCESSOR_ASSERTER = 'asserter'; |
||
| 18 | const ACCESSOR_PUBLICPROPERTY = 'public'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Variables stored in context |
||
| 22 | * |
||
| 23 | * @var mixed |
||
| 24 | */ |
||
| 25 | protected $variables = []; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Variables, if any, with which to initialize this |
||
| 29 | * VariableProvider. |
||
| 30 | * |
||
| 31 | * @param array $variables |
||
| 32 | */ |
||
| 33 | public function __construct(array $variables = []) |
||
| 34 | { |
||
| 35 | $this->variables = $variables; |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param array $variables |
||
| 40 | * @return VariableProviderInterface |
||
| 41 | */ |
||
| 42 | public function getScopeCopy(array $variables): VariableProviderInterface |
||
| 43 | { |
||
| 44 | if (!isset($variables['settings']) && isset($this->variables['settings'])) { |
||
| 45 | $variables['settings'] = $this->variables['settings']; |
||
| 46 | } |
||
| 47 | return new static($variables); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Set the source data used by this VariableProvider. The |
||
| 52 | * source can be any type, but the type must of course be |
||
| 53 | * supported by the VariableProvider itself. |
||
| 54 | * |
||
| 55 | * @param mixed $source |
||
| 56 | * @return void |
||
| 57 | */ |
||
| 58 | public function setSource($source): void |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @return mixed |
||
| 65 | */ |
||
| 66 | public function getSource() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Get every variable provisioned by the VariableProvider |
||
| 73 | * implementing the interface. Must return an array or |
||
| 74 | * ArrayAccess instance! |
||
| 75 | * |
||
| 76 | * @return array |
||
| 77 | */ |
||
| 78 | public function getAll(): array |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Add a variable to the context |
||
| 85 | * |
||
| 86 | * @param string $identifier Identifier of the variable to add |
||
| 87 | * @param mixed $value The variable's value |
||
| 88 | * @return void |
||
| 89 | */ |
||
| 90 | public function add(string $identifier, $value): void |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get a variable from the context. Throws exception if variable is not found in context. |
||
| 97 | * |
||
| 98 | * If "_all" is given as identifier, all variables are returned in an array, |
||
| 99 | * if one of the other reserved variables are given, their appropriate value |
||
| 100 | * they're representing is returned. |
||
| 101 | * |
||
| 102 | * @param string $identifier |
||
| 103 | * @return mixed The variable value identified by $identifier |
||
| 104 | */ |
||
| 105 | public function get(string $identifier) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Get a variable by dotted path expression, retrieving the |
||
| 112 | * variable from nested arrays/objects one segment at a time. |
||
| 113 | * If the second variable is passed, it is expected to contain |
||
| 114 | * extraction method names (constants from VariableExtractor) |
||
| 115 | * which indicate how each value is extracted. |
||
| 116 | * |
||
| 117 | * @param string $path |
||
| 118 | * @param array $accessors Optional list of accessors (see class constants) |
||
| 119 | * @return mixed |
||
| 120 | */ |
||
| 121 | public function getByPath(string $path, array $accessors = []) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Remove a variable from context. Throws exception if variable is not found in context. |
||
| 136 | * |
||
| 137 | * @param string $identifier The identifier to remove |
||
| 138 | * @return void |
||
| 139 | */ |
||
| 140 | public function remove(string $identifier): void |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Returns an array of all identifiers available in the context. |
||
| 147 | * |
||
| 148 | * @return array Array of identifier strings |
||
| 149 | */ |
||
| 150 | public function getAllIdentifiers(): array |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Checks if this property exists in the VariableContainer. |
||
| 157 | * |
||
| 158 | * @param string $identifier |
||
| 159 | * @return boolean TRUE if $identifier exists, FALSE otherwise |
||
| 160 | */ |
||
| 161 | public function exists(string $identifier): bool |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param string $propertyPath |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | public function getAccessorsForPath(string $propertyPath): array |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Extracts a single value from an array or object. |
||
| 192 | * |
||
| 193 | * @param mixed $subject |
||
| 194 | * @param string $propertyName |
||
| 195 | * @param string|null $accessor |
||
| 196 | * @return mixed |
||
| 197 | */ |
||
| 198 | protected function extractSingleValue($subject, string $propertyName, ?string $accessor = null) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Returns TRUE if the data type of $subject is potentially compatible |
||
| 208 | * with the $accessor. |
||
| 209 | * |
||
| 210 | * @param mixed $subject |
||
| 211 | * @param string $propertyName |
||
| 212 | * @param string $accessor |
||
| 213 | * @return boolean |
||
| 214 | */ |
||
| 215 | protected function canExtractWithAccessor($subject, string $propertyName, string $accessor): bool |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param mixed $subject |
||
| 232 | * @param string $propertyName |
||
| 233 | * @param string|null $accessor |
||
| 234 | * @return mixed |
||
| 235 | */ |
||
| 236 | protected function extractWithAccessor($subject, string $propertyName, ?string $accessor) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Detect which type of accessor to use when extracting |
||
| 256 | * $propertyName from $subject. |
||
| 257 | * |
||
| 258 | * @param mixed $subject |
||
| 259 | * @param string $propertyName |
||
| 260 | * @return string|null |
||
| 261 | */ |
||
| 262 | protected function detectAccessor($subject, string $propertyName): ?string |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Tests whether a property can be extracted through `is*` or `has*` methods. |
||
| 286 | * |
||
| 287 | * @param mixed $subject |
||
| 288 | * @param string $propertyName |
||
| 289 | * @return bool |
||
| 290 | */ |
||
| 291 | protected function isExtractableThroughAsserter($subject, string $propertyName): bool |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Extracts a property through `is*` or `has*` methods. |
||
| 299 | * |
||
| 300 | * @param object $subject |
||
| 301 | * @param string $propertyName |
||
| 302 | * @return mixed |
||
| 303 | */ |
||
| 304 | protected function extractThroughAsserter(object $subject, string $propertyName) |
||
| 312 | } |
||
| 313 |
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: