Completed
Push — master ( f283ed...875b97 )
by Neomerx
05:05
created

SingleValidation::validateImplementation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 3
crap 1
1
<?php namespace Limoncello\Validation\Validator;
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\Validation\Contracts\Captures\CaptureAggregatorInterface;
20
use Limoncello\Validation\Contracts\Errors\ErrorAggregatorInterface;
21
use Limoncello\Validation\Contracts\Rules\RuleInterface;
22
use Limoncello\Validation\Execution\BlockInterpreter;
23
use Limoncello\Validation\Execution\BlockSerializer;
24
25
/**
26
 * @package Limoncello\Validation
27
 *
28
 * The trait expects the following method to be implemented by a class that uses this trait.
29
 * - createContextStorageFromBlocks(array $blocks): ContextStorageInterface
30
 */
31
trait SingleValidation
32
{
33
    /**
34
     * @var RuleInterface
35
     */
36
    private $rule;
37
38
    /**
39
     * @return RuleInterface
40
     */
41 4
    protected function getRule(): RuleInterface
42
    {
43 4
        return $this->rule;
44
    }
45
46
    /**
47
     * @param RuleInterface $rule
48
     *
49
     * @return self
50
     */
51 4
    private function setRule(RuleInterface $rule): self
52
    {
53 4
        $this->rule = $rule;
54
55 4
        return $this;
56
    }
57
58
    /**
59
     * @param mixed                      $input
60
     * @param CaptureAggregatorInterface $captures
61
     * @param ErrorAggregatorInterface   $errors
62
     *
63
     * @return void
64
     *
65
     * @SuppressWarnings(PHPMD.StaticAccess)
66
     */
67 4
    private function validateSingleImplementation(
68
        $input,
69
        CaptureAggregatorInterface $captures,
70
        ErrorAggregatorInterface $errors
71
    ): void {
72 4
        $serialized = (new BlockSerializer())->serialize($this->getRule()->toBlock())->get();
73 4
        $blocks     = BlockSerializer::unserializeBlocks($serialized);
74
75
        // the method is expected to be implemented by a class that uses this trait
76 4
        $context = $this->createContextStorageFromBlocks($blocks);
0 ignored issues
show
Bug introduced by
It seems like createContextStorageFromBlocks() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
77
78 4
        BlockInterpreter::execute($input, $serialized, $context, $captures, $errors);
79
    }
80
}
81