| Conditions | 5 |
| Paths | 4 |
| Total Lines | 18 |
| Code Lines | 8 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 7 |
| CRAP Score | 5 |
| Changes | 0 | ||
| 1 | <?php |
||
| 5 | function validate_array(array $data, array $fields, string $exception = null): bool |
||
| 6 | { |
||
| 7 | 3 | foreach ($fields as $field) { |
|
| 8 | if ( |
||
| 9 | 3 | !isset($data[$field]) && // This is faster, |
|
| 10 | // but it will also return false on fields which |
||
| 11 | // value is null, so calling array_key_exists when that happens. |
||
| 12 | 3 | !array_key_exists($field, $data) |
|
| 13 | ) { |
||
| 14 | 2 | if ($exception === null) { |
|
| 15 | 1 | return false; |
|
| 16 | } |
||
| 17 | |||
| 18 | 2 | throw new $exception($data, $field); |
|
| 19 | } |
||
| 20 | } |
||
| 21 | |||
| 22 | 1 | return true; |
|
| 23 | } |
||
| 24 |