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