@@ -50,14 +50,12 @@ function __()
5050 *
5151 * @return \Closure
5252 *
53- * @throws \TomPHP\Transform\Exception\ InvalidArgumentException
54- * @throws \TomPHP\Transform\Exception\ UnexpectedValueException
53+ * @throws InvalidArgumentException
54+ * @throws UnexpectedValueException
5555 */
5656function callMethod ($ methodName , ...$ args )
5757{
58- if (!is_string ($ methodName )) {
59- throw InvalidArgumentException::expectedString ('methodName ' , $ methodName );
60- }
58+ assertArgumentIsString ('methodName ' , $ methodName );
6159
6260 return function ($ object ) use ($ methodName , $ args ) {
6361 if (!is_object ($ object )) {
@@ -81,24 +79,20 @@ function callMethod($methodName, ...$args)
8179 *
8280 * @return \Closure
8381 *
84- * @throws \TomPHP\Transform\Exception\ InvalidArgumentException
85- * @throws \TomPHP\Transform\Exception\ UnexpectedValueException
82+ * @throws InvalidArgumentException
83+ * @throws UnexpectedValueException
8684 */
8785function callStatic ($ methodName , ...$ args )
8886{
89- if (!is_string ($ methodName )) {
90- throw InvalidArgumentException::expectedString ('methodName ' , $ methodName );
91- }
87+ assertArgumentIsString ('methodName ' , $ methodName );
9288
9389 return function ($ objectOrClass ) use ($ methodName , $ args ) {
9490
9591 if (is_object ($ objectOrClass )) {
9692 $ reflectedClass = new \ReflectionObject ($ objectOrClass );
9793 $ subjectObject = $ objectOrClass ;
9894 } else {
99- if (!class_exists ($ objectOrClass )) {
100- throw InvalidArgumentException::expectedValidClassName ($ objectOrClass );
101- }
95+ assertClassExists ($ objectOrClass );
10296
10397 $ reflectedClass = new \ReflectionClass ($ objectOrClass );
10498 $ subjectObject = null ;
@@ -191,12 +185,7 @@ function getProperty($name)
191185function argumentTo (callable $ callable , array $ arguments = [__])
192186{
193187 return function ($ value ) use ($ callable , $ arguments ) {
194- $ arguments = array_map (
195- function ($ arg ) use ($ value ) {
196- return $ arg === __ ? $ value : $ arg ;
197- },
198- $ arguments
199- );
188+ $ arguments = substituteArguments ($ arguments , $ value );
200189
201190 return $ callable (...$ arguments );
202191 };
@@ -209,19 +198,15 @@ function ($arg) use ($value) {
209198 *
210199 * @return \Closure
211200 *
212- * @throws \TomPHP\Transform\Exception\ InvalidArgumentException
213- * @throws \TomPHP\Transform\Exception\ UnexpectedValueException
201+ * @throws InvalidArgumentException
202+ * @throws UnexpectedValueException
214203 */
215204function prepend ($ prefix )
216205{
217- if (!is_string ($ prefix )) {
218- throw InvalidArgumentException::expectedString ('prefix ' , $ prefix );
219- }
206+ assertArgumentIsString ('prefix ' , $ prefix );
220207
221208 return function ($ value ) use ($ prefix ) {
222- if (!is_scalar ($ value ) && !(is_object ($ value ) && method_exists ($ value , '__toString ' ))) {
223- throw UnexpectedValueException::expectedString ('value ' , $ value );
224- }
209+ assertArgumentIsStringable ('value ' , $ value );
225210
226211 return $ prefix .$ value ;
227212 };
@@ -234,18 +219,14 @@ function prepend($prefix)
234219 *
235220 * @return \Closure
236221 *
237- * @throws \TomPHP\Transform\Exception\ InvalidArgumentException
222+ * @throws InvalidArgumentException
238223 */
239224function append ($ suffix )
240225{
241- if (!is_string ($ suffix )) {
242- throw InvalidArgumentException::expectedString ('suffix ' , $ suffix );
243- }
226+ assertArgumentIsString ('suffix ' , $ suffix );
244227
245228 return function ($ value ) use ($ suffix ) {
246- if (!is_scalar ($ value ) && !(is_object ($ value ) && method_exists ($ value , '__toString ' ))) {
247- throw UnexpectedValueException::expectedString ('value ' , $ value );
248- }
229+ assertArgumentIsStringable ('value ' , $ value );
249230
250231 return $ value .$ suffix ;
251232 };
@@ -277,24 +258,15 @@ function concat(...$arguments)
277258 *
278259 * @return \Closure
279260 *
280- * @throws \TomPHP\Transform\Exception\ InvalidArgumentException
261+ * @throws InvalidArgumentException
281262 */
282263function newInstance ($ className , array $ arguments = [__])
283264{
284- if (!is_string ($ className )) {
285- throw InvalidArgumentException::expectedString ('className ' , $ className );
286- }
287- if (!class_exists ($ className )) {
288- throw InvalidArgumentException::expectedValidClassName ($ className );
289- }
265+ assertArgumentIsString ('className ' , $ className );
266+ assertClassExists ($ className );
290267
291268 return function ($ value ) use ($ className , $ arguments ) {
292- $ arguments = array_map (
293- function ($ arg ) use ($ value ) {
294- return $ arg === __ ? $ value : $ arg ;
295- },
296- $ arguments
297- );
269+ $ arguments = substituteArguments ($ arguments , $ value );
298270
299271 return new $ className (...$ arguments );
300272 };
@@ -317,3 +289,47 @@ function ($arg) use ($value) {
317289 $ arguments
318290 );
319291}
292+
293+ /**
294+ * @internal
295+ *
296+ * @param string $name
297+ * @param mixed $value
298+ *
299+ * @throws InvalidArgumentException
300+ */
301+ function assertArgumentIsString ($ name , $ value )
302+ {
303+ if (!is_string ($ value )) {
304+ throw InvalidArgumentException::expectedString ($ name , $ value );
305+ }
306+ }
307+
308+ /**
309+ * @internal
310+ *
311+ * @param string $name
312+ * @param mixed $value
313+ *
314+ * @throws UnexpectedValueException
315+ */
316+ function assertArgumentIsStringable ($ name , $ value )
317+ {
318+ if (!is_scalar ($ value ) && !(is_object ($ value ) && method_exists ($ value , '__toString ' ))) {
319+ throw UnexpectedValueException::expectedString ($ name , $ value );
320+ }
321+ }
322+
323+ /**
324+ * @internal
325+ *
326+ * @param string $className
327+ *
328+ * @throws InvalidArgumentException
329+ */
330+ function assertClassExists ($ className )
331+ {
332+ if (!class_exists ($ className )) {
333+ throw InvalidArgumentException::expectedValidClassName ($ className );
334+ }
335+ }
0 commit comments