@@ -435,14 +435,21 @@ private function compareStatementsInNamespace(array $oldStmts, array $newStmts,
435
435
throw new \LogicException ('Classname changed ' );
436
436
}
437
437
$ newMethods = array_filter ($ new ->stmts , fn (Node \Stmt $ stmt ) => $ stmt instanceof Node \Stmt \ClassMethod);
438
- [$ untouchedStmts , $ oldMethodsStmt ] = $ this ->filterStatementsByVersion ($ old ->stmts , $ updateFrom );
438
+ $ newConstants = array_filter ($ new ->stmts , fn (Node \Stmt $ stmt ) => $ stmt instanceof Node \Stmt \ClassConst);
439
+ [$ untouchedStmts , $ oldStmts ] = $ this ->filterStatementsByVersion ($ old ->stmts , $ updateFrom );
439
440
$ oldMethods = [];
440
- foreach ($ oldMethodsStmt as $ stmt ) {
441
- if (!$ stmt instanceof Node \Stmt \ClassMethod) {
441
+ $ oldConstants = [];
442
+ foreach ($ oldStmts as $ stmt ) {
443
+ if ($ stmt instanceof Node \Stmt \ClassMethod) {
444
+ $ oldMethods [$ stmt ->name ->toLowerString ()] = $ stmt ;
442
445
continue ;
443
446
}
444
447
445
- $ oldMethods [$ stmt ->name ->toLowerString ()] = $ stmt ;
448
+ if ($ stmt instanceof Node \Stmt \ClassConst) {
449
+ $ names = array_map (static fn (Node \Const_ $ const ) => $ const ->name ->toString (), $ stmt ->consts );
450
+ $ oldConstants [implode (', ' , $ names )] = $ stmt ;
451
+ continue ;
452
+ }
446
453
}
447
454
448
455
if ($ old ->stmts !== null ) {
@@ -467,6 +474,26 @@ private function compareStatementsInNamespace(array $oldStmts, array $newStmts,
467
474
468
475
// todo has a method been removed?
469
476
477
+ foreach ($ newConstants as $ stmt ) {
478
+ $ namesKey = implode (', ' , array_map (static fn (Node \Const_ $ const ) => $ const ->name ->toString (), $ stmt ->consts ));
479
+ if (!array_key_exists ($ namesKey , $ oldConstants )) {
480
+ $ stmt ->attrGroups [] = new Node \AttributeGroup ([
481
+ new Node \Attribute (
482
+ new Node \Name \FullyQualified ('Since ' ),
483
+ [new Node \Arg (new Node \Scalar \String_ ($ updateTo ))],
484
+ ),
485
+ ]);
486
+ $ newStmtsToSet [] = $ stmt ;
487
+ continue ;
488
+ }
489
+
490
+ foreach ($ this ->compareConstants ($ oldConstants [$ namesKey ], $ stmt , $ updateTo ) as $ constantStmt ) {
491
+ $ newStmtsToSet [] = $ constantStmt ;
492
+ }
493
+ }
494
+
495
+ // todo has a constant been removed?
496
+
470
497
$ old ->stmts = $ newStmtsToSet ;
471
498
}
472
499
@@ -508,49 +535,49 @@ private function compareFunctions(Node\FunctionLike $old, Node\FunctionLike $new
508
535
}
509
536
}
510
537
if ($ this ->printType ($ old ->getReturnType ()) !== $ this ->printType ($ new ->getReturnType ())) {
511
- return $ this ->functionDiff ($ old , $ new , $ updateTo );
538
+ return $ this ->stmtDiff ($ old , $ new , $ updateTo );
512
539
}
513
540
if ($ old ->returnsByRef () !== $ new ->returnsByRef ()) {
514
- return $ this ->functionDiff ($ old , $ new , $ updateTo );
541
+ return $ this ->stmtDiff ($ old , $ new , $ updateTo );
515
542
}
516
543
if (count ($ old ->getParams ()) !== count ($ new ->getParams ())) {
517
- return $ this ->functionDiff ($ old , $ new , $ updateTo );
544
+ return $ this ->stmtDiff ($ old , $ new , $ updateTo );
518
545
}
519
546
520
547
foreach ($ old ->getParams () as $ i => $ oldParam ) {
521
548
$ newParam = $ new ->getParams ()[$ i ];
522
549
if ($ this ->printType ($ oldParam ->type ) !== $ this ->printType ($ newParam ->type )) {
523
- return $ this ->functionDiff ($ old , $ new , $ updateTo );
550
+ return $ this ->stmtDiff ($ old , $ new , $ updateTo );
524
551
}
525
552
if ($ oldParam ->byRef !== $ newParam ->byRef ) {
526
- return $ this ->functionDiff ($ old , $ new , $ updateTo );
553
+ return $ this ->stmtDiff ($ old , $ new , $ updateTo );
527
554
}
528
555
if ($ oldParam ->variadic !== $ newParam ->variadic ) {
529
- return $ this ->functionDiff ($ old , $ new , $ updateTo );
556
+ return $ this ->stmtDiff ($ old , $ new , $ updateTo );
530
557
}
531
558
assert ($ oldParam ->var instanceof Node \Expr \Variable);
532
559
assert (is_string ($ oldParam ->var ->name ));
533
560
assert ($ newParam ->var instanceof Node \Expr \Variable);
534
561
assert (is_string ($ newParam ->var ->name ));
535
562
if ($ oldParam ->var ->name !== $ newParam ->var ->name ) {
536
- return $ this ->functionDiff ($ old , $ new , $ updateTo );
563
+ return $ this ->stmtDiff ($ old , $ new , $ updateTo );
537
564
}
538
565
if (is_null ($ oldParam ->default ) !== is_null ($ newParam ->default )) {
539
- return $ this ->functionDiff ($ old , $ new , $ updateTo );
566
+ return $ this ->stmtDiff ($ old , $ new , $ updateTo );
540
567
}
541
568
if ($ oldParam ->default !== null && $ newParam ->default !== null ) {
542
569
if ($ this ->printer ->prettyPrintExpr ($ oldParam ->default ) !== $ this ->printer ->prettyPrintExpr ($ newParam ->default )) {
543
- return $ this ->functionDiff ($ old , $ new , $ updateTo );
570
+ return $ this ->stmtDiff ($ old , $ new , $ updateTo );
544
571
}
545
572
}
546
573
if ($ oldParam ->flags !== $ newParam ->flags ) {
547
- return $ this ->functionDiff ($ old , $ new , $ updateTo );
574
+ return $ this ->stmtDiff ($ old , $ new , $ updateTo );
548
575
}
549
576
}
550
577
551
578
if ($ old instanceof Node \Stmt \ClassMethod && $ new instanceof Node \Stmt \ClassMethod) {
552
579
if ($ old ->flags !== $ new ->flags ) {
553
- return $ this ->functionDiff ($ old , $ new , $ updateTo );
580
+ return $ this ->stmtDiff ($ old , $ new , $ updateTo );
554
581
}
555
582
}
556
583
@@ -596,19 +623,19 @@ private function compareFunctions(Node\FunctionLike $old, Node\FunctionLike $new
596
623
}
597
624
598
625
if ($ oldPhpDocParameters !== $ newPhpDocParameters ) {
599
- return $ this ->functionDiff ($ old , $ new , $ updateTo );
626
+ return $ this ->stmtDiff ($ old , $ new , $ updateTo );
600
627
}
601
628
602
629
return [$ old ];
603
630
}
604
631
605
632
/**
606
- * @template T of Node\FunctionLike
633
+ * @template T of Node
607
634
* @param T $old
608
635
* @param T $new
609
636
* @return T[]
610
637
*/
611
- private function functionDiff (Node \ FunctionLike $ old , Node \ FunctionLike $ new , string $ updateTo ): array
638
+ private function stmtDiff (Node $ old , Node $ new , string $ updateTo ): array
612
639
{
613
640
$ args = [new Node \Arg (new Node \Scalar \String_ ($ updateTo ))];
614
641
$ old = clone $ old ;
@@ -632,6 +659,52 @@ private function functionDiff(Node\FunctionLike $old, Node\FunctionLike $new, st
632
659
return [$ old , $ new ];
633
660
}
634
661
662
+ /**
663
+ * @return Node\Stmt\ClassConst[]
664
+ */
665
+ private function compareConstants (Node \Stmt \ClassConst $ old , Node \Stmt \ClassConst $ new , string $ updateTo ): array
666
+ {
667
+ if ($ old ->getDocComment () !== $ new ->getDocComment ()) {
668
+ return $ this ->stmtDiff ($ old , $ new , $ updateTo );
669
+ }
670
+ if ($ old ->flags !== $ new ->flags ) {
671
+ return $ this ->stmtDiff ($ old , $ new , $ updateTo );
672
+ }
673
+
674
+ if (count ($ old ->consts ) !== count ($ new ->consts )) {
675
+ return $ this ->stmtDiff ($ old , $ new , $ updateTo );
676
+ }
677
+
678
+ foreach ($ new ->consts as $ i => $ newConst ) {
679
+ $ oldConst = $ old ->consts [$ i ];
680
+ if ($ oldConst ->name ->toString () !== $ newConst ->name ->toString ()) {
681
+ return $ this ->stmtDiff ($ old , $ new , $ updateTo );
682
+ }
683
+
684
+ $ oldValue = $ this ->printer ->prettyPrintExpr ($ oldConst ->value );
685
+ $ newValue = $ this ->printer ->prettyPrintExpr ($ newConst ->value );
686
+ if ($ oldValue !== $ newValue ) {
687
+ return $ this ->stmtDiff ($ old , $ new , $ updateTo );
688
+ }
689
+ }
690
+
691
+ $ oldType = $ old ->type ;
692
+ $ newType = $ new ->type ;
693
+ if ($ oldType !== null ) {
694
+ if ($ newType === null ) {
695
+ return $ this ->stmtDiff ($ old , $ new , $ updateTo );
696
+ }
697
+
698
+ if ($ this ->printType ($ oldType ) !== $ this ->printType ($ newType )) {
699
+ return $ this ->stmtDiff ($ old , $ new , $ updateTo );
700
+ }
701
+ } elseif ($ newType !== null ) {
702
+ return $ this ->stmtDiff ($ old , $ new , $ updateTo );
703
+ }
704
+
705
+ return [$ old ];
706
+ }
707
+
635
708
/**
636
709
* @param Node\Identifier|Node\Name|Node\ComplexType|null $type
637
710
*/
0 commit comments