Completed
Push — master ( 8155ff...9fff48 )
by Changwan
03:00
created

Validator::assert()   B

Complexity

Conditions 6
Paths 11

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6.1666

Importance

Changes 0
Metric Value
cc 6
eloc 11
nc 11
nop 1
dl 0
loc 19
ccs 10
cts 12
cp 0.8333
crap 6.1666
rs 8.8571
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Validator;
3
4
use Wandu\Validator\Contracts\TesterInterface;
5
use Wandu\Validator\Exception\InvalidValueException;
6
7
class Validator
8
{
9
    /** @var string|\Wandu\Validator\Contracts\RuleInterface|\Wandu\Validator\Contracts\TesterInterface */
10
    protected $rule;
11
    
12
    /**
13
     * @param string|\Wandu\Validator\Contracts\RuleInterface|\Wandu\Validator\Contracts\TesterInterface $rule
14
     */
15 4
    public function __construct($rule)
16
    {
17 4
        $this->rule = $rule;
18 4
    }
19
20
    /**
21
     * @param mixed $data
22
     * @return void
23
     */
24 4
    public function assert($data)
25
    {
26 4
        if (is_string($this->rule)) {
27 1
            if (!tester($this->rule)->test($data)) {
28 1
                throw new InvalidValueException([$this->rule]);
29
            }
30
        }
31 3
        if ($this->rule instanceof TesterInterface) {
32
            if (!$this->rule->test($data)) {
33
                throw new InvalidValueException([get_class($this->rule)]);
34
            }
35
        }
36
        
37 3
        $errorBag = new ErrorBag();
38 3
        $this->rule->define(new AssertRuleDefinition($errorBag, $data));
0 ignored issues
show
Bug introduced by
The method define does only exist in Wandu\Validator\Contracts\RuleInterface, but not in Wandu\Validator\Contracts\TesterInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
39 3
        if (count($errorBag)) {
40 2
            throw new InvalidValueException($errorBag->errors());
41
        }
42 3
    }
43
44
    /**
45
     * @param mixed $data
46
     * @return bool
47
     */
48
    public function validate($data): bool
49
    {
50
        try {
51
            $this->assert($data);
52
            return true;
53
        } catch (InvalidValueException $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
54
        return false;
55
    }
56
}
57