1 | <?php |
||
24 | class ContractCheckerAspect implements Aspect |
||
25 | { |
||
26 | |||
27 | /** |
||
28 | * Annotation reader |
||
29 | * |
||
30 | * @var Reader|null |
||
31 | */ |
||
32 | private $reader = null; |
||
33 | |||
34 | /** |
||
35 | * Default constructor |
||
36 | * |
||
37 | * @todo Remove injection of reader |
||
38 | * @param Reader $reader Annotation reader |
||
39 | */ |
||
40 | public function __construct(Reader $reader) |
||
44 | |||
45 | /** |
||
46 | * Verifies pre-condition contract for the method |
||
47 | * |
||
48 | * @param MethodInvocation $invocation |
||
49 | * @Before("@execution(PhpDeal\Annotation\Verify)") |
||
50 | * |
||
51 | * @throws ContractViolation |
||
52 | */ |
||
53 | 3 | public function preConditionContract(MethodInvocation $invocation) |
|
69 | |||
70 | /** |
||
71 | * Verifies post-condition contract for the method |
||
72 | * |
||
73 | * @Around("@execution(PhpDeal\Annotation\Ensure)") |
||
74 | * @param MethodInvocation $invocation |
||
75 | * |
||
76 | * @throws ContractViolation |
||
77 | * @return mixed |
||
78 | */ |
||
79 | 3 | public function postConditionContract(MethodInvocation $invocation) |
|
103 | |||
104 | /** |
||
105 | * Verifies invariants for contract class |
||
106 | * |
||
107 | * @Around("@within(PhpDeal\Annotation\Invariant) && execution(public **->*(*))") |
||
108 | * @param MethodInvocation $invocation |
||
109 | * |
||
110 | * @throws ContractViolation |
||
111 | * @return mixed |
||
112 | */ |
||
113 | 3 | public function invariantContract(MethodInvocation $invocation) |
|
114 | { |
||
115 | 3 | $object = $invocation->getThis(); |
|
116 | 3 | $args = $this->getMethodArguments($invocation); |
|
117 | 3 | $class = $invocation->getMethod()->getDeclaringClass(); |
|
118 | 3 | if ($class->isCloneable()) { |
|
119 | 3 | $args['__old'] = clone $object; |
|
120 | } |
||
121 | |||
122 | 3 | $result = $invocation->proceed(); |
|
123 | 3 | $args['__result'] = $result; |
|
124 | |||
125 | // TODO: Do not use reader directly and pack annotation information into reflection |
||
126 | 3 | foreach ($this->reader->getClassAnnotations($class) as $annotation) { |
|
127 | 3 | if (!$annotation instanceof Contract\Invariant) { |
|
128 | 2 | continue; |
|
129 | } |
||
130 | |||
131 | 3 | if (!$this->isContractSatisfied($object, $class->name, $args, $annotation)) { |
|
132 | 3 | throw new ContractViolation($invocation, $annotation->value); |
|
133 | }; |
||
134 | } |
||
135 | |||
136 | 2 | return $result; |
|
137 | } |
||
138 | |||
139 | /** |
||
140 | * Returns a result of contract verification |
||
141 | * |
||
142 | * @param object|string $instance Invocation instance or string for static class |
||
143 | * @param string $scope Scope of method |
||
144 | * @param array $args List of arguments for the method |
||
145 | * @param Annotation $annotation Contract annotation |
||
146 | * |
||
147 | * @return mixed |
||
148 | */ |
||
149 | 9 | private function isContractSatisfied($instance, $scope, array $args, $annotation) |
|
163 | |||
164 | /** |
||
165 | * Returns an associative list of arguments for the method invocation |
||
166 | * |
||
167 | * @param MethodInvocation $invocation |
||
168 | * |
||
169 | * @return array |
||
170 | */ |
||
171 | 9 | private function getMethodArguments(MethodInvocation $invocation) |
|
181 | } |
||
182 |