Completed
Pull Request — master (#10)
by
unknown
03:04
created

PostconditionContract   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 54
ccs 22
cts 22
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 17 2
A fetchAllContracts() 0 11 3
A fetchParentsContracts() 0 8 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\MethodConditionFetcher;
15
use PhpDeal\Exception\ContractViolation;
16
use PhpDeal\Annotation\Ensure;
17
18
class PostconditionContract extends Contract
19
{
20
    /**
21
     * @param MethodInvocation $invocation
22
     * @throws ContractViolation
23
     * @return mixed
24
     */
25 8
    public function check(MethodInvocation $invocation)
26
    {
27 8
        $object = $invocation->getThis();
28 8
        $args   = $this->getMethodArguments($invocation);
29 8
        $class  = $invocation->getMethod()->getDeclaringClass();
30 8
        if ($class->isCloneable()) {
31 8
            $args['__old'] = clone $object;
32
        }
33
34 8
        $result = $invocation->proceed();
35 8
        $args['__result'] = $result;
36 8
        $allContracts = $this->makeContractsUnique($this->fetchAllContracts($invocation));
37
38 8
        $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 27 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...
39
40 3
        return $result;
41
    }
42
43
    /**
44
     * @param MethodInvocation $invocation
45
     * @return array
46
     */
47 8
    private function fetchAllContracts(MethodInvocation $invocation)
48
    {
49 8
        $allContracts = $this->fetchParentsContracts($invocation);
50 8
        foreach ($invocation->getMethod()->getAnnotations() as $annotation) {
51 8
            if ($annotation instanceof Ensure) {
52 8
                $allContracts[] = $annotation;
53
            }
54
        }
55
56 8
        return $allContracts;
57
    }
58
59
    /**
60
     * @param MethodInvocation $invocation
61
     * @return array
62
     */
63 8
    private function fetchParentsContracts(MethodInvocation $invocation)
64
    {
65 8
        return (new MethodConditionFetcher(Ensure::class))->getConditions(
66 8
            $invocation->getMethod()->getDeclaringClass(),
67 8
            $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...
68 8
            $invocation->getMethod()->getName()
0 ignored issues
show
Bug introduced by
Consider using $invocation->getMethod()->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
69
        );
70
    }
71
}
72