@@ -61,21 +61,103 @@ public function __construct(InvocationHandler $handler, Matcher $matcher, Config
61
61
}
62
62
63
63
/**
64
+ * @param Constraint|non-empty-string|PropertyHook $constraint
65
+ *
66
+ * @throws InvalidArgumentException
67
+ * @throws MethodCannotBeConfiguredException
68
+ * @throws MethodNameAlreadyConfiguredException
69
+ *
70
+ * @return $this
71
+ */
72
+ public function method (Constraint |PropertyHook |string $ constraint ): InvocationStubber
73
+ {
74
+ if ($ this ->matcher ->hasMethodNameRule ()) {
75
+ throw new MethodNameAlreadyConfiguredException ;
76
+ }
77
+
78
+ if ($ constraint instanceof PropertyHook) {
79
+ $ constraint = $ constraint ->asString ();
80
+ }
81
+
82
+ if (is_string ($ constraint )) {
83
+ $ this ->configurableMethodNames ??= array_flip (
84
+ array_map (
85
+ static fn (ConfigurableMethod $ configurable ) => strtolower ($ configurable ->name ()),
86
+ $ this ->configurableMethods ,
87
+ ),
88
+ );
89
+
90
+ if (!array_key_exists (strtolower ($ constraint ), $ this ->configurableMethodNames )) {
91
+ throw new MethodCannotBeConfiguredException ($ constraint );
92
+ }
93
+ }
94
+
95
+ $ this ->matcher ->setMethodNameRule (new Rule \MethodName ($ constraint ));
96
+
97
+ return $ this ;
98
+ }
99
+
100
+ /**
101
+ * @param non-empty-string $id
102
+ *
64
103
* @throws MatcherAlreadyRegisteredException
65
104
*
66
105
* @return $this
67
106
*/
68
- public function id (string $ id ): self
107
+ public function id (string $ id ): InvocationStubber
69
108
{
70
109
$ this ->invocationHandler ->registerMatcher ($ id , $ this ->matcher );
71
110
72
111
return $ this ;
73
112
}
74
113
75
114
/**
115
+ * @param non-empty-string $id
116
+ *
117
+ * @return $this
118
+ */
119
+ public function after (string $ id ): InvocationStubber
120
+ {
121
+ $ this ->matcher ->setAfterMatchBuilderId ($ id );
122
+
123
+ return $ this ;
124
+ }
125
+
126
+ /**
127
+ * @throws \PHPUnit\Framework\Exception
128
+ * @throws MethodNameNotConfiguredException
129
+ * @throws MethodParametersAlreadyConfiguredException
130
+ *
76
131
* @return $this
77
132
*/
78
- public function will (Stub $ stub ): self
133
+ public function with (mixed ...$ arguments ): InvocationStubber
134
+ {
135
+ $ this ->ensureParametersCanBeConfigured ();
136
+
137
+ $ this ->matcher ->setParametersRule (new Rule \Parameters ($ arguments ));
138
+
139
+ return $ this ;
140
+ }
141
+
142
+ /**
143
+ * @throws MethodNameNotConfiguredException
144
+ * @throws MethodParametersAlreadyConfiguredException
145
+ *
146
+ * @return $this
147
+ */
148
+ public function withAnyParameters (): InvocationStubber
149
+ {
150
+ $ this ->ensureParametersCanBeConfigured ();
151
+
152
+ $ this ->matcher ->setParametersRule (new Rule \AnyParameters );
153
+
154
+ return $ this ;
155
+ }
156
+
157
+ /**
158
+ * @return $this
159
+ */
160
+ public function will (Stub $ stub ): InvocationStubber
79
161
{
80
162
$ this ->matcher ->setStub ($ stub );
81
163
@@ -85,7 +167,7 @@ public function will(Stub $stub): self
85
167
/**
86
168
* @throws IncompatibleReturnValueException
87
169
*/
88
- public function willReturn (mixed $ value , mixed ...$ nextValues ): self
170
+ public function willReturn (mixed $ value , mixed ...$ nextValues ): InvocationStubber
89
171
{
90
172
if (count ($ nextValues ) === 0 ) {
91
173
$ this ->ensureTypeOfReturnValues ([$ value ]);
@@ -104,14 +186,14 @@ public function willReturn(mixed $value, mixed ...$nextValues): self
104
186
return $ this ->will ($ stub );
105
187
}
106
188
107
- public function willReturnReference (mixed &$ reference ): self
189
+ public function willReturnReference (mixed &$ reference ): InvocationStubber
108
190
{
109
191
$ stub = new ReturnReference ($ reference );
110
192
111
193
return $ this ->will ($ stub );
112
194
}
113
195
114
- public function willReturnMap (array $ valueMap ): self
196
+ public function willReturnMap (array $ valueMap ): InvocationStubber
115
197
{
116
198
$ method = $ this ->configuredMethod ();
117
199
@@ -156,117 +238,41 @@ public function willReturnMap(array $valueMap): self
156
238
return $ this ->will ($ stub );
157
239
}
158
240
159
- public function willReturnArgument (int $ argumentIndex ): self
241
+ public function willReturnArgument (int $ argumentIndex ): InvocationStubber
160
242
{
161
243
$ stub = new ReturnArgument ($ argumentIndex );
162
244
163
245
return $ this ->will ($ stub );
164
246
}
165
247
166
- public function willReturnCallback (callable $ callback ): self
248
+ public function willReturnCallback (callable $ callback ): InvocationStubber
167
249
{
168
250
$ stub = new ReturnCallback ($ callback );
169
251
170
252
return $ this ->will ($ stub );
171
253
}
172
254
173
- public function willReturnSelf (): self
255
+ public function willReturnSelf (): InvocationStubber
174
256
{
175
257
$ stub = new ReturnSelf ;
176
258
177
259
return $ this ->will ($ stub );
178
260
}
179
261
180
- public function willReturnOnConsecutiveCalls (mixed ...$ values ): self
262
+ public function willReturnOnConsecutiveCalls (mixed ...$ values ): InvocationStubber
181
263
{
182
264
$ stub = new ConsecutiveCalls ($ values );
183
265
184
266
return $ this ->will ($ stub );
185
267
}
186
268
187
- public function willThrowException (Throwable $ exception ): self
269
+ public function willThrowException (Throwable $ exception ): InvocationStubber
188
270
{
189
271
$ stub = new Exception ($ exception );
190
272
191
273
return $ this ->will ($ stub );
192
274
}
193
275
194
- /**
195
- * @return $this
196
- */
197
- public function after (string $ id ): self
198
- {
199
- $ this ->matcher ->setAfterMatchBuilderId ($ id );
200
-
201
- return $ this ;
202
- }
203
-
204
- /**
205
- * @throws \PHPUnit\Framework\Exception
206
- * @throws MethodNameNotConfiguredException
207
- * @throws MethodParametersAlreadyConfiguredException
208
- *
209
- * @return $this
210
- */
211
- public function with (mixed ...$ arguments ): self
212
- {
213
- $ this ->ensureParametersCanBeConfigured ();
214
-
215
- $ this ->matcher ->setParametersRule (new Rule \Parameters ($ arguments ));
216
-
217
- return $ this ;
218
- }
219
-
220
- /**
221
- * @throws MethodNameNotConfiguredException
222
- * @throws MethodParametersAlreadyConfiguredException
223
- *
224
- * @return $this
225
- */
226
- public function withAnyParameters (): self
227
- {
228
- $ this ->ensureParametersCanBeConfigured ();
229
-
230
- $ this ->matcher ->setParametersRule (new Rule \AnyParameters );
231
-
232
- return $ this ;
233
- }
234
-
235
- /**
236
- * @throws InvalidArgumentException
237
- * @throws MethodCannotBeConfiguredException
238
- * @throws MethodNameAlreadyConfiguredException
239
- *
240
- * @return $this
241
- */
242
- public function method (Constraint |PropertyHook |string $ constraint ): self
243
- {
244
- if ($ this ->matcher ->hasMethodNameRule ()) {
245
- throw new MethodNameAlreadyConfiguredException ;
246
- }
247
-
248
- if ($ constraint instanceof PropertyHook) {
249
- $ constraint = $ constraint ->asString ();
250
- }
251
-
252
- if (is_string ($ constraint )) {
253
- $ this ->configurableMethodNames ??= array_flip (
254
- array_map (
255
- static fn (ConfigurableMethod $ configurable ) => strtolower ($ configurable ->name ()),
256
- $ this ->configurableMethods ,
257
- ),
258
- );
259
-
260
- if (!array_key_exists (strtolower ($ constraint ), $ this ->configurableMethodNames )) {
261
- throw new MethodCannotBeConfiguredException ($ constraint );
262
- }
263
- }
264
-
265
- $ this ->matcher ->setMethodNameRule (new Rule \MethodName ($ constraint ));
266
-
267
- return $ this ;
268
- }
269
-
270
276
/**
271
277
* @throws MethodNameNotConfiguredException
272
278
* @throws MethodParametersAlreadyConfiguredException
0 commit comments