Conditions | 16 |
Paths | 4608 |
Total Lines | 64 |
Code Lines | 35 |
Lines | 22 |
Ratio | 34.38 % |
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 |
||
71 | public function update( $new_instance, $old_instance ) { |
||
72 | $instance = array(); |
||
73 | |||
74 | if ( in_array( $new_instance['hide'], array( 'button', 'scroll', 'time' ) ) ) { |
||
75 | $instance['hide'] = $new_instance['hide']; |
||
76 | } |
||
77 | |||
78 | if ( isset( $new_instance['hide-timeout'] ) ) { |
||
79 | // time can be a value between 5 and 1000 seconds |
||
80 | $instance['hide-timeout'] = min( 1000, max( 3, intval( $new_instance['hide-timeout'] ) ) ); |
||
81 | } |
||
82 | |||
83 | View Code Duplication | if ( in_array( $new_instance['text'], array( 'default', 'custom' ) ) ) { |
|
84 | $instance['text'] = $new_instance['text']; |
||
85 | } |
||
86 | |||
87 | View Code Duplication | if ( isset( $new_instance['customtext'] ) ) { |
|
88 | $instance['customtext'] = mb_substr( $new_instance['customtext'], 0, 4096 ); |
||
89 | } else { |
||
90 | $instance['text'] = 'default'; |
||
91 | } |
||
92 | |||
93 | View Code Duplication | if ( isset( $new_instance['color-scheme'] ) ) { |
|
94 | $instance['color-scheme'] = 'negative' === $new_instance['color-scheme'] ? 'negative' : 'default'; |
||
95 | } else { |
||
96 | $instance['color-scheme'] = 'default'; |
||
97 | } |
||
98 | |||
99 | View Code Duplication | if ( in_array( $new_instance['policy-url'], array( 'default', 'custom' ) ) ) { |
|
100 | $instance['policy-url'] = $new_instance['policy-url']; |
||
101 | } |
||
102 | |||
103 | if ( isset( $new_instance['custom-policy-url'] ) ) { |
||
104 | $instance['custom-policy-url'] = esc_url( $new_instance, array( 'http', 'https' ) ); |
||
105 | |||
106 | if ( strlen( $instance['custom-policy-url'] ) < 10 ) { |
||
107 | unset( $instance['custom-policy-url'] ); |
||
108 | $instance['policy-url'] = 'default'; |
||
109 | } |
||
110 | } else { |
||
111 | $instance['policy-url'] = 'default'; |
||
112 | } |
||
113 | |||
114 | View Code Duplication | if ( isset( $new_instance['policy-link-text'] ) ) { |
|
115 | $instance['policy-link-text'] = trim( mb_substr( $new_instance['policy-link-text'], 0, 100 ) ); |
||
116 | } |
||
117 | |||
118 | if ( empty( $instance['policy-link-text'] ) || $instance['policy-link-text'] == $this->defaults['policy-link-text'] ) { |
||
119 | unset( $instance['policy-link-text'] ); |
||
120 | } |
||
121 | |||
122 | View Code Duplication | if ( isset( $new_instance['button'] ) ) { |
|
123 | $instance['button'] = trim( mb_substr( $new_instance['button'], 0, 100 ) ); |
||
124 | } |
||
125 | |||
126 | if ( empty( $instance['button'] ) || $instance['button'] == $this->defaults['button'] ) { |
||
127 | unset( $instance['button'] ); |
||
128 | } |
||
129 | |||
130 | // show the banner again if a setting has been changed |
||
131 | setcookie( self::$cookie_name, '', time() - 86400, '/' ); |
||
132 | |||
133 | return $instance; |
||
134 | } |
||
135 | |||
165 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.