Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ Before create the PR or merge into develop, please run next commands for validat

```shell
./bin/phpunit
./bin/phpstan

./bin/phpcs --config-set show_warnings 0
./bin/phpcs --standard=vendor/escapestudios/symfony2-coding-standard/Symfony/ src/
./bin/phpcs --standard=tests/phpcs.xml tests/
./bin/phpcs --standard=src/phpcs.xml src/

```

1 change: 1 addition & 0 deletions src/PhpCs/FiveLab/ErrorCodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ final class ErrorCodes
public const ARRAYS_DOC_VECTOR = 'ArraysDocVector';
public const PHPDOC_NOT_ALLOWED = 'PhpDocNotAllowed';
public const MISSED_SPACE = 'MissedSpace';
public const MANY_SPACES = 'ManySpaces';
}
94 changes: 94 additions & 0 deletions src/PhpCs/FiveLab/Sniffs/Formatting/ManySpacesSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types = 1);

/*
* This file is part of the FiveLab CiRules package
*
* (c) FiveLab
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code
*/

namespace FiveLab\Component\CiRules\PhpCs\FiveLab\Sniffs\Formatting;

use FiveLab\Component\CiRules\PhpCs\FiveLab\ErrorCodes;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

class ManySpacesSniff implements Sniff
{
public function register(): array
{
return [
T_WHITESPACE,
];
}

public function process(File $phpcsFile, mixed $stackPtr): void
{
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];
$content = $tokens[$stackPtr]['content'];

// only spaces
if (!\preg_match('/^ +$/', $content)) {
return;
}

$nextPtr = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
$prevPtr = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);

if (false === $prevPtr || \str_contains($tokens[$stackPtr - 1]['content'], \chr(10))) {
return;
}

if (T_SEMICOLON !== $tokens[$nextPtr]['code'] && 1 === \strlen($token['content'])) {
return;
}

if (T_SEMICOLON === $tokens[$nextPtr]['code']) {
$phpcsFile->addError(
'Too many spaces. There must be no space before a semicolon.',
$stackPtr,
ErrorCodes::MANY_SPACES
);

return;
}

if (T_DOUBLE_ARROW === $tokens[$nextPtr]['code']) {
return;
}

if (T_MATCH_ARROW === $tokens[$nextPtr]['code']) {
return;
}

if (T_CONST === $tokens[$stackPtr - 3]['code'] || T_CONST === $tokens[$stackPtr - 5]['code']) {
return;
}

if (T_ENUM_CASE === $tokens[$stackPtr - 3]['code']) {
return;
}

$functionPtr = $phpcsFile->findPrevious([T_FUNCTION, T_CLOSURE, T_FN], $stackPtr);

if (\array_key_exists('parenthesis_opener', $tokens[$functionPtr]) && \array_key_exists('parenthesis_closer', $tokens[$functionPtr])) {
$open = $tokens[$functionPtr]['parenthesis_opener'];
$close = $tokens[$functionPtr]['parenthesis_closer'];

if ($stackPtr > $open && $stackPtr < $close) {
return;
}
}

$phpcsFile->addError(
'Too many spaces. Must be one space.',
$stackPtr,
ErrorCodes::MANY_SPACES
);
}
}
103 changes: 103 additions & 0 deletions tests/PhpCs/FiveLab/Sniffs/Formatting/ManySpacesSniffTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

/*
* This file is part of the FiveLab CiRules package
*
* (c) FiveLab
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code
*/

declare(strict_types = 1);

namespace FiveLab\Component\CiRules\Tests\PhpCs\FiveLab\Sniffs\Formatting;

use FiveLab\Component\CiRules\PhpCs\FiveLab\Sniffs\Formatting\ManySpacesSniff;
use FiveLab\Component\CiRules\Tests\PhpCs\FiveLab\Sniffs\SniffTestCase;

class ManySpacesSniffTest extends SniffTestCase
{
protected function getSniffClass(): string
{
return ManySpacesSniff::class;
}

public static function provideDataSet(): array
{
return [
'success' => [
__DIR__.'/Resources/many-spaces/success.php',
],

'wrong' => [
__DIR__.'/Resources/many-spaces/wrong.php',
[
'message' => 'Too many spaces. Must be one space.',
'source' => 'FiveLab.Formatting.ManySpaces.ManySpaces',
'line' => 3,
],
[
'message' => 'Too many spaces. Must be one space.',
'source' => 'FiveLab.Formatting.ManySpaces.ManySpaces',
'line' => 4,
],
[
'message' => 'Too many spaces. There must be no space before a semicolon.',
'source' => 'FiveLab.Formatting.ManySpaces.ManySpaces',
'line' => 5,
],
[
'message' => 'Too many spaces. Must be one space.',
'source' => 'FiveLab.Formatting.ManySpaces.ManySpaces',
'line' => 7,
],
[
'message' => 'Too many spaces. Must be one space.',
'source' => 'FiveLab.Formatting.ManySpaces.ManySpaces',
'line' => 8,
],
[
'message' => 'Too many spaces. Must be one space.',
'source' => 'FiveLab.Formatting.ManySpaces.ManySpaces',
'line' => 9,
],
[
'message' => 'Too many spaces. Must be one space.',
'source' => 'FiveLab.Formatting.ManySpaces.ManySpaces',
'line' => 10,
],
[
'message' => 'Too many spaces. Must be one space.',
'source' => 'FiveLab.Formatting.ManySpaces.ManySpaces',
'line' => 11,
],
[
'message' => 'Too many spaces. Must be one space.',
'source' => 'FiveLab.Formatting.ManySpaces.ManySpaces',
'line' => 12,
],
[
'message' => 'Too many spaces. Must be one space.',
'source' => 'FiveLab.Formatting.ManySpaces.ManySpaces',
'line' => 13,
],
[
'message' => 'Too many spaces. Must be one space.',
'source' => 'FiveLab.Formatting.ManySpaces.ManySpaces',
'line' => 14,
],
[
'message' => 'Too many spaces. Must be one space.',
'source' => 'FiveLab.Formatting.ManySpaces.ManySpaces',
'line' => 17,
],
[
'message' => 'Too many spaces. Must be one space.',
'source' => 'FiveLab.Formatting.ManySpaces.ManySpaces',
'line' => 20,
],
],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

enum Enum1: string
{
case Bla = 'Bla';
case Bazzzz = 'Baz';
}

public const DD1 = 1;
public const DD2567 = 2;
public const string HHT = 'dsd';
function __construct(
public Bla $bla,
public BlaBlaBla $blaBlaBla
) {
}

$a = 1;
$b = 2;
$c = -1;

if ($a !== $b) {
}
if ($a > $b) {
}

$ar = [
'a' => 8,
'bla' => 5,
'fddfdf0' => 65,
];

return [
'success' => [],
'bba' => 1,
];

$ds = match('dfdfdf0') {
'ffd' => 'ffd',
'fddddd' => 'ddd',
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

$a = 1;
$b = 2;
$c = -1 ;

if ($a > $b) {}
if ($a === $b) {}
if ($a > $b) {}
if ($a === $b) {}
if ($a && $b) {}
if ($a || $b) {}
if ($a && $b) {}
if ($a || $b) {}

$arr = [
$d => 1
];

return $a;