Complex classes like VariableExtractor 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 VariableExtractor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class VariableExtractor |
||
16 | { |
||
17 | |||
18 | const ACCESSOR_ARRAY = 'array'; |
||
19 | const ACCESSOR_GETTER = 'getter'; |
||
20 | const ACCESSOR_ASSERTER = 'asserter'; |
||
21 | const ACCESSOR_PUBLICPROPERTY = 'public'; |
||
22 | |||
23 | /** |
||
24 | * Static interface for instanciating and extracting |
||
25 | * in a single operation. Delegates to getByPath. |
||
26 | * |
||
27 | * @param mixed $subject |
||
28 | * @param string $propertyPath |
||
29 | * @param array $accessors |
||
30 | * @return mixed |
||
31 | */ |
||
32 | public static function extract($subject, $propertyPath, array $accessors = []) |
||
37 | |||
38 | /** |
||
39 | * Static interface for instanciating and extracting |
||
40 | * accessors for each segment of the path. |
||
41 | * |
||
42 | * @param VariableProviderInterface $subject |
||
43 | * @param string $propertyPath |
||
44 | * @return mixed |
||
45 | */ |
||
46 | public static function extractAccessors($subject, $propertyPath) |
||
51 | |||
52 | /** |
||
53 | * Extracts a variable by path, recursively, from the |
||
54 | * subject pass in argument. This implementation supports |
||
55 | * recursive variable references by using {} around sub- |
||
56 | * references, e.g. "array.{index}" will first get the |
||
57 | * "array" variable, then resolve the "index" variable |
||
58 | * before using the value of "index" as name of the property |
||
59 | * to return. So: |
||
60 | * |
||
61 | * $subject = array('foo' => array('bar' => 'baz'), 'key' => 'bar') |
||
62 | * $propertyPath = 'foo.{key}'; |
||
63 | * $result = ...getByPath($subject, $propertyPath); |
||
64 | * // $result value is "baz", because $subject['foo'][$subject['key']] = 'baz'; |
||
65 | * |
||
66 | * @param mixed $subject |
||
67 | * @param string $propertyPath |
||
68 | * @param array $accessors |
||
69 | * @return mixed |
||
70 | */ |
||
71 | public function getByPath($subject, $propertyPath, array $accessors = []) |
||
72 | { |
||
73 | if ($subject instanceof StandardVariableProvider) { |
||
74 | return $subject->getByPath($propertyPath, $accessors); |
||
75 | } |
||
76 | |||
77 | $propertyPath = $this->resolveSubVariableReferences($subject, $propertyPath); |
||
78 | $propertyPathSegments = explode('.', $propertyPath); |
||
79 | foreach ($propertyPathSegments as $index => $pathSegment) { |
||
80 | $accessor = isset($accessors[$index]) ? $accessors[$index] : null; |
||
81 | $subject = $this->extractSingleValue($subject, $pathSegment, $accessor); |
||
82 | if ($subject === null) { |
||
83 | break; |
||
84 | } |
||
85 | } |
||
86 | return $subject; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param VariableProviderInterface $subject |
||
91 | * @param string $propertyPath |
||
92 | * @return array |
||
93 | */ |
||
94 | public function getAccessorsForPath($subject, $propertyPath) |
||
95 | { |
||
96 | $accessors = []; |
||
97 | $propertyPathSegments = explode('.', $propertyPath); |
||
98 | foreach ($propertyPathSegments as $index => $pathSegment) { |
||
99 | $accessor = $this->detectAccessor($subject, $pathSegment); |
||
100 | if ($accessor === null) { |
||
101 | // Note: this may include cases of sub-variable references. When such |
||
102 | // a reference is encountered the accessor chain is stopped and new |
||
103 | // accessors will be detected for the sub-variable and all following |
||
104 | // path segments since the variable is now fully dynamic. |
||
105 | break; |
||
106 | } |
||
107 | $accessors[] = $accessor; |
||
108 | $subject = $this->extractSingleValue($subject, $pathSegment); |
||
109 | } |
||
110 | return $accessors; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @param mixed $subject |
||
115 | * @param string $propertyPath |
||
116 | * @return string |
||
117 | */ |
||
118 | protected function resolveSubVariableReferences($subject, $propertyPath) |
||
129 | |||
130 | /** |
||
131 | * Extracts a single value from an array or object. |
||
132 | * |
||
133 | * @param mixed $subject |
||
134 | * @param string $propertyName |
||
135 | * @param string|null $accessor |
||
136 | * @return mixed |
||
137 | */ |
||
138 | protected function extractSingleValue($subject, $propertyName, $accessor = null) |
||
145 | |||
146 | /** |
||
147 | * Returns TRUE if the data type of $subject is potentially compatible |
||
148 | * with the $accessor. |
||
149 | * |
||
150 | * @param mixed $subject |
||
151 | * @param string $propertyName |
||
152 | * @param string $accessor |
||
153 | * @return boolean |
||
154 | */ |
||
155 | protected function canExtractWithAccessor($subject, $propertyName, $accessor) |
||
169 | |||
170 | /** |
||
171 | * @param mixed $subject |
||
172 | * @param string $propertyName |
||
173 | * @param string $accessor |
||
174 | * @return mixed |
||
175 | */ |
||
176 | protected function extractWithAccessor($subject, $propertyName, $accessor) |
||
193 | |||
194 | /** |
||
195 | * Detect which type of accessor to use when extracting |
||
196 | * $propertyName from $subject. |
||
197 | * |
||
198 | * @param mixed $subject |
||
199 | * @param string $propertyName |
||
200 | * @return string|NULL |
||
201 | */ |
||
202 | protected function detectAccessor($subject, $propertyName) |
||
223 | |||
224 | /** |
||
225 | * Tests whether a property can be extracted through `is*` or `has*` methods. |
||
226 | * |
||
227 | * @param mixed $subject |
||
228 | * @param string $propertyName |
||
229 | * @return bool |
||
230 | */ |
||
231 | protected function isExtractableThroughAsserter($subject, $propertyName) |
||
236 | |||
237 | /** |
||
238 | * Extracts a property through `is*` or `has*` methods. |
||
239 | * |
||
240 | * @param object $subject |
||
241 | * @param string $propertyName |
||
242 | * @return mixed |
||
243 | */ |
||
244 | protected function extractThroughAsserter($subject, $propertyName) |
||
252 | } |
||
253 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: