| Conditions | 11 |
| Paths | 10 |
| Total Lines | 46 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 54 | public function __invoke(array $data, array $options = []) |
||
| 55 | { |
||
| 56 | // check data |
||
| 57 | if (!isset($data['uri']) || !isset($data['oneClickProfiles']) || !isset($data['applyId'])) |
||
| 58 | { |
||
| 59 | throw new \InvalidArgumentException('Invalid data passed'); |
||
| 60 | } |
||
| 61 | |||
| 62 | $variables = [ |
||
| 63 | 'default' => null, |
||
| 64 | 'oneClick' => [], |
||
| 65 | ]; |
||
| 66 | $options = array_merge([ |
||
| 67 | 'partial' => $this->partial, |
||
| 68 | 'oneClickOnly' => false, |
||
| 69 | 'defaultLabel' => null, |
||
| 70 | 'oneClickLabel' => null, |
||
| 71 | 'sendImmediately' => false |
||
| 72 | ], $options); |
||
| 73 | $view = $this->view; |
||
| 74 | $currentTemplate = $view->viewModel() |
||
|
|
|||
| 75 | ->getCurrent() |
||
| 76 | ->getTemplate(); |
||
| 77 | $partial = dirname($currentTemplate) . '/' . $options['partial']; |
||
| 78 | |||
| 79 | if (!$options['oneClickOnly'] && $data['uri']) { |
||
| 80 | $variables['default'] = [ |
||
| 81 | 'label' => $options['defaultLabel'] ?: $view->translate('Apply now'), |
||
| 82 | 'url' => $data['uri'] |
||
| 83 | ]; |
||
| 84 | } |
||
| 85 | |||
| 86 | if ($data['oneClickProfiles']) { |
||
| 87 | $label = $options['oneClickLabel'] ?: $view->translate('Apply with %s'); |
||
| 88 | |||
| 89 | foreach ($data['oneClickProfiles'] as $network) { |
||
| 90 | $variables['oneClick'][] = [ |
||
| 91 | 'label' => sprintf($label, $network), |
||
| 92 | 'url' => $view->url('lang/apply-one-click', ['applyId' => $data['applyId'], 'network' => $network, 'immediately' => $options['sendImmediately'] ?: null], ['force_canonical' => true]), |
||
| 93 | 'network' => $network |
||
| 94 | ]; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | return $view->partial($partial, $variables); |
||
| 99 | } |
||
| 100 | |||
| 120 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: