@@ -70,54 +70,58 @@ public function register()
70
70
*/
71
71
public function process (File $ phpcsFile , $ stackPtr )
72
72
{
73
- // Work out which type of file this is for.
74
73
$ tokens = $ phpcsFile ->getTokens ();
75
- if ($ tokens [$ stackPtr ]['code ' ] === T_STRING_CONCAT ) {
76
- if ($ phpcsFile ->tokenizerType === 'JS ' ) {
77
- return ;
78
- }
79
- } else {
80
- if ($ phpcsFile ->tokenizerType === 'PHP ' ) {
81
- return ;
82
- }
74
+
75
+ if ($ tokens [$ stackPtr ]['code ' ] === T_STRING_CONCAT && $ phpcsFile ->tokenizerType === 'JS ' ) {
76
+ // JS uses T_PLUS for string concatenation, not T_STRING_CONCAT.
77
+ return ;
78
+ } else if ($ tokens [$ stackPtr ]['code ' ] === T_PLUS && $ phpcsFile ->tokenizerType === 'PHP ' ) {
79
+ // PHP uses T_STRING_CONCAT for string concatenation, not T_PLUS.
80
+ return ;
83
81
}
84
82
85
83
$ prev = $ phpcsFile ->findPrevious (T_WHITESPACE , ($ stackPtr - 1 ), null , true );
86
84
$ next = $ phpcsFile ->findNext (T_WHITESPACE , ($ stackPtr + 1 ), null , true );
87
- if ($ prev === false || $ next === false ) {
85
+ if ($ next === false ) {
86
+ return ;
87
+ }
88
+
89
+ if (isset (Tokens::$ stringTokens [$ tokens [$ prev ]['code ' ]]) === false
90
+ || isset (Tokens::$ stringTokens [$ tokens [$ next ]['code ' ]]) === false
91
+ ) {
92
+ // Bow out as at least one of the two tokens being concatenated is not a string.
88
93
return ;
89
94
}
90
95
91
- if (isset (Tokens::$ stringTokens [$ tokens [$ prev ]['code ' ]]) === true
92
- && isset (Tokens::$ stringTokens [$ tokens [$ next ]['code ' ]]) === true
96
+ if ($ tokens [$ prev ]['content ' ][0 ] !== $ tokens [$ next ]['content ' ][0 ]) {
97
+ // Bow out as the two strings are not of the same type.
98
+ return ;
99
+ }
100
+
101
+ // Before we throw an error for PHP, allow strings to be
102
+ // combined if they would have < and ? next to each other because
103
+ // this trick is sometimes required in PHP strings.
104
+ if ($ phpcsFile ->tokenizerType === 'PHP ' ) {
105
+ $ prevChar = substr ($ tokens [$ prev ]['content ' ], -2 , 1 );
106
+ $ nextChar = $ tokens [$ next ]['content ' ][1 ];
107
+ $ combined = $ prevChar .$ nextChar ;
108
+ if ($ combined === '? ' .'> ' || $ combined === '< ' .'? ' ) {
109
+ return ;
110
+ }
111
+ }
112
+
113
+ if ($ this ->allowMultiline === true
114
+ && $ tokens [$ prev ]['line ' ] !== $ tokens [$ next ]['line ' ]
93
115
) {
94
- if ($ tokens [$ prev ]['content ' ][0 ] === $ tokens [$ next ]['content ' ][0 ]) {
95
- // Before we throw an error for PHP, allow strings to be
96
- // combined if they would have < and ? next to each other because
97
- // this trick is sometimes required in PHP strings.
98
- if ($ phpcsFile ->tokenizerType === 'PHP ' ) {
99
- $ prevChar = substr ($ tokens [$ prev ]['content ' ], -2 , 1 );
100
- $ nextChar = $ tokens [$ next ]['content ' ][1 ];
101
- $ combined = $ prevChar .$ nextChar ;
102
- if ($ combined === '? ' .'> ' || $ combined === '< ' .'? ' ) {
103
- return ;
104
- }
105
- }
106
-
107
- if ($ this ->allowMultiline === true
108
- && $ tokens [$ prev ]['line ' ] !== $ tokens [$ next ]['line ' ]
109
- ) {
110
- return ;
111
- }
112
-
113
- $ error = 'String concat is not required here; use a single string instead ' ;
114
- if ($ this ->error === true ) {
115
- $ phpcsFile ->addError ($ error , $ stackPtr , 'Found ' );
116
- } else {
117
- $ phpcsFile ->addWarning ($ error , $ stackPtr , 'Found ' );
118
- }
119
- }//end if
120
- }//end if
116
+ return ;
117
+ }
118
+
119
+ $ error = 'String concat is not required here; use a single string instead ' ;
120
+ if ($ this ->error === true ) {
121
+ $ phpcsFile ->addError ($ error , $ stackPtr , 'Found ' );
122
+ } else {
123
+ $ phpcsFile ->addWarning ($ error , $ stackPtr , 'Found ' );
124
+ }
121
125
122
126
}//end process()
123
127
0 commit comments