Skip to content

Commit 5058218

Browse files
authored
CS/QA: various small tweaks (sirbrillig#256)
* CS/QA: use single quotes for text strings without embedded variables * CS/QA: always have a comma after each item in multi-line arrays * CS/QA: use `self` to refer to current class * CS/QA: no need for parentheses with require/include These are language constructs, not functions, so the parentheses are unnecessary. * CS/QA: consistently use short arrays This fixes up the one instance where there was still a long array used in the code base. * CS/QA: use strict comparisons The `Helper::findContainingOpeningBracket()` method can return either an integer or `null`, so this comparison should be a strict comparison. * CS/QA: use pre-increment ... instead of post-increment as it is less prone to surprising results if code is moved around. Co-authored-by: jrfnl <jrfnl@users.noreply.github.com>
1 parent 5b0845c commit 5058218

File tree

5 files changed

+26
-26
lines changed

5 files changed

+26
-26
lines changed

Tests/VariableAnalysisSniff/VariableAnalysisTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ public function testClassReferenceWarnings() {
465465
22,
466466
23,
467467
24,
468-
25
468+
25,
469469
];
470470
$this->assertSame($expectedWarnings, $lines);
471471
}
@@ -837,7 +837,7 @@ public function testValidUndefinedVariableNamesIgnoresUndefinedProperties() {
837837
22,
838838
23,
839839
24,
840-
25
840+
25,
841841
];
842842
$this->assertSame($expectedWarnings, $lines);
843843
}
@@ -856,7 +856,7 @@ public function testValidUndefinedVariableRegexpIgnoresUndefinedProperties() {
856856
12,
857857
13,
858858
24,
859-
25
859+
25,
860860
];
861861
$this->assertSame($expectedWarnings, $lines);
862862
}

Tests/bootstrap.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php
2-
require_once(__DIR__ . '/../vendor/squizlabs/php_codesniffer/tests/bootstrap.php');
3-
require_once(__DIR__ . '/BaseTestCase.php');
2+
require_once __DIR__ . '/../vendor/squizlabs/php_codesniffer/tests/bootstrap.php';
3+
require_once __DIR__ . '/BaseTestCase.php';

VariableAnalysis/Lib/Helpers.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ public static function getUseIndexForUseImport(File $phpcsFile, $stackPtr) {
244244
public static function findFunctionCall(File $phpcsFile, $stackPtr) {
245245
$tokens = $phpcsFile->getTokens();
246246

247-
$openPtr = Helpers::findContainingOpeningBracket($phpcsFile, $stackPtr);
247+
$openPtr = self::findContainingOpeningBracket($phpcsFile, $stackPtr);
248248
if (is_int($openPtr)) {
249249
// First non-whitespace thing and see if it's a T_STRING function name
250250
$functionPtr = $phpcsFile->findPrevious(Tokens::$emptyTokens, $openPtr - 1, null, true, null, true);
@@ -267,7 +267,7 @@ public static function findFunctionCallArguments(File $phpcsFile, $stackPtr) {
267267
// Slight hack: also allow this to find args for array constructor.
268268
if (($tokens[$stackPtr]['code'] !== T_STRING) && ($tokens[$stackPtr]['code'] !== T_ARRAY)) {
269269
// Assume $stackPtr is something within the brackets, find our function call
270-
$stackPtr = Helpers::findFunctionCall($phpcsFile, $stackPtr);
270+
$stackPtr = self::findFunctionCall($phpcsFile, $stackPtr);
271271
if ($stackPtr === null) {
272272
return [];
273273
}
@@ -289,7 +289,7 @@ public static function findFunctionCallArguments(File $phpcsFile, $stackPtr) {
289289
$lastArgComma = $openPtr;
290290
$nextPtr = $phpcsFile->findNext([T_COMMA], $lastPtr + 1, $closePtr);
291291
while (is_int($nextPtr)) {
292-
if (Helpers::findContainingOpeningBracket($phpcsFile, $nextPtr) == $openPtr) {
292+
if (self::findContainingOpeningBracket($phpcsFile, $nextPtr) === $openPtr) {
293293
// Comma is at our level of brackets, it's an argument delimiter.
294294
$range = range($lastArgComma + 1, $nextPtr - 1);
295295
$range = array_filter($range, function ($element) {
@@ -648,7 +648,7 @@ public static function getListAssignments(File $phpcsFile, $listOpenerIndex) {
648648
$parents = isset($tokens[$listOpenerIndex]['nested_parenthesis']) ? $tokens[$listOpenerIndex]['nested_parenthesis'] : [];
649649
// There's no record of nested brackets for short lists; we'll have to find the parent ourselves
650650
if (empty($parents)) {
651-
$parentSquareBracket = Helpers::findContainingOpeningSquareBracket($phpcsFile, $listOpenerIndex);
651+
$parentSquareBracket = self::findContainingOpeningSquareBracket($phpcsFile, $listOpenerIndex);
652652
if (is_int($parentSquareBracket)) {
653653
// Collect the opening index, but we don't actually need the closing paren index so just make that 0
654654
$parents[$parentSquareBracket] = 0;
@@ -677,7 +677,7 @@ public static function getListAssignments(File $phpcsFile, $listOpenerIndex) {
677677
if (is_int($variablePtr)) {
678678
$variablePtrs[] = $variablePtr;
679679
}
680-
$currentPtr++;
680+
++$currentPtr;
681681
}
682682

683683
return $variablePtrs;
@@ -715,7 +715,7 @@ public static function debug() {
715715
if (PHP_CODESNIFFER_VERBOSITY <= 3) {
716716
return;
717717
}
718-
$output = PHP_EOL . "VariableAnalysisSniff: DEBUG:";
718+
$output = PHP_EOL . 'VariableAnalysisSniff: DEBUG:';
719719
foreach ($messages as $message) {
720720
if (is_string($message) || is_numeric($message)) {
721721
$output .= ' "' . $message . '"';
@@ -866,7 +866,7 @@ public static function getScopeCloseForScopeOpen(File $phpcsFile, $scopeStartInd
866866
}
867867

868868
if ($scopeStartIndex === 0) {
869-
$scopeCloserIndex = Helpers::getLastNonEmptyTokenIndexInFile($phpcsFile);
869+
$scopeCloserIndex = self::getLastNonEmptyTokenIndexInFile($phpcsFile);
870870
}
871871
return $scopeCloserIndex;
872872
}

VariableAnalysis/Lib/VariableInfo.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ class VariableInfo {
9090
/**
9191
* @var string[]
9292
*/
93-
public static $scopeTypeDescriptions = array(
93+
public static $scopeTypeDescriptions = [
9494
ScopeType::LOCAL => 'variable',
9595
ScopeType::PARAM => 'function parameter',
9696
ScopeType::STATICSCOPE => 'static variable',
9797
ScopeType::GLOBALSCOPE => 'global variable',
9898
ScopeType::BOUND => 'bound variable',
99-
);
99+
];
100100

101101
/**
102102
* @param string $varName

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ protected function markVariableDeclaration(
482482
// we catch declarations that come after implicit declarations like
483483
// use of a variable as a local.
484484
$this->addWarning(
485-
"Redeclaration of %s %s as %s.",
485+
'Redeclaration of %s %s as %s.',
486486
$stackPtr,
487487
'VariableRedeclaration',
488488
[
@@ -625,28 +625,28 @@ protected function markAllVariablesRead(File $phpcsFile, $stackPtr) {
625625
* @return void
626626
*/
627627
protected function processVariableAsFunctionDefinitionArgument(File $phpcsFile, $stackPtr, $varName, $outerScope) {
628-
Helpers::debug("processVariableAsFunctionDefinitionArgument", $stackPtr, $varName);
628+
Helpers::debug('processVariableAsFunctionDefinitionArgument', $stackPtr, $varName);
629629
$tokens = $phpcsFile->getTokens();
630630

631631
$functionPtr = Helpers::getFunctionIndexForFunctionArgument($phpcsFile, $stackPtr);
632632
if (! is_int($functionPtr)) {
633633
throw new \Exception("Function index not found for function argument index {$stackPtr}");
634634
}
635635

636-
Helpers::debug("processVariableAsFunctionDefinitionArgument found function definition", $tokens[$functionPtr]);
636+
Helpers::debug('processVariableAsFunctionDefinitionArgument found function definition', $tokens[$functionPtr]);
637637
$this->markVariableDeclaration($varName, ScopeType::PARAM, null, $stackPtr, $functionPtr);
638638

639639
// Are we pass-by-reference?
640640
$referencePtr = $phpcsFile->findPrevious(Tokens::$emptyTokens, $stackPtr - 1, null, true, null, true);
641641
if (($referencePtr !== false) && ($tokens[$referencePtr]['code'] === T_BITWISE_AND)) {
642-
Helpers::debug("processVariableAsFunctionDefinitionArgument found pass-by-reference to scope", $outerScope);
642+
Helpers::debug('processVariableAsFunctionDefinitionArgument found pass-by-reference to scope', $outerScope);
643643
$varInfo = $this->getOrCreateVariableInfo($varName, $functionPtr);
644644
$varInfo->referencedVariableScope = $outerScope;
645645
}
646646

647647
// Are we optional with a default?
648648
if (Helpers::getNextAssignPointer($phpcsFile, $stackPtr) !== null) {
649-
Helpers::debug("processVariableAsFunctionDefinitionArgument optional with default");
649+
Helpers::debug('processVariableAsFunctionDefinitionArgument optional with default');
650650
$this->markVariableAssignment($varName, $stackPtr, $functionPtr);
651651
}
652652
}
@@ -664,7 +664,7 @@ protected function processVariableAsFunctionDefinitionArgument(File $phpcsFile,
664664
protected function processVariableAsUseImportDefinition(File $phpcsFile, $stackPtr, $varName, $outerScope) {
665665
$tokens = $phpcsFile->getTokens();
666666

667-
Helpers::debug("processVariableAsUseImportDefinition", $stackPtr, $varName, $outerScope);
667+
Helpers::debug('processVariableAsUseImportDefinition', $stackPtr, $varName, $outerScope);
668668

669669
$endOfArgsPtr = $phpcsFile->findPrevious([T_CLOSE_PARENTHESIS], $stackPtr - 1, null);
670670
if (! is_int($endOfArgsPtr)) {
@@ -1194,7 +1194,7 @@ protected function processVariableAsForeachLoopVar(File $phpcsFile, $stackPtr, $
11941194
// Are we pass-by-reference?
11951195
$referencePtr = $phpcsFile->findPrevious(Tokens::$emptyTokens, $stackPtr - 1, null, true, null, true);
11961196
if (($referencePtr !== false) && ($tokens[$referencePtr]['code'] === T_BITWISE_AND)) {
1197-
Helpers::debug("processVariableAsForeachLoopVar: found foreach loop variable assigned by reference");
1197+
Helpers::debug('processVariableAsForeachLoopVar: found foreach loop variable assigned by reference');
11981198
$varInfo->isDynamicReference = true;
11991199
}
12001200

@@ -1548,7 +1548,7 @@ protected function processVariableInString(File $phpcsFile, $stackPtr) {
15481548
if (!preg_match_all(Constants::getDoubleQuotedVarRegexp(), $token['content'], $matches)) {
15491549
return;
15501550
}
1551-
Helpers::debug("examining token for variable in string", $token);
1551+
Helpers::debug('examining token for variable in string', $token);
15521552

15531553
foreach ($matches[1] as $varName) {
15541554
$varName = Helpers::normalizeVarName($varName);
@@ -1726,7 +1726,7 @@ protected function warnAboutUnusedVariable(File $phpcsFile, VariableInfo $varInf
17261726
foreach (array_unique($varInfo->allAssignments) as $indexForWarning) {
17271727
Helpers::debug("variable {$varInfo->name} at end of scope looks unused");
17281728
$phpcsFile->addWarning(
1729-
"Unused %s %s.",
1729+
'Unused %s %s.',
17301730
$indexForWarning,
17311731
'UnusedVariable',
17321732
[
@@ -1746,7 +1746,7 @@ protected function warnAboutUnusedVariable(File $phpcsFile, VariableInfo $varInf
17461746
*/
17471747
protected function warnAboutUndefinedVariable(File $phpcsFile, $varName, $stackPtr) {
17481748
$phpcsFile->addWarning(
1749-
"Variable %s is undefined.",
1749+
'Variable %s is undefined.',
17501750
$stackPtr,
17511751
'UndefinedVariable',
17521752
["\${$varName}"]
@@ -1762,7 +1762,7 @@ protected function warnAboutUndefinedVariable(File $phpcsFile, $varName, $stackP
17621762
*/
17631763
protected function warnAboutUndefinedArrayPushShortcut(File $phpcsFile, $varName, $stackPtr) {
17641764
$phpcsFile->addWarning(
1765-
"Array variable %s is undefined.",
1765+
'Array variable %s is undefined.',
17661766
$stackPtr,
17671767
'UndefinedVariable',
17681768
["\${$varName}"]
@@ -1778,7 +1778,7 @@ protected function warnAboutUndefinedArrayPushShortcut(File $phpcsFile, $varName
17781778
*/
17791779
protected function warnAboutUndefinedUnset(File $phpcsFile, $varName, $stackPtr) {
17801780
$phpcsFile->addWarning(
1781-
"Variable %s inside unset call is undefined.",
1781+
'Variable %s inside unset call is undefined.',
17821782
$stackPtr,
17831783
'UndefinedUnsetVariable',
17841784
["\${$varName}"]

0 commit comments

Comments
 (0)