Completed
Push — master ( 89b65c...bde278 )
by Alexander
02:30
created

PostconditionContract::fetchAllContracts()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 3
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);
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,
68 8
            $invocation->getMethod()->name
69
        );
70
    }
71
}
72