| Conditions | 12 |
| Paths | 13 |
| Total Lines | 49 |
| Code Lines | 24 |
| 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 |
||
| 51 | public function readDonor(InputInterface $input): Donor |
||
| 52 | { |
||
| 53 | $taintedId = $input->getArgument('donor'); |
||
| 54 | |||
| 55 | if (!is_string($taintedId)) { |
||
| 56 | throw new \LogicException('Donor key must be string'); |
||
| 57 | } |
||
| 58 | |||
| 59 | $donorId = (new StringValidator())->validate('donor', $taintedId); |
||
| 60 | |||
| 61 | if ($input->getOption('id-payer-number') && $input->getOption('id-mandate-key')) { |
||
| 62 | throw new RuntimeException("Illegal to use the 'id-payer-number' and 'id-mandate-key' flags toghether."); |
||
| 63 | } |
||
| 64 | |||
| 65 | if ($input->getOption('id-payer-number')) { |
||
| 66 | return $this->donorQuery->requireByPayerNumber($donorId); |
||
| 67 | } |
||
| 68 | |||
| 69 | if ($input->getOption('id-mandate-key')) { |
||
| 70 | return $this->donorQuery->requireByMandateKey($donorId); |
||
| 71 | } |
||
| 72 | |||
| 73 | if ($donor = $this->donorQuery->findByPayerNumber($donorId)) { |
||
| 74 | return $donor; |
||
| 75 | } |
||
| 76 | |||
| 77 | if ($donor = $this->donorQuery->findByMandateKey($donorId)) { |
||
| 78 | return $donor; |
||
| 79 | } |
||
| 80 | |||
| 81 | $regexp = '/' . preg_quote($donorId, '/') . '/i'; |
||
| 82 | |||
| 83 | /** @var ?Donor */ |
||
| 84 | $matchedDonor = null; |
||
| 85 | |||
| 86 | foreach ($this->donorQuery->findAll() as $donor) { |
||
| 87 | if (preg_match($regexp, $donor->getName())) { |
||
| 88 | if ($matchedDonor) { |
||
| 89 | throw new DonorDoesNotExistException("Unable to find donor '$donorId', more than one match."); |
||
| 90 | } |
||
| 91 | $matchedDonor = $donor; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | if ($matchedDonor) { |
||
|
|
|||
| 96 | return $matchedDonor; |
||
| 97 | } |
||
| 98 | |||
| 99 | throw new DonorDoesNotExistException("Unable to find donor '$donorId'"); |
||
| 100 | } |
||
| 102 |