Complex classes like InputTransformer 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 InputTransformer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class InputTransformer |
||
14 | { |
||
15 | const SEPARATOR = '#'; |
||
16 | |||
17 | const KEY_PACKAGE_NAME = 'package-name'; |
||
18 | const KEY_TYPE = 'type'; |
||
19 | const KEY_LICENSE = 'license'; |
||
20 | const KEY_PACKAGE_VERSION = 'package-version'; |
||
21 | const KEY_DESCRIPTION = 'description'; |
||
22 | const KEY_KEYWORD = 'keyword'; |
||
23 | const KEY_AUTHOR = 'author'; |
||
24 | const KEY_PROVIDED_PACKAGE = 'provided-package'; |
||
25 | const KEY_SUGGESTED_PACKAGE = 'suggested-package'; |
||
26 | const KEY_SUPPORT = 'support'; |
||
27 | const KEY_AUTOLOAD_PSR0 = 'autoload-psr0'; |
||
28 | const KEY_AUTOLOAD_PSR4 = 'autoload-psr4'; |
||
29 | const KEY_AUTOLOAD_DEV_PSR0 = 'autoload-dev-psr0'; |
||
30 | const KEY_AUTOLOAD_DEV_PSR4 = 'autoload-dev-psr4'; |
||
31 | const KEY_REQUIRE = 'require'; |
||
32 | const KEY_REQUIRE_DEV = 'require-dev'; |
||
33 | const KEY_SCRIPT = 'script'; |
||
34 | |||
35 | /** |
||
36 | * @param $inputList |
||
37 | * |
||
38 | * @return Configuration |
||
39 | */ |
||
40 | 9 | public function fromCommandLine($inputList) |
|
44 | |||
45 | /** |
||
46 | * @param array $inputList |
||
47 | * |
||
48 | * @return Configuration |
||
49 | */ |
||
50 | 9 | protected function createConfiguration(array $inputList) |
|
51 | { |
||
52 | 9 | return new Configuration( |
|
53 | 9 | $inputList[self::KEY_PACKAGE_NAME], |
|
54 | 9 | $this->getValue($inputList, self::KEY_TYPE, null), |
|
55 | 9 | $this->getValue($inputList, self::KEY_LICENSE, null), |
|
56 | 9 | $this->getValue($inputList, self::KEY_PACKAGE_VERSION, null), |
|
57 | 9 | $this->getValue($inputList, self::KEY_DESCRIPTION, null), |
|
58 | 9 | $this->extractKeywords($inputList), |
|
59 | 9 | $this->extractAuthors($inputList), |
|
60 | 9 | $this->extractProvidedPackages($inputList), |
|
61 | 9 | $this->extractSuggestedPackages($inputList), |
|
62 | 9 | $this->extractSupports($inputList), |
|
63 | 9 | $this->extractAutoloads($inputList), |
|
64 | 9 | $this->extractAutoloadsDev($inputList), |
|
65 | 9 | $this->extractRequiredPackages($inputList), |
|
66 | 9 | $this->extractRequiredDevPackages($inputList), |
|
67 | 9 | $this->extractScripts($inputList) |
|
68 | 9 | ); |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * @param array $inputList |
||
73 | * @param string $key |
||
74 | * @param string $defaultValue |
||
75 | * |
||
76 | * @return string |
||
77 | */ |
||
78 | 9 | protected function getValue(array $inputList, $key, $defaultValue) |
|
79 | { |
||
80 | 9 | return isset($inputList[$key]) ? $inputList[$key] : $defaultValue; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param array $inputList |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | 9 | protected function extractKeywords(array $inputList) |
|
89 | { |
||
90 | 9 | $list = []; |
|
91 | 9 | if (isset($inputList[self::KEY_KEYWORD]) && is_array($inputList[self::KEY_KEYWORD])) { |
|
92 | 1 | foreach ($inputList[self::KEY_KEYWORD] as $keyword) { |
|
93 | 1 | $list[] = $keyword; |
|
94 | 1 | } |
|
95 | 1 | } |
|
96 | |||
97 | 9 | return $list; |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param array $inputList |
||
102 | * |
||
103 | * @return array |
||
104 | */ |
||
105 | 9 | protected function extractAuthors(array $inputList) |
|
106 | { |
||
107 | 9 | $list = []; |
|
108 | 9 | if (isset($inputList[self::KEY_AUTHOR]) && is_array($inputList[self::KEY_AUTHOR])) { |
|
109 | 1 | foreach ($inputList[self::KEY_AUTHOR] as $key => $author) { |
|
110 | 1 | $data = $this->extractDataFromValue($author); |
|
111 | 1 | $name = array_shift($data); |
|
112 | 1 | $email = array_shift($data); |
|
113 | 1 | $role = array_shift($data); |
|
114 | |||
115 | 1 | $list[] = new Author($name, $email, $role); |
|
116 | 1 | } |
|
117 | 1 | } |
|
118 | |||
119 | 9 | return $list; |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * @param array $inputList |
||
124 | * |
||
125 | * @return array |
||
126 | */ |
||
127 | 9 | protected function extractProvidedPackages(array $inputList) |
|
128 | { |
||
129 | 9 | $list = []; |
|
130 | 9 | if (isset($inputList[self::KEY_PROVIDED_PACKAGE]) && is_array($inputList[self::KEY_PROVIDED_PACKAGE])) { |
|
131 | 2 | foreach ($inputList[self::KEY_PROVIDED_PACKAGE] as $rawValue) { |
|
132 | 2 | list ($name, $versionConstraint) = $this->extractDataFromValue($rawValue); |
|
133 | 2 | $list[] = new Package($name, $versionConstraint); |
|
134 | 2 | } |
|
135 | 2 | } |
|
136 | |||
137 | 9 | return $list; |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param array $inputList |
||
142 | * |
||
143 | * @return array |
||
144 | */ |
||
145 | 9 | protected function extractSuggestedPackages(array $inputList) |
|
146 | { |
||
147 | 9 | $list = []; |
|
148 | 9 | if (isset($inputList[self::KEY_SUGGESTED_PACKAGE]) |
|
149 | 9 | && is_array($inputList[self::KEY_SUGGESTED_PACKAGE]) |
|
150 | 9 | ) { |
|
151 | 2 | foreach ($inputList[self::KEY_SUGGESTED_PACKAGE] as $rawValue) { |
|
152 | 2 | $data = $this->extractDataFromValue($rawValue); |
|
153 | 2 | $list[] = new SuggestedPackage( |
|
154 | 2 | array_shift($data), |
|
155 | 2 | implode(self::SEPARATOR, $data) |
|
156 | 2 | ); |
|
157 | 2 | } |
|
158 | 2 | } |
|
159 | |||
160 | 9 | return $list; |
|
161 | } |
||
162 | |||
163 | /** |
||
164 | * @param array $inputList |
||
165 | * |
||
166 | * @return array |
||
167 | */ |
||
168 | 9 | protected function extractSupports(array $inputList) |
|
169 | { |
||
170 | 9 | $list = []; |
|
171 | 9 | if (isset($inputList[self::KEY_SUPPORT]) && is_array($inputList[self::KEY_SUPPORT])) { |
|
172 | 2 | foreach ($inputList[self::KEY_SUPPORT] as $rawValue) { |
|
173 | 2 | $data = $this->extractDataFromValue($rawValue); |
|
174 | 2 | $list[] = new Support(array_shift($data), implode(self::SEPARATOR, $data)); |
|
175 | 2 | } |
|
176 | 2 | } |
|
177 | |||
178 | 9 | return $list; |
|
179 | } |
||
180 | |||
181 | /** |
||
182 | * @param array $inputList |
||
183 | * |
||
184 | * @return array |
||
185 | */ |
||
186 | 9 | protected function extractAutoloads(array $inputList) |
|
187 | { |
||
188 | 9 | $list = []; |
|
189 | // PSR0 |
||
190 | 9 | foreach ($this->extractAutoloadList($inputList, self::KEY_AUTOLOAD_PSR0) as $namespace => $path) { |
|
191 | 2 | $list[] = new Autoload(Autoload::TYPE_PSR0, $path, $namespace); |
|
192 | 9 | } |
|
193 | // PSR-4 |
||
194 | 9 | foreach ($this->extractAutoloadList($inputList, self::KEY_AUTOLOAD_PSR4) as $namespace => $path) { |
|
195 | 2 | $list[] = new Autoload(Autoload::TYPE_PSR4, $path, $namespace); |
|
196 | 9 | } |
|
197 | |||
198 | |||
199 | 9 | return $list; |
|
200 | } |
||
201 | |||
202 | /** |
||
203 | * @param array $inputList |
||
204 | * |
||
205 | * @return array |
||
206 | */ |
||
207 | 9 | protected function extractAutoloadsDev(array $inputList) |
|
208 | { |
||
209 | 9 | $list = []; |
|
210 | // PSR0 |
||
211 | 9 | foreach ($this->extractAutoloadList($inputList, self::KEY_AUTOLOAD_DEV_PSR0) as $namespace => $path) { |
|
212 | 2 | $list[] = new Autoload(Autoload::TYPE_PSR0, $path, $namespace); |
|
213 | 9 | } |
|
214 | // PSR-4 |
||
215 | 9 | foreach ($this->extractAutoloadList($inputList, self::KEY_AUTOLOAD_DEV_PSR4) as $namespace => $path) { |
|
216 | 2 | $list[] = new Autoload(Autoload::TYPE_PSR4, $path, $namespace); |
|
217 | 9 | } |
|
218 | |||
219 | 9 | return $list; |
|
220 | } |
||
221 | |||
222 | /** |
||
223 | * @param array $inputList |
||
224 | * |
||
225 | * @return array |
||
226 | */ |
||
227 | 9 | protected function extractRequiredPackages(array $inputList) |
|
228 | { |
||
229 | 9 | $list = []; |
|
230 | 9 | if (isset($inputList[self::KEY_REQUIRE]) && is_array($inputList[self::KEY_REQUIRE])) { |
|
231 | 2 | foreach ($inputList[self::KEY_REQUIRE] as $rawValue) { |
|
232 | 2 | list ($name, $versionConstraint) = $this->extractDataFromValue($rawValue); |
|
233 | 2 | $list[] = new Package($name, $versionConstraint); |
|
234 | 2 | } |
|
235 | 2 | } |
|
236 | |||
237 | 9 | return $list; |
|
238 | } |
||
239 | /** |
||
240 | * @param array $inputList |
||
241 | * |
||
242 | * @return array |
||
243 | */ |
||
244 | 9 | protected function extractRequiredDevPackages(array $inputList) |
|
245 | { |
||
246 | 9 | $list = []; |
|
247 | 9 | if (isset($inputList[self::KEY_REQUIRE_DEV]) && is_array($inputList[self::KEY_REQUIRE_DEV])) { |
|
248 | 2 | foreach ($inputList[self::KEY_REQUIRE_DEV] as $rawValue) { |
|
249 | 2 | list ($name, $versionConstraint) = $this->extractDataFromValue($rawValue); |
|
250 | 2 | $list[] = new Package($name, $versionConstraint); |
|
251 | 2 | } |
|
252 | 2 | } |
|
253 | |||
254 | 9 | return $list; |
|
255 | } |
||
256 | |||
257 | /** |
||
258 | * @param array $inputList |
||
259 | * |
||
260 | * @return array |
||
261 | */ |
||
262 | 9 | protected function extractScripts(array $inputList) |
|
263 | { |
||
264 | 9 | $list = []; |
|
265 | 9 | if (isset($inputList[self::KEY_SCRIPT]) && is_array($inputList[self::KEY_SCRIPT])) { |
|
266 | 2 | foreach ($inputList[self::KEY_SCRIPT] as $rawValue) { |
|
267 | 2 | list ($name, $command) = $this->extractDataFromValue($rawValue); |
|
268 | 2 | $list[] = new Script($name, $command); |
|
269 | 2 | } |
|
270 | 2 | } |
|
271 | |||
272 | 9 | return $list; |
|
273 | } |
||
274 | |||
275 | /** |
||
276 | * @param string $value |
||
277 | * |
||
278 | * @return array |
||
279 | */ |
||
280 | 9 | protected function extractDataFromValue($value) |
|
284 | |||
285 | /** |
||
286 | * @param array $inputList |
||
287 | * @param string $optionKey |
||
288 | * |
||
289 | * @return array |
||
290 | */ |
||
291 | 9 | protected function extractAutoloadList(array $inputList, $optionKey) |
|
292 | { |
||
293 | 9 | $list = []; |
|
294 | 9 | if (isset($inputList[$optionKey]) && is_array($inputList[$optionKey])) { |
|
295 | 3 | foreach ($inputList[$optionKey] as $rawValue) { |
|
296 | 3 | list ($namespace, $path) = $this->extractDataFromValue($rawValue); |
|
303 | } |
||
304 |