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

InvariantContract   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 1
cbo 4
dl 0
loc 56
ccs 20
cts 20
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 17 2
A fetchAllContracts() 0 11 3
A fetchParentsContracts() 0 7 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);
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
72
        );
73
    }
74
}
75