| Conditions | 36 |
| Paths | 30 |
| Total Lines | 76 |
| Code Lines | 54 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 43 | public function check( array $config ) : array |
||
| 44 | { |
||
| 45 | $errors = []; |
||
| 46 | |||
| 47 | foreach( $this->criteria as $key => $attr ) |
||
| 48 | { |
||
| 49 | if( $attr->isRequired() === true && ( !isset( $config[$key] ) || $config[$key] === '' ) ) { |
||
| 50 | $errors[$key] = sprintf( 'Configuration for "%1$s" is missing', $key ); continue; |
||
| 51 | } |
||
| 52 | |||
| 53 | if( isset( $config[$key] ) ) |
||
| 54 | { |
||
| 55 | switch( $attr->getType() ) |
||
| 56 | { |
||
| 57 | case 'bool': |
||
| 58 | case 'boolean': |
||
| 59 | if( !in_array( $config[$key], ['', '0', '1'] ) ) { |
||
| 60 | $errors[$key] = sprintf( 'Not a true/false value' ); continue 2; |
||
| 61 | } |
||
| 62 | break; |
||
| 63 | case 'string': |
||
| 64 | case 'text': |
||
| 65 | if( !is_string( $config[$key] ) && !is_numeric( $config[$key] ) ) { |
||
| 66 | $errors[$key] = sprintf( 'Not a string' ); continue 2; |
||
| 67 | } |
||
| 68 | break; |
||
| 69 | case 'int': |
||
| 70 | case 'integer': |
||
| 71 | if( !is_integer( $config[$key] ) && !( is_string( $config[$key] ) && ctype_digit( $config[$key] ) ) ) { |
||
| 72 | $errors[$key] = sprintf( 'Not an integer number' ); continue 2; |
||
| 73 | } |
||
| 74 | break; |
||
| 75 | case 'number': |
||
| 76 | if( !is_numeric( $config[$key] ) ) { |
||
| 77 | $errors[$key] = sprintf( 'Not a number' ); continue 2; |
||
| 78 | } |
||
| 79 | break; |
||
| 80 | case 'date': |
||
| 81 | $pattern = '/^[0-9]{4}-[0-1][0-9]-[0-3][0-9]$/'; |
||
| 82 | if( !is_string( $config[$key] ) || preg_match( $pattern, $config[$key] ) !== 1 ) { |
||
| 83 | $errors[$key] = sprintf( 'Not a date' ); continue 2; |
||
| 84 | } |
||
| 85 | break; |
||
| 86 | case 'datetime': |
||
| 87 | $pattern = '/^[0-9]{4}-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9](:[0-5][0-9])?$/'; |
||
| 88 | if( !is_string( $config[$key] ) || preg_match( $pattern, $config[$key] ) !== 1 ) { |
||
| 89 | $errors[$key] = sprintf( 'Not a date and time' ); continue 2; |
||
| 90 | } |
||
| 91 | break; |
||
| 92 | case 'time': |
||
| 93 | $pattern = '/^([0-2])?[0-9]:[0-5][0-9](:[0-5][0-9])?$/'; |
||
| 94 | if( !is_string( $config[$key] ) || preg_match( $pattern, $config[$key] ) !== 1 ) { |
||
| 95 | $errors[$key] = sprintf( 'Not a time' ); continue 2; |
||
| 96 | } |
||
| 97 | break; |
||
| 98 | case 'list': |
||
| 99 | case 'select': |
||
| 100 | $default = (array) $attr->getDefault(); |
||
| 101 | if( !empty( $default ) && !isset( $default[$config[$key]] ) && !in_array( $config[$key], $default ) ) { |
||
| 102 | $errors[$key] = sprintf( 'Not a listed value' ); continue 2; |
||
| 103 | } |
||
| 104 | break; |
||
| 105 | case 'map': |
||
| 106 | if( !is_array( $config[$key] ) ) { |
||
| 107 | $errors[$key] = sprintf( 'Not a key/value map' ); continue 2; |
||
| 108 | } |
||
| 109 | break; |
||
| 110 | default: |
||
| 111 | throw new \Aimeos\MShop\Exception( sprintf( 'Invalid type "%1$s"', $attr->getType() ) ); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | $errors[$key] = null; |
||
| 116 | } |
||
| 117 | |||
| 118 | return $errors; |
||
| 119 | } |
||
| 121 |