|
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); |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
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: