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

ContractCheckerAspect   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 70%

Importance

Changes 14
Bugs 1 Features 2
Metric Value
wmc 4
c 14
b 1
f 2
lcom 1
cbo 3
dl 0
loc 60
ccs 7
cts 10
cp 0.7
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A preConditionContract() 0 4 1
A postConditionContract() 0 4 1
A invariantContract() 0 4 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\Aspect;
12
13
use Doctrine\Common\Annotations\Reader;
14
use Go\Aop\Aspect;
15
use Go\Aop\Intercept\MethodInvocation;
16
use Go\Lang\Annotation\Around;
17
use Go\Lang\Annotation\Before;
18
use PhpDeal\Annotation as Contract;
19
use PhpDeal\Contract\InvariantContract;
20
use PhpDeal\Contract\PostconditionContract;
21
use PhpDeal\Contract\PreconditionContract;
22
use PhpDeal\Exception\ContractViolation;
23
24
class ContractCheckerAspect implements Aspect
25
{
26
    /**
27
     * Annotation reader
28
     *
29
     * @var Reader|null
30
     */
31
    private $reader = null;
32
33
    /**
34
     * Default constructor
35
     *
36
     * @param Reader $reader Annotation reader
37
     */
38
    public function __construct(Reader $reader)
39
    {
40
        $this->reader = $reader;
41
    }
42
43
    /**
44
     * Verifies pre-condition contract for the method
45
     *
46
     * @param MethodInvocation $invocation
47
     * @Before("@execution(PhpDeal\Annotation\Verify)")
48
     *
49
     * @throws ContractViolation
50
     */
51 19
    public function preConditionContract(MethodInvocation $invocation)
52
    {
53 19
        (new PreconditionContract($this->reader))->check($invocation);
0 ignored issues
show
Bug introduced by
It seems like $this->reader can be null; however, __construct() 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...
54 6
    }
55
56
    /**
57
     * Verifies post-condition contract for the method
58
     *
59
     * @Around("@execution(PhpDeal\Annotation\Ensure)")
60
     * @param MethodInvocation $invocation
61
     *
62
     * @throws ContractViolation
63
     * @return mixed
64
     */
65 8
    public function postConditionContract(MethodInvocation $invocation)
66
    {
67 8
        return (new PostconditionContract($this->reader))->check($invocation);
0 ignored issues
show
Bug introduced by
It seems like $this->reader can be null; however, __construct() 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
    }
69
70
    /**
71
     * Verifies invariants for contract class
72
     *
73
     * @Around("@within(PhpDeal\Annotation\Invariant) && execution(public **->*(*))")
74
     * @param MethodInvocation $invocation
75
     *
76
     * @throws ContractViolation
77
     * @return mixed
78
     */
79 7
    public function invariantContract(MethodInvocation $invocation)
80
    {
81 7
        return (new InvariantContract($this->reader))->check($invocation);
0 ignored issues
show
Bug introduced by
It seems like $this->reader can be null; however, __construct() 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...
82
    }
83
}
84