Passed
Push — master ( 6afdcd...dc022a )
by Hirofumi
05:03
created

SpecificationVisitor::dispatch()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 13
cp 0
rs 9.8333
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 20
1
<?php
2
3
namespace Shippinno\Notification\Domain\Model;
4
5
use Doctrine\Common\Collections\Criteria;
6
use Tanigami\Specification\AndSpecification;
7
use Tanigami\Specification\OrSpecification;
8
use Tanigami\Specification\Specification;
9
10
class SpecificationVisitor
11
{
12
    public function walkAnd(AndSpecification $specification): Criteria
13
    {
14
        return $this->dispatch($specification->one())->andWhere(
15
            $this->dispatch($specification->other())->getWhereExpression()
0 ignored issues
show
Bug introduced by
It seems like $this->dispatch($specifi...)->getWhereExpression() can be null; however, andWhere() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
16
        );
17
    }
18
19
    public function walkOr(OrSpecification $specification): Criteria
20
    {
21
        return $this->dispatch($specification->one())->orWhere(
22
            $this->dispatch($specification->other())->getWhereExpression()
0 ignored issues
show
Bug introduced by
It seems like $this->dispatch($specifi...)->getWhereExpression() can be null; however, orWhere() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
23
        );
24
    }
25
26
    public function dispatch(Specification $specification): Criteria
27
    {
28
        switch (true) {
29
            case ($specification instanceof AndSpecification):
30
                return $this->walkAnd($specification);
31
            case ($specification instanceof OrSpecification):
32
                return $this->walkOr($specification);
33
            case ($specification instanceof NotificationMetadataSpecification):
34
                return $specification->criteria();
0 ignored issues
show
Bug introduced by
The method criteria() does not seem to exist on object<Shippinno\Notific...nMetadataSpecification>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
35
            default:
36
                throw new \RuntimeException();
37
        }
38
    }
39
}
40