Total Complexity | 45 |
Total Lines | 201 |
Duplicated Lines | 0 % |
Changes | 28 | ||
Bugs | 6 | Features | 6 |
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) |
||
13 | { |
||
14 | $matches = array(); |
||
15 | |||
16 | foreach ($listB as $name => $itemsB) { |
||
17 | $itemsA = isset($listA[$name]) ? $listA[$name] : array(); |
||
18 | |||
19 | if (!$logicProvider($itemsA, $itemsB)) { |
||
20 | continue; |
||
21 | } |
||
22 | |||
23 | $matches[] = $name; |
||
24 | } |
||
25 | |||
26 | return $matches; |
||
27 | } |
||
28 | |||
29 | public function sanitizeFileSystem(array $patches) |
||
38 | } |
||
39 | } |
||
40 | } |
||
41 | |||
42 | public function applyDefinitionFilter(array $patches, \Closure $logicProvider) |
||
43 | { |
||
44 | foreach ($patches as &$packagePatches) { |
||
45 | foreach ($packagePatches as &$patchData) { |
||
46 | $result = $logicProvider($patchData); |
||
47 | |||
48 | if ($result) { |
||
49 | continue; |
||
50 | } |
||
51 | |||
52 | $patchData = false; |
||
53 | } |
||
54 | |||
55 | unset($patchData); |
||
56 | |||
57 | $packagePatches = array_filter($packagePatches); |
||
58 | } |
||
59 | |||
60 | return array_filter($patches); |
||
61 | } |
||
62 | |||
63 | public function applyDefinitionKeyValueFilter(array $patches, $filter, $key) |
||
83 | ); |
||
84 | } |
||
85 | |||
86 | private function shouldIncludePatch($value, $filter) |
||
87 | { |
||
88 | if (is_array($value) && preg_grep($filter, $value)) { |
||
89 | return true; |
||
90 | } |
||
91 | |||
92 | if (is_string($value) && preg_match($filter, $value)) { |
||
93 | return true; |
||
94 | } |
||
95 | |||
96 | if (is_bool($filter) && $value === $filter) { |
||
97 | return true; |
||
98 | } |
||
99 | |||
100 | return false; |
||
101 | } |
||
102 | |||
103 | public function filterListByTargets(array $patches, array $targets) |
||
116 | } |
||
117 | |||
118 | public function mergeLists(array $listA, array $listB) |
||
119 | { |
||
120 | $result = array(); |
||
121 | |||
122 | $keys = array_unique( |
||
123 | array_merge(array_keys($listA), array_keys($listB)) |
||
124 | ); |
||
125 | |||
126 | foreach ($keys as $key) { |
||
127 | $result[$key] = array_replace( |
||
128 | isset($listA[$key]) ? $listA[$key] : array(), |
||
129 | isset($listB[$key]) ? $listB[$key] : array() |
||
130 | ); |
||
131 | } |
||
132 | |||
133 | return $result; |
||
134 | } |
||
135 | |||
136 | public function diffListsByPath(array $listA, array $listB) |
||
137 | { |
||
138 | $pathFlags = array_fill_keys($this->getAllPaths($listB), true); |
||
139 | |||
140 | return array_map(function (array $group) use ($pathFlags) { |
||
141 | return array_filter( |
||
142 | $group, |
||
143 | function (array $item) use ($pathFlags) { |
||
144 | $path = $item[Patch::PATH] ? $item[Patch::PATH] : $item[Patch::URL]; |
||
145 | |||
146 | return !isset($pathFlags[$path]); |
||
147 | } |
||
148 | ); |
||
149 | }, $listA); |
||
150 | } |
||
151 | |||
152 | public function intersectListsByPath(array $listA, array $listB) |
||
153 | { |
||
154 | $pathFlags = array_fill_keys($this->getAllPaths($listB), true); |
||
155 | |||
156 | return array_map(function (array $group) use ($pathFlags) { |
||
157 | return array_filter( |
||
158 | $group, |
||
159 | function (array $item) use ($pathFlags) { |
||
160 | $path = $item[Patch::PATH] ? $item[Patch::PATH] : $item[Patch::URL]; |
||
161 | |||
162 | return isset($pathFlags[$path]); |
||
163 | } |
||
164 | ); |
||
165 | }, $listA); |
||
166 | } |
||
167 | |||
168 | public function diffListsByName(array $listA, array $listB) |
||
179 | } |
||
180 | |||
181 | public function intersectListsByName(array $listA, array $listB) |
||
194 | } |
||
195 | |||
196 | private function getAllPaths($patches) |
||
211 | ); |
||
212 | } |
||
213 | } |
||
214 |