Passed
Push — master ( 2d5d0a...7d78c5 )
by Adrien
02:23
created

Utility   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 41.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 68
ccs 10
cts 24
cp 0.4167
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A entityIdToModel() 0 13 4
A getShortClassName() 0 5 1
A printFiles() 0 12 3
A unique() 0 10 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix;
6
7
use GraphQL\Doctrine\Definition\EntityID;
8
9
abstract class Utility
10
{
11
    /**
12
     * Returns the short class name of any object, eg: Application\Model\Calendar => Calendar
13
     *
14
     * @param object|string $object
15
     *
16
     * @return string
17
     */
18 3
    public static function getShortClassName($object): string
19
    {
20 3
        $reflect = new \ReflectionClass($object);
21
22 3
        return $reflect->getShortName();
23
    }
24
25
    /**
26
     * Print a list of files if non empty
27
     *
28
     * @param string $title
29
     * @param array $files
30
     */
31
    public static function printFiles(string $title, array $files): void
32
    {
33
        if (!$files) {
34
            return;
35
        }
36
37
        echo $title . PHP_EOL . PHP_EOL;
38
39
        foreach ($files as $file) {
40
            echo '    ' . escapeshellarg($file) . PHP_EOL;
41
        }
42
        echo PHP_EOL;
43
    }
44
45
    /**
46
     * Replace EntityID model and don't touch other values
47
     *
48
     * @param array $data mix of objects and scalar values
49
     *
50
     * @return null|array
51
     */
52 1
    public static function entityIdToModel(?array $data): ?array
53
    {
54 1
        if ($data === null) {
0 ignored issues
show
introduced by
The condition $data === null is always false.
Loading history...
55 1
            return null;
56
        }
57
58 1
        foreach ($data as &$value) {
59 1
            if ($value instanceof EntityID) {
60 1
                $value = $value->getEntity();
61
            }
62
        }
63
64 1
        return $data;
65
    }
66
67
    public static function unique(array $array): array
68
    {
69
        $result = [];
70
        foreach ($array as $value) {
71
            if (!in_array($value, $result, true)) {
72
                $result[] = $value;
73
            }
74
        }
75
76
        return $result;
77
    }
78
}
79