ArrayUtils::toArray()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 4
eloc 8
c 2
b 0
f 1
nc 4
nop 1
dl 0
loc 17
ccs 10
cts 10
cp 1
crap 4
rs 10
1
<?php
2
/**
3
 * Licensed to CRATE Technology GmbH("Crate") under one or more contributor
4
 * license agreements.  See the NOTICE file distributed with this work for
5
 * additional information regarding copyright ownership.  Crate licenses
6
 * this file to you under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.  You may
8
 * obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
15
 * License for the specific language governing permissions and limitations
16
 * under the License.
17
 *
18
 * However, if you have executed another commercial license agreement
19
 * with Crate these terms will supersede the license and you may use the
20
 * software solely pursuant to the terms of the relevant commercial agreement.
21
 */
22
23
declare(strict_types=1);
24
25
namespace Crate\Stdlib;
26
27
use Traversable;
28
29
final class ArrayUtils
30
{
31
    /**
32
     * Convert a optional value that when used is expected to be an array
33
     *
34
     * @param mixed $value
35
     *
36
     * @throws Exception\InvalidArgumentException
37
     *
38
     * @return array
39
     */
40 10
    public static function toArray($value)
41
    {
42 10
        if (is_array($value)) {
43 4
            return $value;
44
        }
45
46 8
        if ($value === null) {
47 4
            return [];
48
        }
49
50 4
        if (!$value instanceof Traversable) {
51 2
            throw new Exception\InvalidArgumentException(
52 2
                'An invalid value was provided, should either be an null, array or Traversable'
53 2
            );
54
        }
55
56 2
        return iterator_to_array($value);
57
    }
58
}
59