4
4
5
5
use Closure ;
6
6
use ReflectionObject ;
7
+ use function call_user_func ;
7
8
8
9
/**
9
10
*
@@ -53,22 +54,57 @@ public function __construct()
53
54
public function add (string $ name , mixed $ returnValue ): void
54
55
{
55
56
if (!($ returnValue instanceof Closure)) {
56
- $ returnValue= static fn () => $ returnValue ;
57
+ $ returnValue = static fn () => $ returnValue ;
57
58
}
58
59
$ this ->functions [$ name ] = $ returnValue ;
59
60
}
60
61
62
+ /**
63
+ * @noinspection PhpUnused
64
+ * @param string $name
65
+ * @param mixed $returnValue
66
+ * @return void
67
+ */
68
+ public function addIfNotDefined (string $ name , mixed $ returnValue ): void
69
+ {
70
+ if (!$ this ->has ($ name )) {
71
+ $ this ->add ($ name , $ returnValue );
72
+ }
73
+ }
74
+
75
+ /**
76
+ * @param string $name
77
+ * @return bool
78
+ */
79
+ public function has (string $ name ): bool
80
+ {
81
+ return isset ($ this ->functions [$ name ]);
82
+ }
83
+
61
84
/**
62
85
* @param string $name
63
86
* @param array $args
64
87
* @return mixed
65
88
*/
66
89
private function call (string $ name , array $ args ): mixed
67
90
{
68
- $ func = $ this ->functions [$ name ];
69
- $ returnValue = $ func (...$ args );
70
- $ this ->incrementCount ($ name );
71
- return $ returnValue ;
91
+ return $ this ->executeWithEffect (
92
+ call_user_func ($ this ->functions [$ name ], ...$ args ),
93
+ fn () => $ this ->incrementCount ($ name )
94
+ );
95
+ }
96
+
97
+ /**
98
+ * @param string $name
99
+ * @param mixed ...$args
100
+ * @return mixed
101
+ */
102
+ private function callGlobalFunction (string $ name , mixed ...$ args ): mixed
103
+ {
104
+ return $ this ->executeWithEffect (
105
+ $ name (...$ args ),
106
+ fn () => $ this ->incrementCount ($ name )
107
+ );
72
108
}
73
109
74
110
/**
@@ -112,10 +148,14 @@ private function generate(string $namespace): string
112
148
return $ code ;
113
149
}
114
150
115
- private function callGlobalFunction (string $ name , mixed ...$ args ): mixed
151
+ /**
152
+ * @param mixed $returnValue
153
+ * @param Closure $effect
154
+ * @return mixed
155
+ */
156
+ private function executeWithEffect (mixed $ returnValue , Closure $ effect ): mixed
116
157
{
117
- $ returnValue = $ name (...$ args );
118
- $ this ->incrementCountOutOfScope ($ name );
158
+ $ effect ();
119
159
return $ returnValue ;
120
160
}
121
161
@@ -139,8 +179,7 @@ public function scope(?string $namespace = null): void
139
179
*/
140
180
private function getNamespaceFromTrace (array $ trace ): string
141
181
{
142
- $ file = $ trace [0 ]['file ' ];
143
- if (preg_match ('/\bnamespace\s+([^;]+);/ ' , file_get_contents ($ file ), $ matches )) {
182
+ if (preg_match ('/\bnamespace\s+([^;]+);/ ' , file_get_contents ($ trace [0 ]['file ' ]), $ matches )) {
144
183
return trim ($ matches [1 ]);
145
184
}
146
185
throw new NamespaceNotFound ('the namespace keyword could not find ' );
@@ -183,6 +222,10 @@ public function getCalledCountOutScope(string $functionName): int
183
222
return $ this ->getCalledCount ($ functionName )['out_scope ' ];
184
223
}
185
224
225
+ /**
226
+ * @param string $functionName
227
+ * @return int
228
+ */
186
229
public function getTotalCount (string $ functionName ): int
187
230
{
188
231
$ calledCount = $ this ->getCalledCount ($ functionName );
@@ -222,16 +265,6 @@ public function runWithMock(object $object, Closure $closure): mixed
222
265
return $ result ;
223
266
}
224
267
225
- /**
226
- * @param string $name
227
- * @return void
228
- */
229
- private function incrementCountOutOfScope (string $ name ): void
230
- {
231
- $ this ->calledCount [$ name ][$ name ] ??= 0 ;
232
- ++$ this ->calledCount [$ name ]['out_scope ' ];
233
- }
234
-
235
268
/**
236
269
* @param string $method
237
270
* @param array $arguments
0 commit comments