1
1
<?php
2
+ declare (strict_types=1 );
3
+
2
4
/**
3
5
* PHP Deal framework
4
6
*
5
- * @copyright Copyright 2014 , Lisachenko Alexander <[email protected] >
7
+ * @copyright Copyright 2019 , Lisachenko Alexander <[email protected] >
6
8
*
7
9
* This source file is subject to the license that is bundled
8
10
* with this source code in the file LICENSE.
@@ -36,22 +38,22 @@ public function __construct(Reader $reader)
36
38
*
37
39
* @param MethodInvocation $invocation
38
40
* @return array
41
+ * @throws \ReflectionException
39
42
*/
40
- protected function fetchMethodArguments (MethodInvocation $ invocation )
43
+ protected function fetchMethodArguments (MethodInvocation $ invocation ): array
41
44
{
42
45
$ result = [];
43
46
$ parameters = $ invocation ->getMethod ()->getParameters ();
44
47
$ argumentValues = $ invocation ->getArguments ();
45
48
46
49
// Number of arguments can be less than number of parameters because of default values
47
50
foreach ($ parameters as $ parameterIndex => $ reflectionParameter ) {
48
- $ hasArgumentValue = array_key_exists ($ parameterIndex , $ argumentValues );
51
+ $ hasArgumentValue = \ array_key_exists ($ parameterIndex , $ argumentValues );
49
52
$ argumentValue = $ hasArgumentValue ? $ argumentValues [$ parameterIndex ] : null ;
50
53
if (!$ hasArgumentValue && $ reflectionParameter ->isDefaultValueAvailable ()) {
51
54
$ argumentValue = $ reflectionParameter ->getDefaultValue ();
52
55
}
53
56
$ result [$ reflectionParameter ->name ] = $ argumentValue ;
54
-
55
57
}
56
58
57
59
return $ result ;
@@ -69,28 +71,34 @@ protected function fetchMethodArguments(MethodInvocation $invocation)
69
71
* @throws DomainException
70
72
* @throws ContractViolation
71
73
*/
72
- protected function ensureContracts (MethodInvocation $ invocation , array $ contracts , $ instance , $ scope , array $ args )
73
- {
74
+ protected function ensureContracts (
75
+ MethodInvocation $ invocation ,
76
+ array $ contracts ,
77
+ $ instance ,
78
+ string $ scope ,
79
+ array $ args
80
+ ): void {
74
81
static $ invoker = null ;
75
82
if (!$ invoker ) {
76
83
$ invoker = function () {
77
- extract (func_get_arg (0 ));
84
+ $ args = \func_get_arg (0 );
85
+ \extract ($ args , EXTR_OVERWRITE );
78
86
79
- return eval ('return ' . func_get_arg (1 ) . '; ?> ' );
87
+ return eval ('return ' . \ func_get_arg (1 ) . '; ?> ' );
80
88
};
81
89
}
82
90
83
- $ instance = is_object ($ instance ) ? $ instance : null ;
91
+ $ instance = \ is_object ($ instance ) ? $ instance : null ;
84
92
$ boundInvoker = $ invoker ->bindTo ($ instance , $ scope );
85
93
86
94
foreach ($ contracts as $ contract ) {
87
95
$ contractExpression = $ contract ->value ;
88
96
try {
89
97
$ invocationResult = $ boundInvoker ->__invoke ($ args , $ contractExpression );
90
98
91
- // if ($invocationResult === false) {
92
- // throw new ContractViolation($invocation, $contractExpression);
93
- // }
99
+ if ($ invocationResult === false ) {
100
+ throw new ContractViolation ($ invocation , $ contractExpression );
101
+ }
94
102
95
103
// we accept as a result only true or null
96
104
// null may be a result of assertions from beberlei/assert which passed
@@ -99,7 +107,6 @@ protected function ensureContracts(MethodInvocation $invocation, array $contract
99
107
. ' only boolean or void can be returned ' ;
100
108
throw new DomainException ($ errorMessage );
101
109
}
102
-
103
110
} catch (\Error $ internalError ) {
104
111
// PHP-7 friendly interceptor for fatal errors
105
112
throw new ContractViolation ($ invocation , $ contractExpression , $ internalError );
0 commit comments