Total Complexity | 41 |
Total Lines | 182 |
Duplicated Lines | 0 % |
Changes | 0 |
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 sanitizeFileSystem(array $patches) |
||
21 | } |
||
22 | } |
||
23 | } |
||
24 | |||
25 | public function applyDefinitionFilter(array $patches, $logicProvider) |
||
26 | { |
||
27 | foreach ($patches as &$packagePatches) { |
||
28 | foreach ($packagePatches as &$patchData) { |
||
29 | $result = $logicProvider($patchData); |
||
30 | |||
31 | if ($result) { |
||
32 | continue; |
||
33 | } |
||
34 | |||
35 | $patchData = false; |
||
36 | } |
||
37 | |||
38 | $packagePatches = array_filter($packagePatches); |
||
39 | } |
||
40 | |||
41 | return array_filter($patches); |
||
42 | } |
||
43 | |||
44 | public function applyDefinitionKeyValueFilter(array $patches, $filter, $key) |
||
64 | ); |
||
65 | } |
||
66 | |||
67 | private function shouldIncludePatch($value, $filter) |
||
68 | { |
||
69 | if (is_array($value) && preg_grep($filter, $value)) { |
||
70 | return true; |
||
71 | } |
||
72 | |||
73 | if (is_string($value) && preg_match($filter, $value)) { |
||
74 | return true; |
||
75 | } |
||
76 | |||
77 | if (is_bool($filter) && $value === $filter) { |
||
78 | return true; |
||
79 | } |
||
80 | |||
81 | return false; |
||
82 | } |
||
83 | |||
84 | public function filterListByTargets(array $patches, array $targets) |
||
97 | } |
||
98 | |||
99 | public function mergeLists(array $listA, array $listB) |
||
100 | { |
||
101 | $result = array(); |
||
102 | |||
103 | $keys = array_unique( |
||
104 | array_merge(array_keys($listA), array_keys($listB)) |
||
105 | ); |
||
106 | |||
107 | foreach ($keys as $key) { |
||
108 | $result[$key] = array_replace( |
||
109 | isset($listA[$key]) ? $listA[$key] : array(), |
||
110 | isset($listB[$key]) ? $listB[$key] : array() |
||
111 | ); |
||
112 | } |
||
113 | |||
114 | return $result; |
||
115 | } |
||
116 | |||
117 | public function diffListsByPath(array $listA, array $listB) |
||
118 | { |
||
119 | $pathFlags = array_fill_keys($this->getAllPaths($listB), true); |
||
120 | |||
121 | return array_map(function (array $group) use ($pathFlags) { |
||
122 | return array_filter( |
||
123 | $group, |
||
124 | function (array $item) use ($pathFlags) { |
||
125 | $path = $item[Patch::PATH] ? $item[Patch::PATH] : $item[Patch::URL]; |
||
126 | |||
127 | return !isset($pathFlags[$path]); |
||
128 | } |
||
129 | ); |
||
130 | }, $listA); |
||
131 | } |
||
132 | |||
133 | public function intersectListsByPath(array $listA, array $listB) |
||
134 | { |
||
135 | $pathFlags = array_fill_keys($this->getAllPaths($listB), true); |
||
136 | |||
137 | return array_map(function (array $group) use ($pathFlags) { |
||
138 | return array_filter( |
||
139 | $group, |
||
140 | function (array $item) use ($pathFlags) { |
||
141 | $path = $item[Patch::PATH] ? $item[Patch::PATH] : $item[Patch::URL]; |
||
142 | |||
143 | return isset($pathFlags[$path]); |
||
144 | } |
||
145 | ); |
||
146 | }, $listA); |
||
147 | } |
||
148 | |||
149 | public function diffListsByName(array $listA, array $listB) |
||
160 | } |
||
161 | |||
162 | public function intersectListsByName(array $listA, array $listB) |
||
175 | } |
||
176 | |||
177 | private function getAllPaths($patches) |
||
192 | ); |
||
193 | } |
||
195 |