SingleValidation::setRule()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Validation\Validator;
4
5
/**
6
 * Copyright 2015-2020 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Limoncello\Validation\Contracts\Captures\CaptureAggregatorInterface;
22
use Limoncello\Validation\Contracts\Errors\ErrorAggregatorInterface;
23
use Limoncello\Validation\Contracts\Rules\RuleInterface;
24
use Limoncello\Validation\Execution\BlockInterpreter;
25
use Limoncello\Validation\Execution\BlockSerializer;
26
27
/**
28
 * @package Limoncello\Validation
29
 *
30
 * The trait expects the following method to be implemented by a class that uses this trait.
31
 * - createContextStorageFromBlocks(array $blocks): ContextStorageInterface
32
 */
33
trait SingleValidation
34
{
35
    /**
36
     * @var RuleInterface
37
     */
38
    private $rule;
39
40
    /**
41
     * @return RuleInterface
42
     */
43 4
    protected function getRule(): RuleInterface
44
    {
45 4
        return $this->rule;
46
    }
47
48
    /**
49
     * @param RuleInterface $rule
50
     *
51
     * @return self
52
     */
53 4
    private function setRule(RuleInterface $rule): self
54
    {
55 4
        $this->rule = $rule;
56
57 4
        return $this;
58
    }
59
60
    /**
61
     * @param mixed                      $input
62
     * @param CaptureAggregatorInterface $captures
63
     * @param ErrorAggregatorInterface   $errors
64
     *
65
     * @return void
66
     *
67
     * @SuppressWarnings(PHPMD.StaticAccess)
68
     */
69 4
    private function validateSingleImplementation(
70
        $input,
71
        CaptureAggregatorInterface $captures,
72
        ErrorAggregatorInterface $errors
73
    ): void {
74 4
        $serialized = (new BlockSerializer())->serialize($this->getRule()->toBlock())->get();
75 4
        $blocks     = BlockSerializer::unserializeBlocks($serialized);
76
77
        // the method is expected to be implemented by a class that uses this trait
78 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...
79
80 4
        BlockInterpreter::execute($input, $serialized, $context, $captures, $errors);
81
    }
82
}
83