Completed
Pull Request — master (#109)
by Simone
03:56
created

Rule::ensureRuleNameIsValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
namespace Sensorario\Resources\Rulers;
4
5
class Rule
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 View Code Duplication
    public function ensureRuleNameIsValid()
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...
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 View Code Duplication
    public function getExpectedType()
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...
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