Completed
Push — master ( 1a67d2...dfab50 )
by Neomerx
11:35 queued 22s
created

FormRulesSerializer   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 116
rs 10
c 0
b 0
f 0
ccs 29
cts 29
cp 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A addRulesFromClass() 0 10 1
A addFormRules() 0 9 2
A getData() 0 7 1
A readBlocks() 0 4 1
A hasRules() 0 7 2
A readRules() 0 6 1
A readRuleMainIndexes() 0 4 1
A readRuleStartIndexes() 0 4 1
A readRuleEndIndexes() 0 4 1
A isRulesClass() 0 6 2
1
<?php namespace Limoncello\Flute\Validation\Form\Execution;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Flute\Contracts\Validation\FormRulesInterface;
20
use Limoncello\Flute\Contracts\Validation\FormRulesSerializerInterface;
21
use Limoncello\Flute\Validation\Serialize\RulesSerializer;
22
23
/**
24
 * @package Limoncello\Flute
25
 *
26
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
27
 */
28
class FormRulesSerializer extends RulesSerializer implements FormRulesSerializerInterface
29
{
30
    /**
31
     * @var array
32
     */
33
    private $serializedRules = [];
34
35
    /** Serialized indexes key */
36
    protected const SERIALIZED_RULES = 0;
37
38
    /** Serialized rules key */
39
    protected const SERIALIZED_BLOCKS = self::SERIALIZED_RULES + 1;
40
41
    /**
42
     * @inheritdoc
43
     */
44 30
    public function addRulesFromClass(string $rulesClass): FormRulesSerializerInterface
45
    {
46 30
        assert(static::isRulesClass($rulesClass));
0 ignored issues
show
Bug introduced by
Since isRulesClass() 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 isRulesClass() 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 30
        $name = $rulesClass;
49
50
        /** @var FormRulesInterface $rulesClass */
51
52 30
        return $this->addFormRules($name, $rulesClass::getAttributeRules());
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58 30
    public function addFormRules(string $name, ?array $attributeRules): FormRulesSerializerInterface
59
    {
60 30
        assert(!empty($name));
61 30
        assert(static::hasRules($name, $this->serializedRules) === false);
62
63 30
        $this->serializedRules[$name] = $attributeRules === null ? null : $this->addRules($attributeRules);
64
65 30
        return $this;
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71 30
    public function getData(): array
72
    {
73
        return [
74 30
            static::SERIALIZED_RULES  => $this->serializedRules,
75 30
            static::SERIALIZED_BLOCKS => $this->getBlocks(),
76
        ];
77
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82 3
    public static function readBlocks(array $serializedData): array
83
    {
84 3
        return $serializedData[static::SERIALIZED_BLOCKS];
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90 30
    public static function hasRules(string $name, array $serializedData): bool
91
    {
92
        // the value could be null so we have to check by key existence.
93
        return
94 30
            array_key_exists(static::SERIALIZED_RULES, $serializedData) === true &&
95 30
            array_key_exists($name, $serializedData[static::SERIALIZED_RULES]);
96
    }
97
98
    /**
99
     * @inheritdoc
100
     */
101 3
    public static function readRules(string $rulesClass, array $serializedData): array
102
    {
103 3
        assert(static::hasRules($rulesClass, $serializedData) === true);
104
105 3
        return $serializedData[static::SERIALIZED_RULES][$rulesClass];
106
    }
107
108
    /**
109
     * @inheritdoc
110
     */
111 3
    public static function readRuleMainIndexes(array $ruleIndexes): ?array
112
    {
113 3
        return parent::getRulesIndexes($ruleIndexes);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getRulesIndexes() instead of readRuleMainIndexes()). Are you sure this is correct? If so, you might want to change this to $this->getRulesIndexes().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
114
    }
115
116
    /**
117
     * @inheritdoc
118
     */
119 3
    public static function readRuleStartIndexes(array $ruleIndexes): array
120
    {
121 3
        return parent::getRulesStartIndexes($ruleIndexes);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getRulesStartIndexes() instead of readRuleStartIndexes()). Are you sure this is correct? If so, you might want to change this to $this->getRulesStartIndexes().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
122
    }
123
124
    /**
125
     * @inheritdoc
126
     */
127 3
    public static function readRuleEndIndexes(array $ruleIndexes): array
128
    {
129 3
        return parent::getRulesEndIndexes($ruleIndexes);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getRulesEndIndexes() instead of readRuleEndIndexes()). Are you sure this is correct? If so, you might want to change this to $this->getRulesEndIndexes().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
130
    }
131
132
    /**
133
     * @param string $rulesClass
134
     *
135
     * @return bool
136
     */
137 30
    private static function isRulesClass(string $rulesClass): bool
138
    {
139
        return
140 30
            class_exists($rulesClass) === true &&
141 30
            in_array(FormRulesInterface::class, class_implements($rulesClass)) === true;
142
    }
143
}
144