Skip to content

Commit c45549d

Browse files
committed
Generic/UnnecessaryStringConcat: lower nesting levels by bowing out early
This commit implements an small improvement suggested by @jrfnl during the PR review. It changes two conditions to adopt a bowing out early strategy to lower the nesting levels.
1 parent d555c4f commit c45549d

File tree

1 file changed

+35
-29
lines changed

1 file changed

+35
-29
lines changed

src/Standards/Generic/Sniffs/Strings/UnnecessaryStringConcatSniff.php

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -88,36 +88,42 @@ public function process(File $phpcsFile, $stackPtr)
8888
return;
8989
}
9090

91-
if (isset(Tokens::$stringTokens[$tokens[$prev]['code']]) === true
92-
&& isset(Tokens::$stringTokens[$tokens[$next]['code']]) === true
91+
if (isset(Tokens::$stringTokens[$tokens[$prev]['code']]) === false
92+
|| isset(Tokens::$stringTokens[$tokens[$next]['code']]) === false
9393
) {
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
94+
// Bow out as at least one of the two tokens being concatenated is not a string.
95+
return;
96+
}
97+
98+
if ($tokens[$prev]['content'][0] !== $tokens[$next]['content'][0]) {
99+
// Bow out as the two strings are not of the same type.
100+
return;
101+
}
102+
103+
// Before we throw an error for PHP, allow strings to be
104+
// combined if they would have < and ? next to each other because
105+
// this trick is sometimes required in PHP strings.
106+
if ($phpcsFile->tokenizerType === 'PHP') {
107+
$prevChar = substr($tokens[$prev]['content'], -2, 1);
108+
$nextChar = $tokens[$next]['content'][1];
109+
$combined = $prevChar.$nextChar;
110+
if ($combined === '?'.'>' || $combined === '<'.'?') {
111+
return;
112+
}
113+
}
114+
115+
if ($this->allowMultiline === true
116+
&& $tokens[$prev]['line'] !== $tokens[$next]['line']
117+
) {
118+
return;
119+
}
120+
121+
$error = 'String concat is not required here; use a single string instead';
122+
if ($this->error === true) {
123+
$phpcsFile->addError($error, $stackPtr, 'Found');
124+
} else {
125+
$phpcsFile->addWarning($error, $stackPtr, 'Found');
126+
}
121127

122128
}//end process()
123129

0 commit comments

Comments
 (0)