Completed
Push — master ( 40844b...7e878c )
by Sven
01:47
created

FlintRedisCacheFactory   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 62
Duplicated Lines 9.68 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 1
dl 6
loc 62
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getKey() 0 4 1
A getFullKey() 0 5 1
C create() 6 32 12
A newCacheInstance() 0 9 3

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
namespace Suven\FlintRedis;
3
4
class FlintRedisCacheFactory
5
{
6
    const STRATEGY_REDIS = 1;
7
    const STRATEGY_FLINTSTONE = 2;
8
9
    private static $cachedInstances = [];
10
11
    private static function getKey($realm, $strategy)
12
    {
13
        return "${realm}.${strategy}";
14
    }
15
16
    private static function getFullKey($key, $options)
17
    {
18
        $optionsKey = md5(json_encode($options));
19
        return "${key}.${optionsKey}";
20
    }
21
22
    public static function create($realm, $strategy = false, $options = false)
23
    {
24
        $key = self::getKey($realm, $strategy);
25
        $fullKey = self::getFullKey($key, $options);
26
27
        if ($strategy && $options && isset(self::$cachedInstances[$fullKey])) {
28
            return self::$cachedInstances[$fullKey];
29
        }
30
31 View Code Duplication
        if ($strategy && !$options && isset(self::$cachedInstances[$key])) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
32
            return self::$cachedInstances[$key];
33
        }
34
35 View Code Duplication
        if (!$strategy && !$options && isset(self::$cachedInstances[$realm])) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
36
            return self::$cachedInstances[$realm];
37
        }
38
39
        // No instance available so far... Lets create one with default values
40
        $strategy = $strategy ? $strategy : self::STRATEGY_REDIS;
41
        $options = $options ? $options : [];
42
43
        $key = self::getKey($realm, $strategy);
44
        $fullKey = self::getFullKey($key, $options);
45
46
        $instance = self::newCacheInstance($strategy, $realm, $options);
47
48
        self::$cachedInstances[$fullKey] = &$instance;
49
        self::$cachedInstances[$key] = &$instance;
50
        self::$cachedInstances[$realm] = &$instance;
51
52
        return $instance;
53
    }
54
55
    private static function newCacheInstance($strategy, $realm, $options) {
56
        if ($strategy === self::STRATEGY_REDIS) {
57
            return new FlintRedisCacheRedis($strategy, $realm, $options);
0 ignored issues
show
Unused Code introduced by
The call to FlintRedisCacheRedis::__construct() has too many arguments starting with $options.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
58
        }
59
60
        if ($strategy === self::STRATEGY_FLINTSTONE) {
61
            return new FlintRedisCacheRedis($strategy, $realm, $options);
0 ignored issues
show
Unused Code introduced by
The call to FlintRedisCacheRedis::__construct() has too many arguments starting with $options.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
62
        }
63
    }
64
65
}
66