Total Complexity | 48 |
Total Lines | 215 |
Duplicated Lines | 0 % |
Changes | 8 | ||
Bugs | 2 | Features | 2 |
Complex classes like PatchListUtils 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 PatchListUtils, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class PatchListUtils |
||
11 | { |
||
12 | public function compareLists(array $listA, array $listB, \Closure $logicProvider) |
||
27 | } |
||
28 | |||
29 | public function sanitizeFileSystem(array $patches) |
||
30 | { |
||
31 | foreach ($patches as $patchGroup) { |
||
32 | foreach ($patchGroup as $patchInfo) { |
||
33 | if (!isset($patchInfo[Patch::TMP]) || !$patchInfo[Patch::TMP]) { |
||
34 | continue; |
||
35 | } |
||
36 | |||
37 | if (file_exists($patchInfo[Patch::PATH])) { |
||
38 | unlink($patchInfo[Patch::PATH]); |
||
39 | } |
||
40 | |||
41 | $dirName = dirname($patchInfo[Patch::PATH]); |
||
42 | |||
43 | if (!is_dir($dirName)) { |
||
44 | continue; |
||
45 | } |
||
46 | |||
47 | $iterator = new \FilesystemIterator($dirName); |
||
48 | |||
49 | if (!$iterator->valid()) { |
||
50 | rmdir($dirName); |
||
51 | } |
||
52 | } |
||
53 | } |
||
54 | } |
||
55 | |||
56 | public function applyDefinitionFilter(array $patches, \Closure $logicProvider) |
||
57 | { |
||
58 | foreach ($patches as &$packagePatches) { |
||
59 | foreach ($packagePatches as &$patchData) { |
||
60 | $result = $logicProvider($patchData); |
||
61 | |||
62 | if ($result) { |
||
63 | continue; |
||
64 | } |
||
65 | |||
66 | $patchData = false; |
||
67 | } |
||
68 | |||
69 | unset($patchData); |
||
70 | |||
71 | $packagePatches = array_filter($packagePatches); |
||
72 | } |
||
73 | |||
74 | return array_filter($patches); |
||
75 | } |
||
76 | |||
77 | public function applyDefinitionKeyValueFilter(array $patches, $filter, $key) |
||
97 | ); |
||
98 | } |
||
99 | |||
100 | private function shouldIncludePatch($value, $filter) |
||
101 | { |
||
102 | if (is_array($value) && preg_grep($filter, $value)) { |
||
103 | return true; |
||
104 | } |
||
105 | |||
106 | if (is_string($value) && preg_match($filter, $value)) { |
||
107 | return true; |
||
108 | } |
||
109 | |||
110 | if (is_bool($filter) && $value === $filter) { |
||
111 | return true; |
||
112 | } |
||
113 | |||
114 | return false; |
||
115 | } |
||
116 | |||
117 | public function filterListByTargets(array $patches, array $targets) |
||
130 | } |
||
131 | |||
132 | public function mergeLists(array $listA, array $listB) |
||
133 | { |
||
134 | $result = array(); |
||
135 | |||
136 | $keys = array_unique( |
||
137 | array_merge(array_keys($listA), array_keys($listB)) |
||
138 | ); |
||
139 | |||
140 | foreach ($keys as $key) { |
||
141 | $result[$key] = array_replace( |
||
142 | isset($listA[$key]) ? $listA[$key] : array(), |
||
143 | isset($listB[$key]) ? $listB[$key] : array() |
||
144 | ); |
||
145 | } |
||
146 | |||
147 | return $result; |
||
148 | } |
||
149 | |||
150 | public function diffListsByPath(array $listA, array $listB) |
||
151 | { |
||
152 | $pathFlags = array_fill_keys($this->getAllPaths($listB), true); |
||
153 | |||
154 | return array_map(function (array $group) use ($pathFlags) { |
||
155 | return array_filter( |
||
156 | $group, |
||
157 | function (array $item) use ($pathFlags) { |
||
158 | $path = $item[Patch::PATH] ? $item[Patch::PATH] : $item[Patch::URL]; |
||
159 | |||
160 | return !isset($pathFlags[$path]); |
||
161 | } |
||
162 | ); |
||
163 | }, $listA); |
||
164 | } |
||
165 | |||
166 | public function intersectListsByPath(array $listA, array $listB) |
||
167 | { |
||
168 | $pathFlags = array_fill_keys($this->getAllPaths($listB), true); |
||
169 | |||
170 | return array_map(function (array $group) use ($pathFlags) { |
||
171 | return array_filter( |
||
172 | $group, |
||
173 | function (array $item) use ($pathFlags) { |
||
174 | $path = $item[Patch::PATH] ? $item[Patch::PATH] : $item[Patch::URL]; |
||
175 | |||
176 | return isset($pathFlags[$path]); |
||
177 | } |
||
178 | ); |
||
179 | }, $listA); |
||
180 | } |
||
181 | |||
182 | public function diffListsByName(array $listA, array $listB) |
||
193 | } |
||
194 | |||
195 | public function intersectListsByName(array $listA, array $listB) |
||
208 | } |
||
209 | |||
210 | private function getAllPaths($patches) |
||
225 | ); |
||
226 | } |
||
227 | } |
||
228 |