1 | <?php |
||
25 | class ContractCheckerAspect implements Aspect |
||
26 | { |
||
27 | |||
28 | /** |
||
29 | * Annotation reader |
||
30 | * |
||
31 | * @var Reader|null |
||
32 | */ |
||
33 | private $reader = null; |
||
34 | |||
35 | /** |
||
36 | * Default constructor |
||
37 | * |
||
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 | 8 | public function preConditionContract(MethodInvocation $invocation) |
|
54 | { |
||
55 | 8 | $object = $invocation->getThis(); |
|
56 | 8 | $args = $this->getMethodArguments($invocation); |
|
57 | 8 | $scope = $invocation->getMethod()->getDeclaringClass()->name; |
|
58 | |||
59 | 8 | foreach ($invocation->getMethod()->getAnnotations() as $annotation) { |
|
60 | 8 | if (!$annotation instanceof Contract\Verify) { |
|
61 | 1 | continue; |
|
62 | } |
||
63 | |||
64 | try { |
||
65 | 8 | $this->ensureContractSatisfied($object, $scope, $args, $annotation); |
|
66 | 5 | } catch (DomainException $e) { |
|
67 | 8 | throw new ContractViolation($invocation, $annotation->value, $e->getPrevious()); |
|
68 | } |
||
69 | } |
||
70 | 3 | } |
|
71 | |||
72 | /** |
||
73 | * Verifies post-condition contract for the method |
||
74 | * |
||
75 | * @Around("@execution(PhpDeal\Annotation\Ensure)") |
||
76 | * @param MethodInvocation $invocation |
||
77 | * |
||
78 | * @throws ContractViolation |
||
79 | * @return mixed |
||
80 | */ |
||
81 | 3 | public function postConditionContract(MethodInvocation $invocation) |
|
82 | { |
||
83 | 3 | $object = $invocation->getThis(); |
|
84 | 3 | $args = $this->getMethodArguments($invocation); |
|
85 | 3 | $class = $invocation->getMethod()->getDeclaringClass(); |
|
86 | 3 | if ($class->isCloneable()) { |
|
87 | 3 | $args['__old'] = clone $object; |
|
88 | } |
||
89 | |||
90 | 3 | $result = $invocation->proceed(); |
|
91 | 3 | $args['__result'] = $result; |
|
92 | |||
93 | 3 | foreach ($invocation->getMethod()->getAnnotations() as $annotation) { |
|
94 | 3 | if (!$annotation instanceof Contract\Ensure) { |
|
95 | continue; |
||
96 | } |
||
97 | |||
98 | try { |
||
99 | 3 | $this->ensureContractSatisfied($object, $class->name, $args, $annotation); |
|
100 | 1 | } catch (DomainException $e) { |
|
101 | 3 | throw new ContractViolation($invocation, $annotation->value, $e->getPrevious()); |
|
102 | } |
||
103 | } |
||
104 | |||
105 | 2 | return $result; |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * Verifies invariants for contract class |
||
110 | * |
||
111 | * @Around("@within(PhpDeal\Annotation\Invariant) && execution(public **->*(*))") |
||
112 | * @param MethodInvocation $invocation |
||
113 | * |
||
114 | * @throws ContractViolation |
||
115 | * @return mixed |
||
116 | */ |
||
117 | 3 | public function invariantContract(MethodInvocation $invocation) |
|
118 | { |
||
119 | 3 | $object = $invocation->getThis(); |
|
120 | 3 | $args = $this->getMethodArguments($invocation); |
|
121 | 3 | $class = $invocation->getMethod()->getDeclaringClass(); |
|
122 | 3 | if ($class->isCloneable()) { |
|
123 | 3 | $args['__old'] = clone $object; |
|
124 | } |
||
125 | |||
126 | 3 | $result = $invocation->proceed(); |
|
127 | 3 | $args['__result'] = $result; |
|
128 | |||
129 | 3 | foreach ($this->reader->getClassAnnotations($class) as $annotation) { |
|
130 | 3 | if (!$annotation instanceof Contract\Invariant) { |
|
131 | 2 | continue; |
|
132 | } |
||
133 | |||
134 | try { |
||
135 | 3 | $this->ensureContractSatisfied($object, $class->name, $args, $annotation); |
|
136 | 2 | } catch (DomainException $e) { |
|
137 | 3 | throw new ContractViolation($invocation, $annotation->value, $e->getPrevious()); |
|
138 | } |
||
139 | } |
||
140 | |||
141 | 2 | return $result; |
|
142 | } |
||
143 | |||
144 | /** |
||
145 | * Returns a result of contract verification |
||
146 | * |
||
147 | * @param object|string $instance Invocation instance or string for static class |
||
148 | * @param string $scope Scope of method |
||
149 | * @param array $args List of arguments for the method |
||
150 | * @param Annotation $annotation Contract annotation |
||
151 | * @throws DomainException |
||
152 | */ |
||
153 | 14 | private function ensureContractSatisfied($instance, $scope, array $args, $annotation) |
|
154 | { |
||
155 | 14 | static $invoker = null; |
|
156 | 14 | if (!$invoker) { |
|
157 | $invoker = function () { |
||
158 | 14 | extract(func_get_arg(0)); |
|
|
|||
159 | |||
160 | 14 | return eval('return ' . func_get_arg(1) . '; ?>'); |
|
161 | 1 | }; |
|
162 | } |
||
163 | 14 | $instance = is_object($instance) ? $instance : null; |
|
164 | |||
165 | try { |
||
166 | 14 | $invocationResult = $invoker->bindTo($instance, $scope)->__invoke($args, $annotation->value); |
|
167 | 4 | } catch (\Exception $e) { |
|
168 | 4 | throw new DomainException("", 0, $e); |
|
169 | } |
||
170 | |||
171 | // we accept as a result only true or null |
||
172 | // null may be a result of assertions from beberlei/assert which passed |
||
173 | 10 | if ($invocationResult !== null && $invocationResult !== true) { |
|
174 | 4 | throw new DomainException(); |
|
175 | } |
||
176 | 7 | } |
|
177 | |||
178 | /** |
||
179 | * Returns an associative list of arguments for the method invocation |
||
180 | * |
||
181 | * @param MethodInvocation $invocation |
||
182 | * |
||
183 | * @return array |
||
184 | */ |
||
185 | 14 | private function getMethodArguments(MethodInvocation $invocation) |
|
195 | } |
||
196 |