Skip to content

Refacto #94

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

Merged
merged 4 commits into from
Dec 25, 2019
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/
composer.phar
coverage/
vendor/
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace SymfonyCustom\Sniffs;
namespace SymfonyCustom\Helpers;

use PHP_CodeSniffer\Files\File;

Expand Down Expand Up @@ -58,6 +58,37 @@ public static function removeLines(File $phpcsFile, int $fromPtr, int $fromLine,
$phpcsFile->fixer->endChangeset();
}

/**
* @param File $phpcsFile
* @param int $stackPtr
* @param int $expected
* @param int|string $found
*/
public static function fixWhitespaceAfter(
File $phpcsFile,
int $stackPtr,
int $expected,
$found
): void {
$phpcsFile->fixer->beginChangeset();

if (0 === $found) {
$phpcsFile->fixer->addContent($stackPtr, str_repeat(' ', $expected));
} else {
if ('newline' === $found) {
$next = $phpcsFile->findNext(T_WHITESPACE, $stackPtr, null, true);

for ($i = $stackPtr + 1; $i < $next; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}
}

$phpcsFile->fixer->replaceToken($stackPtr + 1, str_repeat(' ', $expected));
}

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

/**
* @param File $phpcsFile
* @param int $stackPtr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace SymfonyCustom\Sniffs;
namespace SymfonyCustom\Helpers;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;
Expand Down
86 changes: 31 additions & 55 deletions SymfonyCustom/Sniffs/Arrays/ArrayDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
use SymfonyCustom\Sniffs\FixerHelper;
use SymfonyCustom\Helpers\FixerHelper;

/**
* A test to ensure that arrays conform to the array coding standard.
Expand Down Expand Up @@ -158,56 +158,40 @@ public function processSingleLineArray(File $phpcsFile, int $stackPtr, int $star
$nextArrow = $phpcsFile->findNext(T_DOUBLE_ARROW, $start + 1, $end);
while (false !== $nextArrow) {
if (T_WHITESPACE !== $tokens[$nextArrow - 1]['code']) {
$spaceBefore = 0;
} else {
$spaceBefore = $tokens[$nextArrow - 1]['length'];
}

if (1 !== $spaceBefore) {
$fix = $phpcsFile->addFixableError(
'Expected 1 space between "%s" and double arrow; 0 found',
'Expected 1 space between "%s" and double arrow; %s found',
$nextArrow,
'NoSpaceBeforeDoubleArrow',
[$tokens[$nextArrow - 1]['content']]
'SpaceAfterDoubleArrow',
[$tokens[$nextArrow - 1]['content'], $spaceBefore]
);

if ($fix) {
$phpcsFile->fixer->addContentBefore($nextArrow, ' ');
}
} else {
$spaceLength = $tokens[$nextArrow - 1]['length'];
if (1 !== $spaceLength) {
$fix = $phpcsFile->addFixableError(
'Expected 1 space between "%s" and double arrow; %s found',
$nextArrow,
'SpaceBeforeDoubleArrow',
[$tokens[$nextArrow - 2]['content'], $spaceLength]
);

if ($fix) {
$phpcsFile->fixer->replaceToken($nextArrow - 1, ' ');
}
FixerHelper::fixWhitespaceBefore($phpcsFile, $nextArrow, 1, $spaceBefore);
}
}

if (T_WHITESPACE !== $tokens[$nextArrow + 1]['code']) {
$spaceAfter = 0;
} else {
$spaceAfter = $tokens[$nextArrow + 1]['length'];
}

if (1 !== $spaceAfter) {
$fix = $phpcsFile->addFixableError(
'Expected 1 space between double arrow and "%s"; 0 found',
'Expected 1 space between double arrow and "%s"; %s found',
$nextArrow,
'NoSpaceAfterDoubleArrow',
[$tokens[$nextArrow + 1]['content']]
'SpaceAfterDoubleArrow',
[$tokens[$nextArrow + 1]['content'], $spaceAfter]
);

if ($fix) {
$phpcsFile->fixer->addContent($nextArrow, ' ');
}
} else {
$spaceLength = $tokens[$nextArrow + 1]['length'];
if (1 !== $spaceLength) {
$fix = $phpcsFile->addFixableError(
'Expected 1 space between double arrow and "%s"; %s found',
$nextArrow,
'SpaceAfterDoubleArrow',
[$tokens[$nextArrow + 2]['content'], $spaceLength]
);

if ($fix) {
$phpcsFile->fixer->replaceToken($nextArrow + 1, ' ');
}
FixerHelper::fixWhitespaceAfter($phpcsFile, $nextArrow, 1, $spaceAfter);
}
}

Expand All @@ -218,29 +202,21 @@ public function processSingleLineArray(File $phpcsFile, int $stackPtr, int $star
// We have a multiple value array
foreach ($commas as $comma) {
if (T_WHITESPACE !== $tokens[$comma + 1]['code']) {
$spaceAfter = 0;
} else {
$spaceAfter = $tokens[$comma + 1]['length'];
}

if (1 !== $spaceAfter) {
$fix = $phpcsFile->addFixableError(
'Expected 1 space between comma and "%s"; 0 found',
'Expected 1 space between comma and "%s"; %s found',
$comma,
'NoSpaceAfterComma',
[$tokens[$comma + 1]['content']]
'SpaceAfterComma',
[$tokens[$comma + 1]['content'], $spaceAfter]
);

if ($fix) {
$phpcsFile->fixer->addContent($comma, ' ');
}
} else {
$spaceLength = $tokens[$comma + 1]['length'];
if (1 !== $spaceLength) {
$fix = $phpcsFile->addFixableError(
'Expected 1 space between comma and "%s"; %s found',
$comma,
'SpaceAfterComma',
[$tokens[$comma + 2]['content'], $spaceLength]
);

if ($fix) {
$phpcsFile->fixer->replaceToken($comma + 1, ' ');
}
FixerHelper::fixWhitespaceAfter($phpcsFile, $comma, 1, $spaceAfter);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SymfonyCustom\Sniffs\FixerHelper;
use SymfonyCustom\Sniffs\SniffHelper;
use SymfonyCustom\Helpers\FixerHelper;
use SymfonyCustom\Helpers\SniffHelper;

/**
* Throws errors if comments are not grouped by type with one blank line between them.
Expand Down
2 changes: 1 addition & 1 deletion SymfonyCustom/Sniffs/Commenting/DocCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SymfonyCustom\Sniffs\FixerHelper;
use SymfonyCustom\Helpers\FixerHelper;

/**
* Ensures doc blocks follow basic formatting.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
use SymfonyCustom\Sniffs\SniffHelper;
use SymfonyCustom\Helpers\SniffHelper;

/**
* Class AlphabeticallySortedUseSniff
Expand Down
2 changes: 1 addition & 1 deletion SymfonyCustom/Sniffs/Namespaces/UnusedUseSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
use SymfonyCustom\Sniffs\SniffHelper;
use SymfonyCustom\Helpers\SniffHelper;

/**
* Checks for "use" statements that are not needed in a file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SymfonyCustom\Sniffs\SniffHelper;
use SymfonyCustom\Helpers\SniffHelper;

/**
* Checks that there are not 2 empty lines following each other.
Expand Down
2 changes: 2 additions & 0 deletions SymfonyCustom/Tests/Arrays/ArrayDeclarationUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,5 @@ return [
1 =>
2,
];

[1 => 2 , 2=>3,4 => 5];
2 changes: 2 additions & 0 deletions SymfonyCustom/Tests/Arrays/ArrayDeclarationUnitTest.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,5 @@ return [
0 => 1,
1 => 2,
];

[1 => 2, 2 => 3, 4 => 5];
1 change: 1 addition & 0 deletions SymfonyCustom/Tests/Arrays/ArrayDeclarationUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ protected function getErrorList(): array
107 => 2,
142 => 1,
148 => 1,
151 => 7,
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@ class ValidFileNameUnitTest extends AbstractSniffUnitTest
protected function getErrorList($filename = '')
{
switch ($filename) {
case 'ValidFileNameUnitTest.inc':
return [];
case 'ValidFileNameUnitTest.Invalid.inc':
return [
1 => 1,
];
return [1 => 1];
default:
return [];
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@
</exec>
</target>

<target name="coverage" depends="symlink-cs" description="Run coverage with PHPUnit">
<exec executable="${phpunit.bin}" failonerror="true" >
<arg value="--verbose"/>
<arg value="--group=SymfonyCustom"/>
<arg value="${phpcs.dir}/tests/AllTests.php"/>
<arg value="--coverage-html=coverage"/>
</exec>
</target>

<target name="phpcs" depends="symlink-cs" description="Find coding standard violations using PHP Code Sniffer">
<exec executable="${phpcs.bin}" failonerror="true">
<arg value="--standard=SymfonyCustom"/>
Expand Down
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@
"phpunit/phpunit": "^7.0"
},
"autoload": {
"psr-4": { "TwigCS\\": "TwigCS/" }
"psr-4": {
"TwigCS\\": "TwigCS/src/"
}
},
"autoload-dev": {
"psr-4": { "TwigCS\\Tests\\": "TwigCS/Tests/" }
"psr-4": {
"TwigCS\\Tests\\": "TwigCS/tests/"
}
}
}
Loading