| 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 |
||
| 45 | public function doExperiment(string $experimentName, ?UserInterface $user = null, array $options = []): string |
||
| 46 | { |
||
| 47 | try { |
||
| 48 | $session = $this->requestStack->getSession(); |
||
| 49 | } catch (SessionNotFoundException $e) { |
||
| 50 | return 'control'; |
||
| 51 | } |
||
| 52 | |||
| 53 | if (!$this->enabledDecider->isTestable()) { |
||
| 54 | return 'control'; |
||
| 55 | } |
||
| 56 | |||
| 57 | $decision = $this->choiceDecider->getChoice($experimentName); |
||
| 58 | |||
| 59 | if (!is_null($decision)) { |
||
| 60 | return $decision; |
||
| 61 | } |
||
| 62 | |||
| 63 | $decision = $session->get('ab_testing_'.$experimentName); |
||
| 64 | |||
| 65 | if (null === $decision) { |
||
| 66 | $randomNumber = mt_rand(0, 100); |
||
| 67 | $lowerBound = 0; |
||
| 68 | try { |
||
| 69 | $experiment = $this->experimentRepository->findByName($experimentName); |
||
| 70 | $decision = 'control'; |
||
| 71 | foreach ($experiment->getVariants() as $variant) { |
||
| 72 | $upperBound = $lowerBound + $variant->getPercentage(); |
||
| 73 | if (100 === $upperBound) { |
||
| 74 | $decision = $variant->getName(); |
||
| 75 | break; |
||
| 76 | } |
||
| 77 | if ($randomNumber > $lowerBound && $randomNumber <= $upperBound) { |
||
| 78 | $decision = $variant->getName(); |
||
| 79 | break; |
||
| 80 | } |
||
| 81 | $lowerBound = $upperBound; |
||
| 82 | } |
||
| 83 | } catch (NoEntityFoundException $e) { |
||
| 84 | $decision = 'control'; |
||
| 85 | } |
||
| 86 | |||
| 87 | $idAsString = $session->get(SessionCreator::SESSION_ID); |
||
| 88 | $uuid = Uuid::fromString($idAsString); |
||
| 89 | |||
| 90 | $this->experimentLogRepository->saveDecision($uuid, $experimentName, $decision); |
||
| 91 | $session->set('ab_testing_'.$experimentName, $decision); |
||
| 92 | } |
||
| 93 | |||
| 94 | return $decision; |
||
| 95 | } |
||
| 97 |