Skip to content

Commit 069331a

Browse files
committed
PSR12/ControlStructureSpacing: small tweak
No need to create a new instance of the PSR2 sniff every single time the sniff is triggered. Re-using the same object, just like is done with sniffs in general, will work just as well. This will have a (tiny) positive impact on performance, though the impact is so small, it can be considered negligible.
1 parent 6be2da7 commit 069331a

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/Standards/PSR12/Sniffs/ControlStructures/ControlStructureSpacingSniff.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
use PHP_CodeSniffer\Files\File;
1313
use PHP_CodeSniffer\Sniffs\Sniff;
14-
use PHP_CodeSniffer\Standards\PSR2\Sniffs\ControlStructures\ControlStructureSpacingSniff as PSR2Spacing;
14+
use PHP_CodeSniffer\Standards\PSR2\Sniffs\ControlStructures\ControlStructureSpacingSniff as PSR2ControlStructureSpacing;
1515
use PHP_CodeSniffer\Util\Tokens;
1616

1717
class ControlStructureSpacingSniff implements Sniff
@@ -24,6 +24,23 @@ class ControlStructureSpacingSniff implements Sniff
2424
*/
2525
public $indent = 4;
2626

27+
/**
28+
* Instance of the PSR2 ControlStructureSpacingSniff sniff.
29+
*
30+
* @var \PHP_CodeSniffer\Standards\PSR2\Sniffs\ControlStructures\ControlStructureSpacingSniff
31+
*/
32+
private $psr2ControlStructureSpacing;
33+
34+
35+
/**
36+
* Constructor.
37+
*/
38+
public function __construct()
39+
{
40+
$this->psr2ControlStructureSpacing = new PSR2ControlStructureSpacing();
41+
42+
}//end __construct()
43+
2744

2845
/**
2946
* Returns an array of tokens this test wants to listen for.
@@ -70,8 +87,7 @@ public function process(File $phpcsFile, $stackPtr)
7087

7188
if ($tokens[$parenOpener]['line'] === $tokens[$parenCloser]['line']) {
7289
// Conditions are all on the same line, so follow PSR2.
73-
$sniff = new PSR2Spacing();
74-
return $sniff->process($phpcsFile, $stackPtr);
90+
return $this->psr2ControlStructureSpacing->process($phpcsFile, $stackPtr);
7591
}
7692

7793
$next = $phpcsFile->findNext(T_WHITESPACE, ($parenOpener + 1), $parenCloser, true);

0 commit comments

Comments
 (0)