Completed
Pull Request — master (#10)
by
unknown
02:46
created

InvariantContract::fetchParentsContracts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * PHP Deal framework
4
 *
5
 * @copyright Copyright 2014, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace PhpDeal\Contract;
12
13
use Go\Aop\Intercept\MethodInvocation;
14
use PhpDeal\Contract\Fetcher\ParentClass\InvariantFetcher;
15
use PhpDeal\Exception\ContractViolation;
16
use PhpDeal\Annotation\Invariant;
17
use ReflectionClass;
18
19
class InvariantContract extends Contract
20
{
21
    /**
22
     * Verifies invariants for contract class
23
     *
24
     * @Around("@within(PhpDeal\Annotation\Invariant) && execution(public **->*(*))")
25
     * @param MethodInvocation $invocation
26
     * @throws ContractViolation
27
     * @return mixed
28
     */
29 7
    public function check(MethodInvocation $invocation)
30
    {
31 7
        $object = $invocation->getThis();
32 7
        $args   = $this->getMethodArguments($invocation);
33 7
        $class  = $invocation->getMethod()->getDeclaringClass();
34 7
        if ($class->isCloneable()) {
35 7
            $args['__old'] = clone $object;
36
        }
37
38 7
        $result = $invocation->proceed();
39 7
        $args['__result'] = $result;
40
41 7
        $allContracts = $this->makeContractsUnique($this->fetchAllContracts($class));
42 7
        $this->fulfillContracts($allContracts, $object, $class->name, $args, $invocation);
0 ignored issues
show
Bug introduced by
It seems like $object defined by $invocation->getThis() on line 31 can also be of type string; however, PhpDeal\Contract\Contract::fulfillContracts() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
43
44 3
        return $result;
45
    }
46
47
    /**
48
     * @param ReflectionClass $class
49
     * @return array
50
     */
51 7
    private function fetchAllContracts(ReflectionClass $class)
52
    {
53 7
        $allContracts = $this->fetchParentsContracts($class);
54 7
        foreach ($this->reader->getClassAnnotations($class) as $annotation) {
55 7
            if ($annotation instanceof Invariant) {
56 7
                $allContracts[] = $annotation;
57
            }
58
        }
59
60 7
        return $allContracts;
61
    }
62
63
    /**
64
     * @param ReflectionClass $class
65
     * @return array
66
     */
67 7
    private function fetchParentsContracts(ReflectionClass $class)
68
    {
69 7
        return (new InvariantFetcher(Invariant::class))->getConditions(
70
            $class,
71 7
            $this->reader
0 ignored issues
show
Bug introduced by
It seems like $this->reader can be null; however, getConditions() 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...
72
        );
73
    }
74
}
75