Skip to content

Commit

Permalink
PSR12/ControlStructureSpacing: small tweak
Browse files Browse the repository at this point in the history
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 neglible.

Also: the `process()` method of the PSR2 sniff is a `void` method, so returning its return value is moot.
  • Loading branch information
jrfnl committed Dec 26, 2023
1 parent 6be2da7 commit 642ef60
Showing 1 changed file with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 642ef60

Please sign in to comment.