From 069331a424842b865f25fa0b6b86314e9521b909 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 28 Oct 2023 20:36:51 +0200 Subject: [PATCH] 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. --- .../ControlStructureSpacingSniff.php | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/Standards/PSR12/Sniffs/ControlStructures/ControlStructureSpacingSniff.php b/src/Standards/PSR12/Sniffs/ControlStructures/ControlStructureSpacingSniff.php index 2ddb66340b..9a200aa81c 100644 --- a/src/Standards/PSR12/Sniffs/ControlStructures/ControlStructureSpacingSniff.php +++ b/src/Standards/PSR12/Sniffs/ControlStructures/ControlStructureSpacingSniff.php @@ -11,7 +11,7 @@ use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\Sniff; -use PHP_CodeSniffer\Standards\PSR2\Sniffs\ControlStructures\ControlStructureSpacingSniff as PSR2Spacing; +use PHP_CodeSniffer\Standards\PSR2\Sniffs\ControlStructures\ControlStructureSpacingSniff as PSR2ControlStructureSpacing; use PHP_CodeSniffer\Util\Tokens; class ControlStructureSpacingSniff implements Sniff @@ -24,6 +24,23 @@ class ControlStructureSpacingSniff implements Sniff */ public $indent = 4; + /** + * Instance of the PSR2 ControlStructureSpacingSniff sniff. + * + * @var \PHP_CodeSniffer\Standards\PSR2\Sniffs\ControlStructures\ControlStructureSpacingSniff + */ + private $psr2ControlStructureSpacing; + + + /** + * Constructor. + */ + public function __construct() + { + $this->psr2ControlStructureSpacing = new PSR2ControlStructureSpacing(); + + }//end __construct() + /** * Returns an array of tokens this test wants to listen for. @@ -70,8 +87,7 @@ public function process(File $phpcsFile, $stackPtr) if ($tokens[$parenOpener]['line'] === $tokens[$parenCloser]['line']) { // Conditions are all on the same line, so follow PSR2. - $sniff = new PSR2Spacing(); - return $sniff->process($phpcsFile, $stackPtr); + return $this->psr2ControlStructureSpacing->process($phpcsFile, $stackPtr); } $next = $phpcsFile->findNext(T_WHITESPACE, ($parenOpener + 1), $parenCloser, true);