-
Notifications
You must be signed in to change notification settings - Fork 51
implement new sniff to force spaces after =
#123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Ocramius
wants to merge
1
commit into
doctrine:master
from
vv12131415:force-spaces-after-assigment-operator
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
lib/Doctrine/Sniffs/Arrays/OperatorSpacingAfterSniff.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Sniffs\Arrays; | ||
|
||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\OperatorSpacingSniff as SquizOperatorSpacingSniff; | ||
use const T_EQUAL; | ||
use const T_INLINE_ELSE; | ||
use const T_INLINE_THEN; | ||
use const T_WHITESPACE; | ||
use function strlen; | ||
|
||
final class OperatorSpacingAfterSniff extends SquizOperatorSpacingSniff | ||
{ | ||
private const MESSAGE_AFTER = 'Expected exactly 1 space after "%s"; %d found'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function register() : array | ||
{ | ||
return [T_EQUAL]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @param int $pointer | ||
*/ | ||
public function process(File $phpcsFile, $pointer) : void | ||
{ | ||
if (! $this->isOperator($phpcsFile, $pointer)) { | ||
return; | ||
} | ||
|
||
$this->ensureOneSpaceAfterOperator($phpcsFile, $pointer, $phpcsFile->getTokens()); | ||
} | ||
|
||
/** | ||
* @param mixed[] $tokens | ||
*/ | ||
private function ensureOneSpaceAfterOperator(File $file, int $pointer, array $tokens) : void | ||
{ | ||
if (! $this->shouldValidateAfter($pointer, $tokens)) { | ||
return; | ||
} | ||
|
||
$numberOfSpaces = $this->numberOfSpaces($tokens[$pointer + 1]); | ||
|
||
if ($numberOfSpaces === 1 || ! $this->recordErrorAfter($file, $pointer, $tokens[$pointer], $numberOfSpaces)) { | ||
return; | ||
} | ||
|
||
if ($numberOfSpaces === 0) { | ||
$file->fixer->addContent($pointer, ' '); | ||
|
||
return; | ||
} | ||
|
||
$file->fixer->replaceToken($pointer + 1, ' '); | ||
} | ||
|
||
/** | ||
* @param mixed[] $tokens | ||
*/ | ||
private function shouldValidateAfter(int $pointer, array $tokens) : bool | ||
{ | ||
if (! isset($tokens[$pointer + 1])) { | ||
return false; | ||
} | ||
|
||
return $tokens[$pointer]['code'] !== T_INLINE_THEN || $tokens[$pointer + 1]['code'] !== T_INLINE_ELSE; | ||
} | ||
|
||
/** | ||
* @param mixed[] $token | ||
*/ | ||
private function recordErrorAfter(File $file, int $pointer, array $token, int $numberOfSpaces) : bool | ||
{ | ||
return $file->addFixableError( | ||
self::MESSAGE_AFTER, | ||
$pointer, | ||
'NoSpaceAfterAssigment', | ||
[$token['content'], $numberOfSpaces] | ||
); | ||
} | ||
|
||
/** | ||
* @param mixed[] $token | ||
*/ | ||
private function numberOfSpaces(array $token) : int | ||
{ | ||
if ($token['code'] !== T_WHITESPACE) { | ||
return 0; | ||
} | ||
|
||
return strlen($token['content']); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
parameters: | ||
level: 7 | ||
paths: | ||
- "%currentWorkingDirectory%/lib/" | ||
- "%currentWorkingDirectory%/tests/Doctrine" | ||
autoload_files: | ||
- "%currentWorkingDirectory%/tests/bootstrap.php" | ||
ignoreErrors: | ||
- | ||
message: '#Method [a-zA-Z0-9\\_]+::process\(\) has parameter \$pointer with no typehint specified#' | ||
path: %currentWorkingDirectory%/lib/Doctrine/Sniffs/* | ||
|
||
includes: | ||
- vendor/phpstan/phpstan-deprecation-rules/rules.neon | ||
- vendor/phpstan/phpstan-phpunit/extension.neon | ||
- vendor/phpstan/phpstan-phpunit/rules.neon | ||
- vendor/phpstan/phpstan-strict-rules/rules.neon |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" | ||
colors="true" | ||
beStrictAboutOutputDuringTests="true" | ||
beStrictAboutTodoAnnotatedTests="true" | ||
failOnRisky="true" | ||
failOnWarning="true" | ||
bootstrap="tests/bootstrap.php" | ||
> | ||
|
||
<testsuite name="Doctrine Coding Standard Test Suite"> | ||
<directory suffix="Test.php">tests/Doctrine/</directory> | ||
</testsuite> | ||
</phpunit> |
25 changes: 25 additions & 0 deletions
25
tests/Doctrine/Sniffs/Arrays/OperatorSpacingAfterSniffTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Sniffs\Arrays; | ||
|
||
use SlevomatCodingStandard\Sniffs\TestCase; | ||
|
||
final class OperatorSpacingAfterSniffTest extends TestCase | ||
{ | ||
public function testNoErrors() : void | ||
{ | ||
$phpcsFile = self::checkFile(__DIR__ . '/data/operatorSpacingAfterSniff.noErrors.php'); | ||
self::assertNoSniffErrorInFile($phpcsFile); | ||
} | ||
|
||
public function testErrors() : void | ||
{ | ||
$phpcsFile = self::checkFile(__DIR__ . '/data/operatorSpacingAfterSniff.errors.php'); | ||
|
||
self::assertSame(1, $phpcsFile->getErrorCount()); | ||
|
||
self::assertSniffError($phpcsFile, 4, 'NoSpaceAfterAssigment'); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/Doctrine/Sniffs/Arrays/data/operatorSpacingAfterSniff.errors.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
|
||
$arrayWithMultipleSpacesAfterAssigmentOperator = [ | ||
0, | ||
1, | ||
2, | ||
3, | ||
4, | ||
5, | ||
]; |
21 changes: 21 additions & 0 deletions
21
tests/Doctrine/Sniffs/Arrays/data/operatorSpacingAfterSniff.noErrors.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
$array = [ | ||
0, | ||
1, | ||
2, | ||
3, | ||
4, | ||
5, | ||
]; | ||
|
||
$arrayWithMultipleSpacesBeforeAssigmentOperator = [ | ||
0, | ||
1, | ||
2, | ||
3, | ||
4, | ||
5, | ||
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
Ocramius marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
declare(strict_types = 1); | ||
|
||
error_reporting(E_ALL); | ||
|
||
require __DIR__.'/../vendor/autoload.php'; | ||
require __DIR__.'/../vendor/squizlabs/php_codesniffer/tests/bootstrap.php'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,12 @@ | |
9, | ||
], | ||
]; | ||
|
||
$arrayWithMultipleSpacesAfterAssigmentOperator = [ | ||
0, | ||
1, | ||
2, | ||
3, | ||
4, | ||
5, | ||
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
declare(strict_types=1); | ||
|
||
$array = [ | ||
$array = [ | ||
'key1' => 'value', | ||
'key2' => 'value', | ||
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,12 @@ | |
9, | ||
], | ||
]; | ||
|
||
$arrayWithMultipleSpacesAfterAssigmentOperator = [ | ||
0, | ||
1, | ||
2, | ||
3, | ||
4, | ||
5, | ||
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.