Total Complexity | 46 |
Total Lines | 269 |
Duplicated Lines | 0 % |
Changes | 8 | ||
Bugs | 0 | 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 |
||
15 | class SearchAndReplace extends Task |
||
16 | { |
||
17 | protected $taskStep = 's30'; |
||
18 | |||
19 | /** |
||
20 | * for debugging purposes |
||
21 | * @var bool |
||
22 | */ |
||
23 | protected $debug = false; |
||
24 | |||
25 | /** |
||
26 | * check if there are double-ups in replacement. |
||
27 | * @var bool |
||
28 | */ |
||
29 | protected $checkReplacementIssues = false; |
||
30 | |||
31 | /** |
||
32 | * string used to show issues in replacement in the actual code being replaced. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $replacementHeader = 'automated upgrade'; |
||
37 | |||
38 | /** |
||
39 | * folder containing the replacement file |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $folderContainingReplacementData = ''; |
||
44 | |||
45 | /** |
||
46 | * list of folders to ignore in search and replace |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $ignoreFolderArray = [ |
||
50 | '.git', |
||
51 | '.svn', |
||
52 | ]; |
||
53 | |||
54 | /** |
||
55 | * the names of the folder that contains the data we need |
||
56 | * e.g. SS4 / SS37 |
||
57 | * IMPORTANT! |
||
58 | * |
||
59 | * @var array |
||
60 | */ |
||
61 | protected $sourceFolders = [ |
||
62 | 'SS4', |
||
63 | ]; |
||
64 | |||
65 | protected $commitAndPush = true; |
||
66 | |||
67 | public function getTitle() |
||
68 | { |
||
69 | return 'Search and Replace'; |
||
70 | } |
||
71 | |||
72 | public function getDescription() |
||
75 | Replaces a bunch of code snippets in preparation of the upgrade. |
||
76 | Controversial replacements will be replaced with a comment |
||
77 | next to it so you can review replacements easily.'; |
||
78 | } |
||
79 | |||
80 | public function setCheckReplacementIssues(bool $b) |
||
81 | { |
||
82 | $this->checkReplacementIssues = $b; |
||
83 | |||
84 | return $this; |
||
85 | } |
||
86 | |||
87 | public function setIgnoreFolderArray(array $a) |
||
88 | { |
||
89 | $this->ignoreFolderArray = $a; |
||
90 | |||
91 | return $this; |
||
92 | } |
||
93 | |||
94 | public function setCommitAndPush(bool $b) |
||
95 | { |
||
96 | $this->commitAndPush = $b; |
||
97 | |||
98 | return $this; |
||
99 | } |
||
100 | |||
101 | public function setFolderContainingReplacementData(string $s) |
||
102 | { |
||
103 | $this->folderContainingReplacementData = $s; |
||
104 | |||
105 | return $this; |
||
106 | } |
||
107 | |||
108 | public function setSourceFolders(array $a) |
||
109 | { |
||
110 | $this->sourceFolders = $a; |
||
111 | |||
112 | return $this; |
||
113 | } |
||
114 | |||
115 | public function runActualTask($params = []) |
||
196 | } |
||
197 | } |
||
198 | } |
||
199 | } |
||
200 | } |
||
201 | } |
||
202 | |||
203 | protected function hasCommitAndPush() |
||
206 | } |
||
207 | |||
208 | protected function getReplacementDataObjects(): array |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * 1. check that one find is not used twice: |
||
224 | * find can be found 2x |
||
225 | * @param mixed $replacementDataObject |
||
226 | */ |
||
227 | private function checkReplacementDataIssues($replacementDataObject) |
||
286 |