Total Complexity | 48 |
Total Lines | 284 |
Duplicated Lines | 0 % |
Changes | 11 | ||
Bugs | 1 | Features | 0 |
Complex classes like SearchAndReplace 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 SearchAndReplace, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class SearchAndReplace extends Task |
||
15 | { |
||
16 | protected $taskStep = 's30'; |
||
17 | |||
18 | /** |
||
19 | * for debugging purposes |
||
20 | * @var bool |
||
21 | */ |
||
22 | protected $debug = false; |
||
23 | |||
24 | /** |
||
25 | * check if there are double-ups in replacement. |
||
26 | * @var bool |
||
27 | */ |
||
28 | protected $checkReplacementIssues = false; |
||
29 | |||
30 | /** |
||
31 | * string used to show issues in replacement in the actual code being replaced. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $replacementHeader = 'automated upgrade'; |
||
36 | |||
37 | /** |
||
38 | * folder containing the replacement file |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $folderContainingReplacementData = ''; |
||
43 | |||
44 | /** |
||
45 | * list of folders to ignore in search and replace |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $ignoreFolderArray = [ |
||
49 | '.git', |
||
50 | '.svn', |
||
51 | ]; |
||
52 | |||
53 | protected $runInRootDir = false; |
||
54 | |||
55 | /** |
||
56 | * the names of the folder that contains the data we need |
||
57 | * e.g. SS4 / SS37 |
||
58 | * IMPORTANT! |
||
59 | * |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $sourceFolders = [ |
||
63 | 'SS4', |
||
64 | ]; |
||
65 | |||
66 | protected $commitAndPush = true; |
||
67 | |||
68 | public function getTitle() |
||
69 | { |
||
70 | return 'Search and Replace'; |
||
71 | } |
||
72 | |||
73 | public function getDescription() |
||
76 | Replaces a bunch of code snippets in preparation of the upgrade. |
||
77 | Controversial replacements will be replaced with a comment |
||
78 | next to it so you can review replacements easily.'; |
||
79 | } |
||
80 | |||
81 | public function setCheckReplacementIssues(bool $b) |
||
82 | { |
||
83 | $this->checkReplacementIssues = $b; |
||
84 | |||
85 | return $this; |
||
86 | } |
||
87 | |||
88 | public function setIgnoreFolderArray(array $a) |
||
89 | { |
||
90 | $this->ignoreFolderArray = $a; |
||
91 | |||
92 | return $this; |
||
93 | } |
||
94 | |||
95 | public function setRunInRootDir(bool $b) |
||
96 | { |
||
97 | $this->runInRootDir = $b; |
||
98 | |||
99 | return $this; |
||
100 | } |
||
101 | |||
102 | public function setCommitAndPush(bool $b) |
||
103 | { |
||
104 | $this->commitAndPush = $b; |
||
105 | |||
106 | return $this; |
||
107 | } |
||
108 | |||
109 | public function setFolderContainingReplacementData(string $s) |
||
110 | { |
||
111 | $this->folderContainingReplacementData = $s; |
||
112 | |||
113 | return $this; |
||
114 | } |
||
115 | |||
116 | |||
117 | public function setSourceFolders(array $a) |
||
118 | { |
||
119 | $this->sourceFolders = $a; |
||
120 | |||
121 | return $this; |
||
122 | } |
||
123 | |||
124 | public function runActualTask($params = []): ?string |
||
215 | } |
||
216 | |||
217 | protected function hasCommitAndPush() |
||
218 | { |
||
219 | return $this->commitAndPush; |
||
220 | } |
||
221 | |||
222 | protected function getReplacementDataObjects(): array |
||
223 | { |
||
224 | $array = []; |
||
225 | foreach ($this->sourceFolders as $sourceFolder) { |
||
226 | $array[] = new LoadReplacementData( |
||
227 | $this->mu(), |
||
228 | $this->folderContainingReplacementData, |
||
229 | $sourceFolder |
||
230 | ); |
||
231 | } |
||
232 | |||
233 | return $array; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * 1. check that one find is not used twice: |
||
238 | * find can be found 2x |
||
239 | * @param mixed $replacementDataObject |
||
240 | */ |
||
241 | private function checkReplacementDataIssues($replacementDataObject) |
||
298 | } |
||
299 | } |
||
300 |