FormatterService::unwrapIfArray()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 4
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\Formatters;
6
7
use MichaelRubel\Formatters\Exceptions\ShouldImplementInterfaceException;
8
use MichaelRubel\Formatters\Exceptions\ShouldNotUseCamelCaseException;
9
10
class FormatterService implements FormatterServiceInterface
11
{
12
    /**
13
     * Ensures all the formatters will implement the same interface.
14
     *
15
     * @param  object  $formatter
16
     */
17 73
    public static function ensureFormatterImplementsInterface(object $formatter): void
18
    {
19 73
        if (! $formatter instanceof Formatter) {
20 2
            if (config('formatters.bindings_case') === 'camel') {
21 1
                throw new ShouldNotUseCamelCaseException();
22
            }
23
24 1
            throw new ShouldImplementInterfaceException();
25
        }
26
    }
27
28
    /**
29
     * Unwrap the array if it is only one passed parameter.
30
     *
31
     * @param  mixed  $items
32
     *
33
     * @return mixed
34
     */
35 75
    public static function unwrapIfArray(mixed $items): mixed
36
    {
37 75
        $first = current($items);
38
39 75
        if (is_array($first) && count($items) === 1) {
40 25
            return $first;
41
        }
42
43 55
        return $items;
44
    }
45
}
46