| Conditions | 15 |
| Paths | 42 |
| Total Lines | 38 |
| Lines | 5 |
| Ratio | 13.16 % |
| 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 |
||
| 49 | public static function check_data_from_schema( $data, $model, $current_data = null, $errors = array() ) { |
||
| 50 | $current_data = ( null === $current_data ) ? $data : $current_data; |
||
| 51 | |||
| 52 | foreach ( $model as $field_name => $field_def ) { |
||
| 53 | $value = null; |
||
| 54 | $error = null; |
||
| 55 | // Si la définition de la donnée ne contient pas "child". |
||
| 56 | if ( ! isset( $field_def['child'] ) ) { |
||
| 57 | |||
| 58 | // Si on est au premier niveau de $current_object, sinon si on est plus haut que le premier niveau. |
||
| 59 | if ( isset( $field_def['field'] ) && isset( $current_data[ $field_def['field'] ] ) ) { |
||
| 60 | $value = $current_data[ $field_def['field'] ]; |
||
| 61 | } elseif ( isset( $current_data[ $field_name ] ) && isset( $field_def ) && ! isset( $field_def['child'] ) ) { |
||
| 62 | $value = $current_data[ $field_name ]; |
||
| 63 | } |
||
| 64 | |||
| 65 | // Verifie si le champ est required. |
||
| 66 | if ( isset( $field_def['required'] ) && $field_def['required'] && null === $value ) { |
||
| 67 | $errors[] = $field_name . ' is required'; |
||
| 68 | } |
||
| 69 | |||
| 70 | // Vérifie le type de $value. |
||
| 71 | if ( null !== $value ) { |
||
| 72 | if ( ! self::check_type( $value, $field_name, $field_def['type'], $error ) ) { |
||
| 73 | $errors[] = $error; |
||
| 74 | } |
||
| 75 | } |
||
| 76 | } else { |
||
| 77 | // Values car c'est un tableau, nous sommes dans "child". Nous avons donc un tableau dans $data[ $field_name ]. |
||
| 78 | $values = ! empty( $data[ $field_name ] ) ? $data[ $field_name ] : array(); |
||
| 79 | |||
| 80 | // Récursivité sur les enfants de la définition courante. |
||
| 81 | $errors = self::check_data_from_schema( $data, $field_def['child'], $values, $errors ); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | return $errors; |
||
| 86 | } |
||
| 87 | |||
| 135 |