Conditions | 10 |
Paths | 12 |
Total Lines | 50 |
Code Lines | 32 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
52 | public function doExperiment(string $experimentName, ?UserInterface $user = null, array $options = []): string |
||
53 | { |
||
54 | try { |
||
55 | $session = $this->requestStack->getSession(); |
||
56 | } catch (SessionNotFoundException $e) { |
||
57 | return 'control'; |
||
58 | } |
||
59 | |||
60 | if (!$this->enabledDecider->isTestable()) { |
||
61 | return 'control'; |
||
62 | } |
||
63 | |||
64 | $decision = $this->choiceDecider->getChoice($experimentName); |
||
65 | |||
66 | if (!is_null($decision)) { |
||
67 | return $decision; |
||
68 | } |
||
69 | |||
70 | $decision = $session->get('ab_testing_'.$experimentName); |
||
71 | |||
72 | if (null === $decision) { |
||
73 | $randomNumber = mt_rand(0, 100); |
||
74 | $lowerBound = 0; |
||
75 | try { |
||
76 | $experiment = $this->experimentRepository->findByName($experimentName); |
||
77 | $decision = 'control'; |
||
78 | foreach ($experiment->getVariants() as $variant) { |
||
79 | $upperBound = $lowerBound + $variant->getPercentage(); |
||
80 | if (100 === $upperBound) { |
||
81 | $decision = $variant->getName(); |
||
82 | break; |
||
83 | } |
||
84 | if ($randomNumber > $lowerBound && $randomNumber <= $upperBound) { |
||
85 | $decision = $variant->getName(); |
||
86 | break; |
||
87 | } |
||
88 | $lowerBound = $upperBound; |
||
89 | } |
||
90 | } catch (NoEntityFoundException $e) { |
||
91 | $decision = 'control'; |
||
92 | } |
||
93 | |||
94 | $idAsString = $session->get(SessionCreator::SESSION_ID); |
||
95 | $uuid = Uuid::fromString($idAsString); |
||
96 | |||
97 | $this->experimentLogRepository->saveDecision($uuid, $experimentName, $decision); |
||
98 | $session->set('ab_testing_'.$experimentName, $decision); |
||
99 | } |
||
100 | |||
101 | return $decision; |
||
102 | } |
||
104 |