@@ -2638,6 +2638,7 @@ abstract class BaseCodeFormatter {
26382638 'AlignGroupDoubleArrow ' => false ,
26392639 'AlignDoubleArrow ' => false ,
26402640 'AlignEquals ' => false ,
2641+ 'AlignSuperEquals ' => false ,
26412642 'AlignConstVisibilityEquals ' => false ,
26422643
26432644 'ReindentSwitchBlocks ' => false ,
@@ -7490,6 +7491,172 @@ public function getExample() {
74907491 }
74917492 }
74927493
7494+ class AlignSuperEquals extends AdditionalPass {
7495+ const ALIGNABLE_EQUAL = "\x2 EQUAL%d \x3" ;
7496+ const ALIGNABLE_EQUAL_SPACE = "\x2 EQUAL_SPACE \x3" ;
7497+
7498+ const OPEN_TAG = "<?php /* \x2 EQUAL OPEN TAG \x3*/ " ;
7499+
7500+ public function candidate ($ source , $ foundTokens ) {
7501+ return true ;
7502+ }
7503+
7504+ public function format ($ source ) {
7505+ $ this ->tkns = token_get_all ($ source );
7506+ $ this ->code = '' ;
7507+
7508+ $ blockCounter = 0 ;
7509+ $ blockCountEquals = array (0 => 0 );
7510+
7511+ while (list ($ index , $ token ) = $ this ->each ($ this ->tkns )) {
7512+ list ($ id , $ text ) = $ this ->getToken ($ token );
7513+ $ this ->ptr = $ index ;
7514+ switch ($ id ) {
7515+ case T_WHITESPACE :
7516+ if ($ this ->hasLn ($ text ) && substr_count ($ text , $ this ->newLine ) >= 2 ) {
7517+ $ blockCounter ++;
7518+ $ blockCountEquals [$ blockCounter ] = 0 ;
7519+ }
7520+ break ;
7521+ case ST_EQUAL :
7522+ case T_PLUS_EQUAL :
7523+ case T_MINUS_EQUAL :
7524+ case T_MUL_EQUAL :
7525+ case T_POW_EQUAL :
7526+ case T_DIV_EQUAL :
7527+ case T_CONCAT_EQUAL :
7528+ case T_MOD_EQUAL :
7529+ case T_AND_EQUAL :
7530+ case T_OR_EQUAL :
7531+ case T_XOR_EQUAL :
7532+ case T_SL_EQUAL :
7533+ case T_SR_EQUAL :
7534+ case T_IS_EQUAL :
7535+ case T_IS_NOT_EQUAL :
7536+ case T_IS_IDENTICAL :
7537+ case T_IS_NOT_IDENTICAL :
7538+ case T_IS_SMALLER_OR_EQUAL :
7539+ case T_IS_GREATER_OR_EQUAL :
7540+ $ len = strlen ($ text );
7541+ if ($ len > $ blockCountEquals [$ blockCounter ]) {
7542+ $ blockCountEquals [$ blockCounter ] = $ len ;
7543+ }
7544+ break ;
7545+ }
7546+ }
7547+
7548+ reset ($ this ->tkns );
7549+
7550+ $ parenCount = 0 ;
7551+ $ bracketCount = 0 ;
7552+ $ contextCounter = 0 ;
7553+ $ blockCounter = 0 ;
7554+
7555+ while (list ($ index , $ token ) = $ this ->each ($ this ->tkns )) {
7556+ list ($ id , $ text ) = $ this ->getToken ($ token );
7557+ $ this ->ptr = $ index ;
7558+ switch ($ id ) {
7559+ case T_WHITESPACE :
7560+ if ($ this ->hasLn ($ text ) && substr_count ($ text , $ this ->newLine ) >= 2 ) {
7561+ $ blockCounter ++;
7562+ }
7563+ $ this ->appendCode ($ text );
7564+ break ;
7565+ case T_FUNCTION :
7566+ ++$ contextCounter ;
7567+ $ this ->appendCode ($ text );
7568+ break ;
7569+
7570+ case ST_CURLY_OPEN :
7571+ $ this ->appendCode ($ text );
7572+ $ block = $ this ->walkAndAccumulateCurlyBlock ($ this ->tkns );
7573+ $ aligner = new self ();
7574+ $ this ->appendCode (
7575+ str_replace (self ::OPEN_TAG , '' , $ aligner ->format (self ::OPEN_TAG . $ block ))
7576+ );
7577+ break ;
7578+
7579+ case ST_PARENTHESES_OPEN :
7580+ ++$ parenCount ;
7581+ $ this ->appendCode ($ text );
7582+ break ;
7583+ case ST_PARENTHESES_CLOSE :
7584+ --$ parenCount ;
7585+ $ this ->appendCode ($ text );
7586+ break ;
7587+ case ST_BRACKET_OPEN :
7588+ ++$ bracketCount ;
7589+ $ this ->appendCode ($ text );
7590+ break ;
7591+ case ST_BRACKET_CLOSE :
7592+ --$ bracketCount ;
7593+ $ this ->appendCode ($ text );
7594+ break ;
7595+ case ST_EQUAL :
7596+ case T_PLUS_EQUAL :
7597+ case T_MINUS_EQUAL :
7598+ case T_MUL_EQUAL :
7599+ case T_POW_EQUAL :
7600+ case T_DIV_EQUAL :
7601+ case T_CONCAT_EQUAL :
7602+ case T_MOD_EQUAL :
7603+ case T_AND_EQUAL :
7604+ case T_OR_EQUAL :
7605+ case T_XOR_EQUAL :
7606+ case T_SL_EQUAL :
7607+ case T_SR_EQUAL :
7608+ case T_IS_EQUAL :
7609+ case T_IS_NOT_EQUAL :
7610+ case T_IS_IDENTICAL :
7611+ case T_IS_NOT_IDENTICAL :
7612+ case T_IS_SMALLER_OR_EQUAL :
7613+ case T_IS_GREATER_OR_EQUAL :
7614+ if (!$ parenCount && !$ bracketCount ) {
7615+ $ this ->appendCode (sprintf (self ::ALIGNABLE_EQUAL , $ contextCounter ));
7616+ if ($ blockCountEquals [$ blockCounter ] > 1 ) {
7617+ $ extra_chars = $ blockCountEquals [$ blockCounter ] - strlen ($ text );
7618+ if ($ extra_chars > 0 ) {
7619+ $ this ->appendCode (str_repeat (self ::ALIGNABLE_EQUAL_SPACE , $ extra_chars ));
7620+ }
7621+ }
7622+ }
7623+ $ this ->appendCode ($ text );
7624+ break ;
7625+ default :
7626+ $ this ->appendCode ($ text );
7627+ break ;
7628+ }
7629+ }
7630+
7631+ $ this ->alignPlaceholders (self ::ALIGNABLE_EQUAL , $ contextCounter );
7632+
7633+ $ this ->code = str_replace (self ::ALIGNABLE_EQUAL_SPACE , $ this ->getSpace (), $ this ->code );
7634+
7635+ return $ this ->code ;
7636+ }
7637+
7638+ public function getDescription () {
7639+ return 'Vertically align "=", ".=", "&=", ">>=", etc. ' ;
7640+ }
7641+
7642+ public function getExample () {
7643+ return <<<'EOT'
7644+ <?php
7645+ $a .= 1;
7646+ $bb = 22;
7647+ $ccc &= 333;
7648+ $d <<= 1;
7649+
7650+ $a .= 1;
7651+ $bb = 22;
7652+ $ccc &= 333;
7653+ $d <<= 1;
7654+
7655+ ?>
7656+ EOT;
7657+ }
7658+ }
7659+
74937660 final class AlignGroupDoubleArrow extends AlignDoubleArrow {
74947661 public function format ($ source ) {
74957662 $ this ->tkns = token_get_all ($ source );
0 commit comments