Conditions | 9 |
Paths | 18 |
Total Lines | 52 |
Code Lines | 36 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
27 | public function createItem($name, array $options = array()) |
||
28 | { |
||
29 | $item = new MenuItem($name, $this); |
||
30 | |||
31 | $options = array_merge( |
||
32 | array( |
||
33 | 'uri' => null, |
||
34 | 'label' => null, |
||
35 | 'attributes' => array(), |
||
36 | 'linkAttributes' => array(), |
||
37 | 'childrenAttributes' => array(), |
||
38 | 'labelAttributes' => array(), |
||
39 | 'extras' => array(), |
||
40 | 'display' => true, |
||
41 | 'displayChildren' => true, |
||
42 | 'pattern' => null, |
||
43 | ), |
||
44 | $options |
||
45 | ); |
||
46 | |||
47 | if (!empty($options['route'])) { |
||
48 | $params = isset($options['routeParameters']) ? $options['routeParameters'] : array(); |
||
49 | $absolute = isset($options['routeAbsolute']) ? $options['routeAbsolute'] : false; |
||
50 | $options['uri'] = $this->generator->generate($options['route'], $params, $absolute); |
||
51 | |||
52 | if (!isset($options['pattern']) || empty($options['pattern'])) { |
||
53 | $options['pattern'] = $options['uri']; |
||
54 | } |
||
55 | } |
||
56 | |||
57 | // Ajoute un dolar au pattern utilisé pour déterminer |
||
58 | // par regex s'il s'agit de l'item actuel |
||
59 | if (!empty($options['pattern']) && isset($options['pattern_strict']) |
||
60 | && $options['pattern_strict'] === true) { |
||
61 | $options['pattern'] .= '$'; |
||
62 | } |
||
63 | |||
64 | $item |
||
|
|||
65 | ->setUri($options['uri']) |
||
66 | ->setLabel($options['label']) |
||
67 | ->setAttributes($options['attributes']) |
||
68 | ->setLinkAttributes($options['linkAttributes']) |
||
69 | ->setChildrenAttributes($options['childrenAttributes']) |
||
70 | ->setLabelAttributes($options['labelAttributes']) |
||
71 | ->setExtras($options['extras']) |
||
72 | ->setDisplay($options['display']) |
||
73 | ->setDisplayChildren($options['displayChildren']) |
||
74 | ->setPattern($options['pattern']) |
||
75 | ; |
||
76 | |||
77 | return $item; |
||
78 | } |
||
79 | } |
||
80 |
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: