Failed Conditions
Push — master ( a0775a...0669e5 )
by Adrien
09:15
created

Utility::getCacheKey()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 14
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 28
ccs 0
cts 15
cp 0
crap 42
rs 9.2222
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application;
6
7
use Cake\Chronos\Chronos;
8
9
abstract class Utility
10
{
11
    /**
12
     * @var null|Chronos
13
     */
14
    private static $now;
15
16
    /**
17
     * Returns now, always same value for a single PHP execution
18
     *
19
     * @return Chronos
20
     */
21 46
    public static function getNow(): Chronos
22
    {
23 46
        if (!self::$now) {
24 1
            self::$now = new Chronos();
25
        }
26
27 46
        return self::$now;
28
    }
29
30
    /**
31
     * Returns the short class name of any object, eg: Application\Model\Calendar => Calendar
32
     *
33
     * @param object $object
34
     *
35
     * @return string
36
     */
37 12
    public static function getShortClassName($object): string
38
    {
39 12
        $reflect = new \ReflectionClass($object);
40
41 12
        return $reflect->getShortName();
42
    }
43
44
    /**
45
     * Print a list of files if non empty
46
     *
47
     * @param string $title
48
     * @param array $files
49
     */
50
    public static function printFiles(string $title, array $files): void
51
    {
52
        if (!$files) {
53
            return;
54
        }
55
56
        echo $title . PHP_EOL . PHP_EOL;
57
58
        foreach ($files as $file) {
59
            echo '    ' . escapeshellarg($file) . PHP_EOL;
60
        }
61
        echo PHP_EOL;
62
    }
63
64
    /**
65
     * Returns a unique key identifying all arguments in the array, so we can use the result as cache key
66
     *
67
     * This only works for in-memory objects. The key returned should *never* be
68
     * persisted. And it may be expensive in memory because object are forced
69
     * not to be garbage collected.
70
     *
71
     * @param mixed $value
72
     *
73
     * @return string
74
     */
75
    public static function getCacheKey($value): string
76
    {
77
        static $preventGarbageCollectorFromDestroyingObject = [];
78
79
        if (is_object($value)) {
80
            $preventGarbageCollectorFromDestroyingObject[] = $value;
81
82
            return spl_object_hash($value);
83
        }
84
85
        if (is_array($value)) {
86
            $key = '[ARRAY|';
87
            foreach ($value as $i => $modelInCollection) {
88
                $key .= $i . '>' . self::getCacheKey($modelInCollection) . ':';
89
            }
90
91
            return $key . ']';
92
        }
93
94
        if (is_bool($value)) {
95
            return '[BOOL|' . $value . ']';
96
        }
97
98
        if ($value === null) {
99
            return '[NULL]';
100
        }
101
102
        return (string) $value;
103
    }
104
}
105