18
18
use Go \Lang \Annotation \Before ;
19
19
use PhpDeal \Annotation as Contract ;
20
20
use PhpDeal \Exception \ContractViolation ;
21
+ use DomainException ;
21
22
22
23
/**
23
24
*/
@@ -60,9 +61,11 @@ public function preConditionContract(MethodInvocation $invocation)
60
61
continue ;
61
62
}
62
63
63
- if (!$ this ->isContractSatisfied ($ object , $ scope , $ args , $ annotation )) {
64
- throw new ContractViolation ($ invocation , $ annotation ->value );
65
- };
64
+ try {
65
+ $ this ->ensureContractSatisfied ($ object , $ scope , $ args , $ annotation );
66
+ } catch (DomainException $ e ) {
67
+ throw new ContractViolation ($ invocation , $ annotation ->value , $ e ->getPrevious ());
68
+ }
66
69
}
67
70
}
68
71
@@ -92,9 +95,11 @@ public function postConditionContract(MethodInvocation $invocation)
92
95
continue ;
93
96
}
94
97
95
- if (!$ this ->isContractSatisfied ($ object , $ class ->name , $ args , $ annotation )) {
96
- throw new ContractViolation ($ invocation , $ annotation ->value );
97
- };
98
+ try {
99
+ $ this ->ensureContractSatisfied ($ object , $ class ->name , $ args , $ annotation );
100
+ } catch (DomainException $ e ) {
101
+ throw new ContractViolation ($ invocation , $ annotation ->value , $ e ->getPrevious ());
102
+ }
98
103
}
99
104
100
105
return $ result ;
@@ -126,9 +131,11 @@ public function invariantContract(MethodInvocation $invocation)
126
131
continue ;
127
132
}
128
133
129
- if (!$ this ->isContractSatisfied ($ object , $ class ->name , $ args , $ annotation )) {
130
- throw new ContractViolation ($ invocation , $ annotation ->value );
131
- };
134
+ try {
135
+ $ this ->ensureContractSatisfied ($ object , $ class ->name , $ args , $ annotation );
136
+ } catch (DomainException $ e ) {
137
+ throw new ContractViolation ($ invocation , $ annotation ->value , $ e ->getPrevious ());
138
+ }
132
139
}
133
140
134
141
return $ result ;
@@ -141,10 +148,9 @@ public function invariantContract(MethodInvocation $invocation)
141
148
* @param string $scope Scope of method
142
149
* @param array $args List of arguments for the method
143
150
* @param Annotation $annotation Contract annotation
144
- *
145
- * @return mixed
151
+ * @throws DomainException
146
152
*/
147
- private function isContractSatisfied ($ instance , $ scope , array $ args , $ annotation )
153
+ private function ensureContractSatisfied ($ instance , $ scope , array $ args , $ annotation )
148
154
{
149
155
static $ invoker = null ;
150
156
if (!$ invoker ) {
@@ -156,7 +162,17 @@ private function isContractSatisfied($instance, $scope, array $args, $annotation
156
162
}
157
163
$ instance = is_object ($ instance ) ? $ instance : null ;
158
164
159
- return $ invoker ->bindTo ($ instance , $ scope )->__invoke ($ args , $ annotation ->value );
165
+ try {
166
+ $ invocationResult = $ invoker ->bindTo ($ instance , $ scope )->__invoke ($ args , $ annotation ->value );
167
+ } catch (\Exception $ e ) {
168
+ 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
+ if ($ invocationResult !== null && $ invocationResult !== true ) {
174
+ throw new DomainException ();
175
+ }
160
176
}
161
177
162
178
/**
0 commit comments