Completed
Pull Request — master (#1)
by Ondrej
01:51
created

StrategyEnum   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A FIRST_VOTE_DECIDES() 4 4 1
A ALLOW_UNLESS_DENIED() 4 4 1
A DENY_UNLESS_ALLOWED() 4 4 1
A instance() 7 7 2
A __toString() 4 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 View Code Duplication
class StrategyEnum
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...
6
{
7
    /**
8
     * @var string
9
     */
10
    private $value;
11
12
    /**
13
     * @var self[]
14
     */
15
    static private $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');
0 ignored issues
show
Bug introduced by
Since instance() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of instance() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
29
    }
30
31
32
    /**
33
     * @return self
34
     */
35
    public static function ALLOW_UNLESS_DENIED()
36
    {
37
        return static::instance('allow_unless_denied');
0 ignored issues
show
Bug introduced by
Since instance() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of instance() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
38
    }
39
40
41
    /**
42
     * @return self
43
     */
44
    public static function DENY_UNLESS_ALLOWED()
45
    {
46
        return static::instance('deny_unless_allowed');
0 ignored issues
show
Bug introduced by
Since instance() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of instance() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
47
    }
48
49
50
    /**
51
     * @param $string
52
     * @return self
53
     */
54
    private static function instance($string)
55
    {
56
        if (!isset(static::$registry[$string])) {
0 ignored issues
show
Bug introduced by
Since $registry is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $registry to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
57
            static::$registry[$string] = new static($string);
0 ignored issues
show
Bug introduced by
Since $registry is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $registry to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
58
        }
59
        return static::$registry[$string];
0 ignored issues
show
Bug introduced by
Since $registry is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $registry to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
60
    }
61
62
63
    /**
64
     * @return string
65
     */
66
    public function __toString()
67
    {
68
        return $this->value;
69
    }
70
}
71