Completed
Pull Request — master (#5)
by Ondrej
01:58
created

StrategyEnum   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 8.33 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 7
loc 84
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A FIRST_VOTE_DECIDES() 0 4 1
A ALLOW_UNLESS_DENIED() 0 4 1
A DENY_UNLESS_ALLOWED() 0 4 1
A EVERYONE_MUST_ALLOW_TO_BE_ALLOWED() 0 4 1
A EVERYONE_MUST_DENY_TO_BE_DENIED() 0 4 1
A instance() 7 7 2
A __toString() 0 4 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
namespace SpareParts\Overseer;
3
4
5
class StrategyEnum
6
{
7
    /**
8
     * @var string
9
     */
10
    private $value;
11
12
    /**
13
     * @var self[]
14
     */
15
    static protected $registry = [];
16
17
    private function __construct($value)
18
    {
19
        $this->value = $value;
20
    }
21
22
23
    /**
24
     * @return self
25
     */
26
    public static function FIRST_VOTE_DECIDES()
27
    {
28
        return static::instance('first_vote_decides');
29
    }
30
31
32
    /**
33
     * @return self
34
     */
35
    public static function ALLOW_UNLESS_DENIED()
36
    {
37
        return static::instance('allow_unless_denied');
38
    }
39
40
41
    /**
42
     * @return self
43
     */
44
    public static function DENY_UNLESS_ALLOWED()
45
    {
46
        return static::instance('deny_unless_allowed');
47
    }
48
49
50
    /**
51
     * @return self
52
     */
53
    public static function EVERYONE_MUST_ALLOW_TO_BE_ALLOWED()
54
    {
55
        return static::instance('everyone_must_allow_to_be_allowed');
56
    }
57
58
59
    /**
60
     * @return self
61
     */
62
    public static function EVERYONE_MUST_DENY_TO_BE_DENIED()
63
    {
64
        return static::instance('everyone_must_deny_to_be_denied');
65
    }
66
67
68
    /**
69
     * @param string $string
70
     * @return self
71
     */
72 View Code Duplication
    protected static function instance($string)
0 ignored issues
show
Duplication introduced by
This method 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...
73
    {
74
        if (!isset(static::$registry[$string])) {
75
            static::$registry[$string] = new static($string);
76
        }
77
        return static::$registry[$string];
78
    }
79
80
81
    /**
82
     * @return string
83
     */
84
    public function __toString()
85
    {
86
        return $this->value;
87
    }
88
}
89