Conditions | 14 |
Paths | 384 |
Total Lines | 76 |
Code Lines | 25 |
Lines | 76 |
Ratio | 100 % |
Changes | 2 | ||
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 namespace Anomaly\Streams\Platform\Ui\Form\Component\Button; |
||
38 | protected function process($key, $button) |
||
39 | { |
||
40 | /* |
||
41 | * If the button is a string then use |
||
42 | * it as the button parameter. |
||
43 | */ |
||
44 | if (is_string($button)) { |
||
45 | $button = [ |
||
46 | 'button' => $button, |
||
47 | ]; |
||
48 | } |
||
49 | |||
50 | /* |
||
51 | * If the key is a string and the button |
||
52 | * is an array without a button param then |
||
53 | * move the key into the button as that param. |
||
54 | */ |
||
55 | if (!is_integer($key) && !isset($button['button'])) { |
||
56 | $button['button'] = $key; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Default to size "sm" |
||
61 | */ |
||
62 | if (!isset($button['size'])) { |
||
63 | $button['size'] = 'sm'; |
||
64 | } |
||
65 | |||
66 | /* |
||
67 | * Make sure some default parameters exist. |
||
68 | */ |
||
69 | $button['attributes'] = array_get($button, 'attributes', []); |
||
70 | |||
71 | /* |
||
72 | * Move the HREF if any to the attributes. |
||
73 | */ |
||
74 | if (isset($button['href'])) { |
||
75 | array_set($button['attributes'], 'href', array_pull($button, 'href')); |
||
76 | } |
||
77 | |||
78 | /* |
||
79 | * Move the target if any to the attributes. |
||
80 | */ |
||
81 | if (isset($button['target'])) { |
||
82 | array_set($button['attributes'], 'target', array_pull($button, 'target')); |
||
83 | } |
||
84 | |||
85 | /* |
||
86 | * Move all data-* keys |
||
87 | * to attributes. |
||
88 | */ |
||
89 | foreach ($button as $attribute => $value) { |
||
90 | if (str_is('data-*', $attribute)) { |
||
91 | array_set($button, 'attributes.' . $attribute, array_pull($button, $attribute)); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | /* |
||
96 | * Make sure the HREF is absolute. |
||
97 | */ |
||
98 | if ( |
||
99 | isset($button['attributes']['href']) && |
||
100 | is_string($button['attributes']['href']) && |
||
101 | !starts_with($button['attributes']['href'], ['http', '{']) |
||
102 | ) { |
||
103 | $button['attributes']['href'] = url($button['attributes']['href']); |
||
104 | } |
||
105 | |||
106 | if (isset($button['dropdown'])) { |
||
107 | foreach ($button['dropdown'] as $key => &$dropdown) { |
||
108 | $dropdown = $this->process($key, $dropdown); |
||
109 | } |
||
110 | } |
||
111 | |||
112 | return $button; |
||
113 | } |
||
114 | } |
||
115 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.