Total Complexity | 80 |
Total Lines | 572 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like PropertyTask 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 PropertyTask, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
46 | class PropertyTask extends Task |
||
47 | { |
||
48 | use FilterChainAware; |
||
49 | |||
50 | /** |
||
51 | * @var string name of the property |
||
52 | */ |
||
53 | protected $name; |
||
54 | |||
55 | /** |
||
56 | * @var string of the property |
||
57 | */ |
||
58 | protected $value; |
||
59 | |||
60 | /** |
||
61 | * @var Reference |
||
62 | */ |
||
63 | protected $reference; |
||
64 | |||
65 | /** |
||
66 | * @var string environment |
||
67 | */ |
||
68 | protected $env; |
||
69 | |||
70 | /** |
||
71 | * @var File |
||
72 | */ |
||
73 | protected $file; |
||
74 | |||
75 | /** |
||
76 | * @var string |
||
77 | */ |
||
78 | protected $prefix; |
||
79 | |||
80 | /** |
||
81 | * @var Project |
||
82 | */ |
||
83 | protected $fallback; |
||
84 | |||
85 | /** |
||
86 | * Whether to force overwrite of existing property. |
||
87 | */ |
||
88 | protected $override = false; |
||
89 | |||
90 | /** |
||
91 | * Whether property should be treated as "user" property. |
||
92 | */ |
||
93 | protected $userProperty = false; |
||
94 | |||
95 | /** |
||
96 | * Whether to log messages as INFO or VERBOSE. |
||
97 | */ |
||
98 | protected $logOutput = true; |
||
99 | |||
100 | /** |
||
101 | * @var FileParserFactoryInterface |
||
102 | */ |
||
103 | private $fileParserFactory; |
||
104 | |||
105 | /** |
||
106 | * Whether a warning should be displayed when the property ismissing. |
||
107 | */ |
||
108 | private $quiet = false; |
||
109 | |||
110 | /** |
||
111 | * Whether the task should fail when the property file is not found. |
||
112 | */ |
||
113 | private $required = false; |
||
114 | |||
115 | /** |
||
116 | * @param FileParserFactoryInterface|null $fileParserFactory |
||
117 | */ |
||
118 | public function __construct(?FileParserFactoryInterface $fileParserFactory = null) |
||
119 | { |
||
120 | parent::__construct(); |
||
121 | $this->fileParserFactory = $fileParserFactory ?? new FileParserFactory(); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @return string |
||
126 | */ |
||
127 | public function __toString() |
||
128 | { |
||
129 | return $this->value; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * File required or not. |
||
134 | * |
||
135 | * @param string $d |
||
136 | */ |
||
137 | public function setRequired($d) |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @return string |
||
144 | */ |
||
145 | public function getRequired() |
||
146 | { |
||
147 | return $this->required; |
||
|
|||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Sets a the name of current property component. |
||
152 | */ |
||
153 | public function setName(string $name): void |
||
154 | { |
||
155 | $this->name = $name; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Get property component name. |
||
160 | */ |
||
161 | public function getName() |
||
162 | { |
||
163 | return $this->name; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Sets a the value of current property component. |
||
168 | * |
||
169 | * @param string $value Value of name, all scalars allowed |
||
170 | */ |
||
171 | public function setValue(string $value): void |
||
172 | { |
||
173 | $this->value = $value; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Sets value of property to CDATA tag contents. |
||
178 | * |
||
179 | * @since 2.2.0 |
||
180 | */ |
||
181 | public function addText(string $value): void |
||
182 | { |
||
183 | $this->setValue($value); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Get the value of current property component. |
||
188 | */ |
||
189 | public function getValue() |
||
190 | { |
||
191 | return $this->value; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Set a file to use as the source for properties. |
||
196 | * |
||
197 | * @param File|string $file |
||
198 | * |
||
199 | * @throws IOException |
||
200 | */ |
||
201 | public function setFile($file) |
||
202 | { |
||
203 | if (is_string($file)) { |
||
204 | $file = new File($file); |
||
205 | } |
||
206 | $this->file = $file; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Get the PhingFile that is being used as property source. |
||
211 | */ |
||
212 | public function getFile() |
||
213 | { |
||
214 | return $this->file; |
||
215 | } |
||
216 | |||
217 | public function setRefid(Reference $ref): void |
||
218 | { |
||
219 | $this->reference = $ref; |
||
220 | } |
||
221 | |||
222 | public function getRefid() |
||
223 | { |
||
224 | return $this->reference; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Prefix to apply to properties loaded using <code>file</code>. |
||
229 | * A "." is appended to the prefix if not specified. |
||
230 | * |
||
231 | * @param string $prefix prefix string |
||
232 | * |
||
233 | * @since 2.0 |
||
234 | */ |
||
235 | public function setPrefix(string $prefix): void |
||
236 | { |
||
237 | $this->prefix = $prefix; |
||
238 | if (!StringHelper::endsWith('.', $prefix)) { |
||
239 | $this->prefix .= '.'; |
||
240 | } |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * @return string |
||
245 | * |
||
246 | * @since 2.0 |
||
247 | */ |
||
248 | public function getPrefix() |
||
249 | { |
||
250 | return $this->prefix; |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * the prefix to use when retrieving environment variables. |
||
255 | * Thus if you specify environment="myenv" |
||
256 | * you will be able to access OS-specific |
||
257 | * environment variables via property names "myenv.PATH" or |
||
258 | * "myenv.TERM". |
||
259 | * <p> |
||
260 | * Note that if you supply a property name with a final |
||
261 | * "." it will not be doubled. ie environment="myenv." will still |
||
262 | * allow access of environment variables through "myenv.PATH" and |
||
263 | * "myenv.TERM". This functionality is currently only implemented |
||
264 | * on select platforms. Feel free to send patches to increase the number of platforms |
||
265 | * this functionality is supported on ;).<br> |
||
266 | * Note also that properties are case sensitive, even if the |
||
267 | * environment variables on your operating system are not, e.g. it |
||
268 | * will be ${env.Path} not ${env.PATH} on Windows 2000. |
||
269 | */ |
||
270 | public function setEnvironment(string $env): void |
||
271 | { |
||
272 | $this->env = $env; |
||
273 | } |
||
274 | |||
275 | public function getEnvironment() |
||
276 | { |
||
277 | return $this->env; |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Set whether this is a user property (ro). |
||
282 | * This is deprecated in Ant 1.5, but the userProperty attribute |
||
283 | * of the class is still being set via constructor, so Phing will |
||
284 | * allow this method to function. |
||
285 | */ |
||
286 | public function setUserProperty(bool $v): void |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * @return bool |
||
293 | */ |
||
294 | public function getUserProperty() |
||
295 | { |
||
296 | return $this->userProperty; |
||
297 | } |
||
298 | |||
299 | public function setOverride(bool $override): void |
||
300 | { |
||
301 | $this->override = $override; |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * @return bool |
||
306 | */ |
||
307 | public function getOverride() |
||
308 | { |
||
309 | return $this->override; |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * @param Project $p |
||
314 | */ |
||
315 | public function setFallback($p): void |
||
316 | { |
||
317 | $this->fallback = $p; |
||
318 | } |
||
319 | |||
320 | public function getFallback() |
||
321 | { |
||
322 | return $this->fallback; |
||
323 | } |
||
324 | |||
325 | public function setLogoutput(bool $logOutput): void |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * @return bool |
||
332 | */ |
||
333 | public function getLogoutput() |
||
334 | { |
||
335 | return $this->logOutput; |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Set quiet mode, which suppresses warnings if chmod() fails. |
||
340 | * |
||
341 | * @see setFailonerror() |
||
342 | */ |
||
343 | public function setQuiet(bool $bool): void |
||
344 | { |
||
345 | $this->quiet = $bool; |
||
346 | } |
||
347 | |||
348 | public function getQuiet(): bool |
||
349 | { |
||
350 | return $this->quiet; |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * set the property in the project to the value. |
||
355 | * if the task was give a file or env attribute |
||
356 | * here is where it is loaded. |
||
357 | */ |
||
358 | public function main() |
||
359 | { |
||
360 | $this->validate(); |
||
361 | |||
362 | if (null !== $this->name && null !== $this->value) { |
||
363 | $this->addProperty($this->name, $this->value); |
||
364 | } |
||
365 | |||
366 | if (null !== $this->file) { |
||
367 | $this->loadFile($this->file); |
||
368 | } |
||
369 | |||
370 | if (null !== $this->env) { |
||
371 | $this->loadEnvironment($this->env); |
||
372 | } |
||
373 | |||
374 | if (null !== $this->name && null !== $this->reference) { |
||
375 | // get the refereced property |
||
376 | try { |
||
377 | $referencedObject = $this->reference->getReferencedObject($this->project); |
||
378 | |||
379 | if ($referencedObject instanceof Exception) { |
||
380 | $reference = $referencedObject->getMessage(); |
||
381 | } else { |
||
382 | $reference = (string) $referencedObject; |
||
383 | } |
||
384 | |||
385 | $this->addProperty($this->name, $reference); |
||
386 | } catch (BuildException $be) { |
||
387 | if (null !== $this->fallback) { |
||
388 | $referencedObject = $this->reference->getReferencedObject($this->fallback); |
||
389 | |||
390 | if ($referencedObject instanceof Exception) { |
||
391 | $reference = $referencedObject->getMessage(); |
||
392 | } else { |
||
393 | $reference = (string) $referencedObject; |
||
394 | } |
||
395 | $this->addProperty($this->name, $reference); |
||
396 | } else { |
||
397 | throw $be; |
||
398 | } |
||
399 | } |
||
400 | } |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * load the environment values. |
||
405 | * |
||
406 | * @param string $prefix prefix to place before them |
||
407 | */ |
||
408 | protected function loadEnvironment(string $prefix) |
||
409 | { |
||
410 | $props = new Properties(); |
||
411 | if ('.' === substr($prefix, strlen($prefix) - 1)) { |
||
412 | $prefix .= '.'; |
||
413 | } |
||
414 | $this->log("Loading Environment {$prefix}", Project::MSG_VERBOSE); |
||
415 | foreach (getenv() as $key => $value) { |
||
416 | $props->setProperty($prefix . '.' . $key, $value); |
||
417 | } |
||
418 | $this->addProperties($props); |
||
419 | } |
||
420 | |||
421 | /** |
||
422 | * iterate through a set of properties, |
||
423 | * resolve them then assign them. |
||
424 | * |
||
425 | * @param Properties $props |
||
426 | * |
||
427 | * @throws BuildException |
||
428 | */ |
||
429 | protected function addProperties($props) |
||
439 | } |
||
440 | } |
||
441 | |||
442 | /** |
||
443 | * add a name value pair to the project property set. |
||
444 | * |
||
445 | * @param string $name name of property |
||
446 | * @param string $value value to set |
||
447 | */ |
||
448 | protected function addProperty($name, $value) |
||
449 | { |
||
450 | if (null === $this->file && count($this->filterChains) > 0) { |
||
467 | } |
||
468 | } |
||
469 | } |
||
470 | |||
471 | /** |
||
472 | * load properties from a file. |
||
473 | * |
||
474 | * @throws BuildException |
||
475 | */ |
||
476 | protected function loadFile(File $file) |
||
510 | } |
||
511 | } |
||
512 | |||
513 | /** |
||
514 | * Given a Properties object, this method goes through and resolves |
||
515 | * any references to properties within the object. |
||
516 | * |
||
517 | * @param Properties $props the collection of Properties that need to be resolved |
||
518 | * |
||
519 | * @throws BuildException |
||
520 | */ |
||
521 | protected function resolveAllProperties(Properties $props) |
||
593 | } // while (!$resolved) |
||
594 | } // while (count($keys) |
||
595 | } |
||
596 | |||
597 | /** |
||
598 | * @throws BuildException |
||
599 | */ |
||
600 | private function validate(): void |
||
621 |