CacheKeys   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 115
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 115
loc 115
ccs 0
cts 50
cp 0
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A putKey() 12 12 2
A getKeys() 7 7 2
A loadKeys() 18 18 4
A storeKeys() 8 8 2
A getFileKeys() 6 6 1
A __callStatic() 6 6 1
A __call() 6 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
 * This file is part of the Laravel Platfourm package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\Platfourm\Repository\Helpers;
12
13
/**
14
 * Class CacheKeys.
15
 */
16 View Code Duplication
class CacheKeys
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
{
18
    /**
19
     * @var string
20
     */
21
    protected static $storeFile = 'repository-cache-keys.json';
22
23
    /**
24
     * @var array
25
     */
26
    protected static $keys = null;
27
28
    /**
29
     * @param $group
30
     * @param $key
31
     *
32
     * @return void
33
     */
34
    public static function putKey($group, $key)
35
    {
36
        self::loadKeys();
37
38
        self::$keys[$group] = self::getKeys($group);
39
40
        if (!in_array($key, self::$keys[$group])) {
41
            self::$keys[$group][] = $key;
42
        }
43
44
        self::storeKeys();
45
    }
46
47
    /**
48
     * @param $group
49
     *
50
     * @return array|mixed
51
     */
52
    public static function getKeys($group)
53
    {
54
        self::loadKeys();
55
        self::$keys[$group] = isset(self::$keys[$group]) ? self::$keys[$group] : [];
56
57
        return self::$keys[$group];
58
    }
59
60
    /**
61
     * @return array|mixed
62
     */
63
    public static function loadKeys()
64
    {
65
66
        if (!is_null(self::$keys) && is_array(self::$keys)) {
67
            return self::$keys;
68
        }
69
70
        $file = self::getFileKeys();
71
72
        if (!file_exists($file)) {
73
            self::storeKeys();
74
        }
75
76
        $content    = file_get_contents($file);
77
        self::$keys = json_decode($content, true);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_decode($content, true) of type * is incompatible with the declared type array of property $keys.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
78
79
        return self::$keys;
80
    }
81
82
    /**
83
     * @return int
84
     */
85
    public static function storeKeys()
86
    {
87
        $file       = self::getFileKeys();
88
        self::$keys = is_null(self::$keys) ? [] : self::$keys;
89
        $content    = json_encode(self::$keys);
90
91
        return file_put_contents($file, $content);
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public static function getFileKeys()
98
    {
99
        $file = storage_path('framework/cache/' . self::$storeFile);
100
101
        return $file;
102
    }
103
104
    /**
105
     * @param $method
106
     * @param $parameters
107
     *
108
     * @return mixed
109
     */
110
    public static function __callStatic($method, $parameters)
111
    {
112
        $instance = new static();
113
114
        return call_user_func_array([$instance, $method], $parameters);
115
    }
116
117
    /**
118
     * @param $method
119
     * @param $parameters
120
     *
121
     * @return mixed
122
     */
123
    public function __call($method, $parameters)
124
    {
125
        $instance = new static();
126
127
        return call_user_func_array([$instance, $method], $parameters);
128
    }
129
130
}
131