Complex classes like SplitTestAnalyzer 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 SplitTestAnalyzer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | final class SplitTestAnalyzer implements \IteratorAggregate |
||
6 | { |
||
7 | |||
8 | const DEFAULT_SAMPLES = 5000; |
||
9 | |||
10 | /** |
||
11 | * @var int |
||
12 | */ |
||
13 | private $numSamples; |
||
14 | |||
15 | /** |
||
16 | * @var Variation[] |
||
17 | */ |
||
18 | private $variations = []; |
||
19 | |||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | private $result; |
||
24 | |||
25 | /** |
||
26 | * BayesianPerformance constructor. |
||
27 | * @param int $numSamples |
||
28 | */ |
||
29 | private function __construct(int $numSamples = self::DEFAULT_SAMPLES) |
||
33 | |||
34 | /** |
||
35 | * @param Variation[] ...$variations |
||
36 | * @throws \InvalidArgumentException |
||
37 | */ |
||
38 | private function setVariations(Variation ...$variations) |
||
48 | |||
49 | /** |
||
50 | * @param int $numSamples |
||
51 | * @return SplitTestAnalyzer |
||
52 | */ |
||
53 | public static function create(int $numSamples = self::DEFAULT_SAMPLES): self |
||
57 | |||
58 | /** |
||
59 | * @param Variation[] ...$variations |
||
60 | * @return SplitTestAnalyzer |
||
61 | * @throws \InvalidArgumentException |
||
62 | */ |
||
63 | public function withVariations(Variation ...$variations): self |
||
73 | |||
74 | /** |
||
75 | * @param Variation $variation |
||
76 | * @return float |
||
77 | */ |
||
78 | public function getVariationProbability(Variation $variation): float |
||
84 | |||
85 | /** |
||
86 | * @param int $sort |
||
87 | * @return Variation[] |
||
88 | */ |
||
89 | public function getOrderedVariations(int $sort = SORT_DESC): array |
||
103 | |||
104 | /** |
||
105 | * @return Variation |
||
106 | */ |
||
107 | public function getBestVariation(): Variation |
||
112 | |||
113 | /** |
||
114 | * @return array |
||
115 | */ |
||
116 | public function getResult() |
||
121 | |||
122 | public function getIterator() |
||
127 | |||
128 | /** |
||
129 | * @return mixed |
||
130 | * @throws \Exception |
||
131 | * @throws \InvalidArgumentException |
||
132 | */ |
||
133 | private function computeProbabilities() |
||
164 | |||
165 | /** |
||
166 | * @param Variation[] ...$variations |
||
167 | * @throws \InvalidArgumentException |
||
168 | */ |
||
169 | private function check(Variation ...$variations) |
||
177 | |||
178 | /** |
||
179 | * @return float |
||
180 | * @throws \Exception |
||
181 | */ |
||
182 | private function getMathRandom(): float |
||
186 | |||
187 | /** |
||
188 | * @return float |
||
189 | * @throws \Exception |
||
190 | */ |
||
191 | private function getRandn(): float |
||
203 | |||
204 | /** |
||
205 | * @param float $shape |
||
206 | * @return float |
||
207 | * @throws \Exception |
||
208 | */ |
||
209 | private function getRandg(float $shape): float |
||
247 | |||
248 | /** |
||
249 | * @param float $alpha |
||
250 | * @param float $beta |
||
251 | * @return float |
||
252 | * @throws \Exception |
||
253 | */ |
||
254 | private function getSample(float $alpha, float $beta): float |
||
259 | |||
260 | private function reset() |
||
265 | } |
||
266 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.