26
26
use PhpParser \Node \Expr \Cast ;
27
27
use PhpParser \Node \Expr \Cast \String_ ;
28
28
use PhpParser \Node \Expr \ClassConstFetch ;
29
+ use PhpParser \Node \Expr \ConstFetch ;
29
30
use PhpParser \Node \Expr \FuncCall ;
30
31
use PhpParser \Node \Expr \Match_ ;
31
32
use PhpParser \Node \Expr \MethodCall ;
@@ -183,6 +184,10 @@ public function refactor(Node $node): ?Node
183
184
return $ this ->refactorGetRandomInstance ($ node );
184
185
}
185
186
187
+ if ($ this ->isName ($ node ->name , 'hasValue ' )) {
188
+ return $ this ->refactorHasValue ($ node );
189
+ }
190
+
186
191
return $ this ->refactorMaybeMagicStaticCall ($ node );
187
192
}
188
193
@@ -321,9 +326,9 @@ protected function refactorFromKey(StaticCall $call): ?Node
321
326
}
322
327
323
328
/** @see Enum::getInstances() */
324
- protected function refactorGetInstances (StaticCall $ node ): ?StaticCall
329
+ protected function refactorGetInstances (StaticCall $ call ): ?StaticCall
325
330
{
326
- $ class = $ node ->class ;
331
+ $ class = $ call ->class ;
327
332
if ($ class instanceof Name) {
328
333
return new StaticCall ($ class , 'cases ' );
329
334
}
@@ -332,11 +337,11 @@ protected function refactorGetInstances(StaticCall $node): ?StaticCall
332
337
}
333
338
334
339
/** @see Enum::getKeys() */
335
- protected function refactorGetKeys (StaticCall $ node ): ?Node
340
+ protected function refactorGetKeys (StaticCall $ call ): ?Node
336
341
{
337
- $ class = $ node ->class ;
342
+ $ class = $ call ->class ;
338
343
if ($ class instanceof Name) {
339
- $ args = $ node ->args ;
344
+ $ args = $ call ->args ;
340
345
if ($ args === []) {
341
346
$ paramName = lcfirst ($ class ->getLast ());
342
347
$ paramVariable = new Variable ($ paramName );
@@ -364,11 +369,11 @@ protected function refactorGetKeys(StaticCall $node): ?Node
364
369
}
365
370
366
371
/** @see Enum::getValues() */
367
- protected function refactorGetValues (StaticCall $ node ): ?Node
372
+ protected function refactorGetValues (StaticCall $ call ): ?Node
368
373
{
369
- $ class = $ node ->class ;
374
+ $ class = $ call ->class ;
370
375
if ($ class instanceof Name) {
371
- $ args = $ node ->args ;
376
+ $ args = $ call ->args ;
372
377
if ($ args === []) {
373
378
$ paramName = lcfirst ($ class ->getLast ());
374
379
$ paramVariable = new Variable ($ paramName );
@@ -395,27 +400,72 @@ protected function refactorGetValues(StaticCall $node): ?Node
395
400
}
396
401
397
402
/** @see Enum::getRandomInstance() */
398
- protected function refactorGetRandomInstance (StaticCall $ staticCall ): ?Node
403
+ protected function refactorGetRandomInstance (StaticCall $ call ): ?Node
399
404
{
400
405
return new MethodCall (
401
406
new FuncCall (new Name ('fake ' )),
402
407
'randomElement ' ,
403
- [new Arg (new StaticCall ($ staticCall ->class , 'cases ' ))]
408
+ [new Arg (new StaticCall ($ call ->class , 'cases ' ))]
404
409
);
405
410
}
406
411
412
+ /** @see Enum::hasValue() */
413
+ protected function refactorHasValue (StaticCall $ call ): ?Node
414
+ {
415
+ $ class = $ call ->class ;
416
+ if ($ class instanceof Name) {
417
+ $ makeTryFromNotNull = function (Arg $ arg ) use ($ class ): NotIdentical {
418
+ $ tryFrom = new StaticCall (
419
+ $ class ,
420
+ 'tryFrom ' ,
421
+ [$ arg ]
422
+ );
423
+ $ null = new ConstFetch (new Name ('null ' ));
424
+
425
+ return new NotIdentical ($ tryFrom , $ null );
426
+ };
427
+
428
+ if ($ call ->isFirstClassCallable ()) {
429
+ $ valueVariable = new Variable ('value ' );
430
+
431
+ return new ArrowFunction ([
432
+ 'static ' => true ,
433
+ 'params ' => [new Param ($ valueVariable , null , 'mixed ' )],
434
+ 'returnType ' => 'bool ' ,
435
+ 'expr ' => $ makeTryFromNotNull (new Arg ($ valueVariable )),
436
+ ]);
437
+ }
438
+
439
+ $ args = $ call ->args ;
440
+ $ firstArg = $ args [0 ] ?? null ;
441
+ if ($ firstArg instanceof Arg) {
442
+ $ firstArgValue = $ firstArg ->value ;
443
+ if (
444
+ $ firstArgValue instanceof ClassConstFetch
445
+ && $ firstArgValue ->class ->toString () === $ class ->toString ()
446
+ ) {
447
+ return new ConstFetch (new Name ('true ' ));
448
+ }
449
+
450
+ return $ makeTryFromNotNull ($ firstArg );
451
+ }
452
+ }
453
+
454
+ return null ;
455
+ }
456
+
407
457
/**
408
458
* @see Enum::__callStatic()
409
459
* @see Enum::__call()
410
460
*/
411
- protected function refactorMaybeMagicStaticCall (StaticCall $ node ): ?Node
461
+ protected function refactorMaybeMagicStaticCall (StaticCall $ call ): ?Node
412
462
{
413
- $ name = $ node ->name ;
463
+ $ name = $ call ->name ;
414
464
if ($ name instanceof Expr) {
415
465
return null ;
416
466
}
417
467
418
- $ class = $ node ->class ;
468
+ $ class = $ call ->class ;
419
469
if ($ class instanceof Name) {
420
470
if ($ class ->isSpecialClassName ()) {
421
471
$ type = $ this ->getType ($ class );
@@ -473,11 +523,11 @@ protected function refactorIsOrIsNot(MethodCall|NullsafeMethodCall $call, bool $
473
523
* @see Enum::in()
474
524
* @see Enum::notIn()
475
525
*/
476
- protected function refactorInOrNotIn (MethodCall |NullsafeMethodCall $ node , bool $ in ): ?Node
526
+ protected function refactorInOrNotIn (MethodCall |NullsafeMethodCall $ call , bool $ in ): ?Node
477
527
{
478
- $ args = $ node ->args ;
528
+ $ args = $ call ->args ;
479
529
if (isset ($ args [0 ]) && $ args [0 ] instanceof Arg) {
480
- $ needle = new Arg ($ node ->var );
530
+ $ needle = new Arg ($ call ->var );
481
531
$ haystack = $ args [0 ];
482
532
483
533
$ haystackValue = $ haystack ->value ;
@@ -502,17 +552,17 @@ protected function refactorInOrNotIn(MethodCall|NullsafeMethodCall $node, bool $
502
552
}
503
553
504
554
/** @see Enum::__toString() */
505
- protected function refactorMagicToString (MethodCall |NullsafeMethodCall $ node ): Cast
555
+ protected function refactorMagicToString (MethodCall |NullsafeMethodCall $ call ): Cast
506
556
{
507
557
return new String_ (
508
- $ this ->createValueFetch ($ node ->var , $ node instanceof NullsafeMethodCall)
558
+ $ this ->createValueFetch ($ call ->var , $ call instanceof NullsafeMethodCall)
509
559
);
510
560
}
511
561
512
562
/** @see Enum::$key */
513
- protected function refactorKey (PropertyFetch $ node ): ?Node
563
+ protected function refactorKey (PropertyFetch $ fetch ): ?Node
514
564
{
515
- return new PropertyFetch ($ node ->var , 'name ' );
565
+ return new PropertyFetch ($ fetch ->var , 'name ' );
516
566
}
517
567
518
568
protected function refactorMatch (Match_ $ match ): ?Node
@@ -611,13 +661,13 @@ protected function refactorSwitch(Switch_ $switch): ?Node
611
661
return new Switch_ ($ cond , $ cases , $ switch ->getAttributes ());
612
662
}
613
663
614
- protected function refactorArrayItem (ArrayItem $ node ): ?Node
664
+ protected function refactorArrayItem (ArrayItem $ arrayItem ): ?Node
615
665
{
616
- $ key = $ node ->key ;
666
+ $ key = $ arrayItem ->key ;
617
667
$ convertedKey = $ this ->convertConstToValueFetch ($ key );
618
668
619
- $ value = $ node ->value ;
620
- $ hasAttribute = $ node ->hasAttribute (self ::COMPARED_AGAINST_ENUM_INSTANCE );
669
+ $ value = $ arrayItem ->value ;
670
+ $ hasAttribute = $ arrayItem ->hasAttribute (self ::COMPARED_AGAINST_ENUM_INSTANCE );
621
671
$ convertedValue = $ hasAttribute
622
672
? null
623
673
: $ this ->convertConstToValueFetch ($ value );
@@ -626,9 +676,9 @@ protected function refactorArrayItem(ArrayItem $node): ?Node
626
676
return new ArrayItem (
627
677
$ convertedValue ?? $ value ,
628
678
$ convertedKey ?? $ key ,
629
- $ node ->byRef ,
630
- $ node ->getAttributes (),
631
- $ node ->unpack ,
679
+ $ arrayItem ->byRef ,
680
+ $ arrayItem ->getAttributes (),
681
+ $ arrayItem ->unpack ,
632
682
);
633
683
}
634
684
0 commit comments