Skip to content

Commit

Permalink
SpaceBeforeNotSniff
Browse files Browse the repository at this point in the history
  • Loading branch information
Spilky committed Sep 7, 2023
1 parent 9a186d3 commit 2741c53
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
"email": "milan.pala@peckadesign.cz"
}
],
"autoload": {
"psr-4": {
"Pd\\CodingStandard\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"PdTests\\": "tests/"
Expand Down
2 changes: 2 additions & 0 deletions src/PeckaCodingStandardPSR12Based/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

<arg value="n"/>

<rule ref="coding-standard/Sniffs"/>

<rule ref="PSR12">
<exclude name="Generic.WhiteSpace.DisallowTabIndent"/>
<exclude name="PSR12.Files.DeclareStatement"/>
Expand Down
125 changes: 125 additions & 0 deletions src/Sniffs/SpactBeforeNotSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php declare(strict_types = 1);

namespace Pd\CodingStandard\Sniffs;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;

use const T_BOOLEAN_NOT;
use const T_WHITESPACE;

class SpaceBeforeNotSniff implements Sniff
{
/**
* A list of tokenizers this sniff supports.
*
* @var array<string>
*/
public array $supportedTokenizers = [
'PHP',
'JS',
];

/**
* The number of spaces desired after the NOT operator.
*
*/
public int $spacing = 1;

/**
* Allow newlines instead of spaces.
*
*/
public bool $ignoreNewlines = false;


/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return [T_BOOLEAN_NOT];
}


/**
* Processes this test, when one of its tokens is encountered.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$this->spacing = (int) $this->spacing;

$nextNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
if ($nextNonEmpty === false) {
return;
}

if (
$this->ignoreNewlines === true
&& $tokens[$stackPtr]['line'] !== $tokens[$nextNonEmpty]['line']
) {
return;
}

if ($this->spacing === 0 && $nextNonEmpty === ($stackPtr + 1)) {
return;
}

$previousNonWhitespace = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
if ($nextNonEmpty !== $previousNonWhitespace) {
$error = 'Expected %s space(s) before NOT operator';
$data = [$this->spacing];
$phpcsFile->addError($error, $stackPtr, 'CommentFound', $data);

return;
}

$found = 0;
if ($tokens[$stackPtr]['line'] !== $tokens[$nextNonEmpty]['line']) {
$found = 'newline';
} elseif ($tokens[($stackPtr - 1)]['code'] === T_WHITESPACE) {
$found = $tokens[($stackPtr - 1)]['length'];
}

if ($found === $this->spacing) {
return;
}

$error = 'Expected %s space(s) before NOT operator; %s found';
$data = [
$this->spacing,
$found,
];

$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Incorrect', $data);
if ($fix === true) {
$padding = str_repeat(' ', $this->spacing);
if ($found === 0) {
$phpcsFile->fixer->addContentBefore($stackPtr, $padding);
} else {
$phpcsFile->fixer->beginChangeset();
$start = ($stackPtr - 1);

if ($this->spacing > 0) {
$phpcsFile->fixer->replaceToken($start, $padding);
--$start;
}

for ($i = $start; $i < $previousNonWhitespace; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}

$phpcsFile->fixer->endChangeset();
}
}
}
}

0 comments on commit 2741c53

Please sign in to comment.