Completed
Pull Request — master (#109)
by Simone
02:19
created

Rule   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 114
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 0
dl 114
loc 114
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A fromArray() 10 10 2
A ensureRuleNameIsValid() 10 10 2
A asArray() 4 4 1
A isValid() 4 4 1
A is() 4 4 1
A isNot() 4 4 1
A isCustom() 4 4 1
A isNotCustom() 4 4 1
A isNotMail() 4 4 1
A getRuleType() 4 4 1
A isObject() 4 4 1
A getObjectType() 4 4 1
A getValue() 4 4 1
A getExpectedType() 10 10 3
A isValueNotAnObject() 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
3
namespace Sensorario\Resources\Rulers;
4
5 View Code Duplication
class Rule
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
    const TYPE_OBJECT = 'object';
8
9
    const TYPE_SCALAR = 'scalar';
10
11
    const TYPE_CUSTOM = 'custom-validator';
12
13
    private static $avalableRuleTypes = [
14
        self::TYPE_OBJECT,
15
        self::TYPE_SCALAR,
16
        self::TYPE_CUSTOM,
17
    ];
18
19
    private $rule;
20
21
    private function __construct(array $rule) 
22
    {
23
        $this->rule = $rule;
24
    }
25
26
    public static function fromArray(array $rule)
27
    {
28
        if ([] === $rule) {
29
            throw new \LogicException(
30
                'rule type is not defined'
31
            );
32
        }
33
34
        return new self($rule);
35
    }
36
37
    public function ensureRuleNameIsValid()
38
    {
39
        if (!$this->isValid()) {
40
            throw new \RuntimeException(
41
                'Oops! Invalid configuration!!!'
42
                . 'Type `' . key($this->rule) . '` is not valid. '
43
                . 'Available types are ' . var_export(static::$avalableRuleTypes, true)
0 ignored issues
show
Bug introduced by
Since $avalableRuleTypes is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $avalableRuleTypes 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...
44
            );
45
        }
46
    }
47
48
    public function asArray() : array
49
    {
50
         return $this->rule;
51
    }
52
53
    public function isValid()
54
    {
55
        return in_array(key($this->rule), static::$avalableRuleTypes);
0 ignored issues
show
Bug introduced by
Since $avalableRuleTypes is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $avalableRuleTypes 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...
56
    }
57
58
    public function is($type)
59
    {
60
        return key($this->rule) === $type;
61
    }
62
63
    public function isNot($type)
64
    {
65
        return !$this->is($type);
66
    }
67
68
    public function isCustom()
69
    {
70
        return $this->is(Rule::TYPE_CUSTOM);
71
    }
72
73
    public function isNotCustom()
74
    {
75
        return !$this->isCustom();
76
    }
77
78
    public function isNotMail()
79
    {
80
        return 'email' != $this->getValue();
81
    }
82
83
    public function getRuleType()
84
    {
85
        return key($this->rule);
86
    }
87
88
    public function isObject()
89
    {
90
        return isset($this->rule[self::TYPE_OBJECT]);
91
    }
92
93
    public function getObjectType()
94
    {
95
        return $this->rule['object'];
96
    }
97
98
    public function getValue()
99
    {
100
        return current($this->rule);
101
    }
102
103
    public function getExpectedType()
104
    {
105
        $expectedType = $this->isObject()
106
            ? $this->getObjectType()
107
            : 'undefined';
108
109
        return $this->getRuleType() == 'scalar'
110
            ? $this->getValue()
111
            : $expectedType;
112
    }
113
114
    public function isValueNotAnObject()
115
    {
116
        return 'array' !== $this->getValue();
117
    }
118
}
119