Completed
Push — master ( 89b65c...bde278 )
by Alexander
02:30
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);
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