Conditions | 33 |
Paths | 24 |
Total Lines | 77 |
Code Lines | 53 |
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 |
||
87 | protected function checkConfig( array $config, array $attributes ) |
||
88 | { |
||
89 | $errors = []; |
||
90 | |||
91 | foreach( $config as $key => $def ) |
||
92 | { |
||
93 | if( $def['required'] === true && ( !isset( $attributes[$key] ) || $attributes[$key] === '' ) ) |
||
94 | { |
||
95 | $errors[$key] = sprintf( 'Configuration for "%1$s" is missing', $key ); |
||
96 | continue; |
||
97 | } |
||
98 | |||
99 | if( isset( $attributes[$key] ) && $attributes[$key] != '' ) |
||
100 | { |
||
101 | switch( $def['type'] ) |
||
102 | { |
||
103 | case 'boolean': |
||
104 | if( !is_string( $attributes[$key] ) || $attributes[$key] != '0' && $attributes[$key] != '1' ) { |
||
105 | $errors[$key] = sprintf( 'Not a true/false value' ); continue 2; |
||
106 | } |
||
107 | break; |
||
108 | case 'string': |
||
109 | if( is_string( $attributes[$key] ) === false ) { |
||
110 | $errors[$key] = sprintf( 'Not a string' ); continue 2; |
||
111 | } |
||
112 | break; |
||
113 | case 'integer': |
||
114 | if( ctype_digit( $attributes[$key] ) === false ) { |
||
115 | $errors[$key] = sprintf( 'Not an integer number' ); continue 2; |
||
116 | } |
||
117 | break; |
||
118 | case 'number': |
||
119 | if( is_numeric( $attributes[$key] ) === false ) { |
||
120 | $errors[$key] = sprintf( 'Not a number' ); continue 2; |
||
121 | } |
||
122 | break; |
||
123 | case 'date': |
||
124 | $pattern = '/^[0-9]{4}-[0-1][0-9]-[0-3][0-9]$/'; |
||
125 | if( !is_string( $attributes[$key] ) || preg_match( $pattern, $attributes[$key] ) !== 1 ) { |
||
126 | $errors[$key] = sprintf( 'Not a date' ); continue 2; |
||
127 | } |
||
128 | break; |
||
129 | case 'datetime': |
||
130 | $pattern = '/^[0-9]{4}-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9](:[0-5][0-9])?$/'; |
||
131 | if( !is_string( $attributes[$key] ) || preg_match( $pattern, $attributes[$key] ) !== 1 ) { |
||
132 | $errors[$key] = sprintf( 'Not a date and time' ); continue 2; |
||
133 | } |
||
134 | break; |
||
135 | case 'time': |
||
136 | $pattern = '/^([0-2])?[0-9]:[0-5][0-9](:[0-5][0-9])?$/'; |
||
137 | if( !is_string( $attributes[$key] ) || preg_match( $pattern, $attributes[$key] ) !== 1 ) { |
||
138 | $errors[$key] = sprintf( 'Not a time' ); continue 2; |
||
139 | } |
||
140 | break; |
||
141 | case 'list': |
||
142 | case 'select': |
||
143 | if( !is_array( $def['default'] ) || !isset( $def['default'][$attributes[$key]] ) |
||
144 | && !in_array( $attributes[$key], $def['default'] ) |
||
145 | ) { |
||
146 | $errors[$key] = sprintf( 'Not a listed value' ); continue 2; |
||
147 | } |
||
148 | break; |
||
149 | case 'map': |
||
150 | if( !is_array( $attributes[$key] ) ) { |
||
151 | $errors[$key] = sprintf( 'Not a key/value map' ); continue 2; |
||
152 | } |
||
153 | break; |
||
154 | default: |
||
155 | throw new \Aimeos\MShop\Service\Exception( sprintf( 'Invalid type "%1$s"', $def['type'] ) ); |
||
156 | } |
||
157 | } |
||
158 | |||
159 | $errors[$key] = null; |
||
160 | } |
||
161 | |||
162 | return $errors; |
||
163 | } |
||
164 | |||
237 | } |