Skip to content
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

Auto-fix Magento2.Whitespace.MultipleEmptyLines #449

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
17 changes: 16 additions & 1 deletion Magento2/Sniffs/Whitespace/MultipleEmptyLinesSniff.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento2\Sniffs\Whitespace;

use PHP_CodeSniffer\Files\File;
Expand Down Expand Up @@ -53,12 +55,25 @@ public function process(File $phpcsFile, $stackPtr)
$next = $phpcsFile->findNext(T_WHITESPACE, $stackPtr, null, true);
$lines = $tokens[$next]['line'] - $tokens[$stackPtr]['line'];
if ($lines > 1) {
$phpcsFile->addWarning(
$fix = $phpcsFile->addFixableWarning(
$this->warningMessage,
$stackPtr,
$this->warningCode,
[$lines]
);

if ($fix) {
// $stackPtr + 1 to keep one empty line.
// $next - 1 to keep the indentation
for ($i = $stackPtr + 1; $i < $next - 1; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}

// Handle case where the next line isn't indented
if ($tokens[$next - 1]['content'] === PHP_EOL) {
$phpcsFile->fixer->replaceToken($next - 1, '');
}
}
}
}
}
Expand Down
64 changes: 64 additions & 0 deletions Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,70 @@ class Foo
$someValue = 1;


$someValue = 2;



$someValue = 3;



$someValue = 40;







































$someValue = 'newlines and spaces and tabs';











$someValue = 'no indentation';



return $someValue;
}
}
24 changes: 24 additions & 0 deletions Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.inc.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
class Foo
{
private $foo;
private $bar;

private $baz;
public function test()
{
$someValue = 1;

$someValue = 2;

$someValue = 3;

$someValue = 40;

$someValue = 'newlines and spaces and tabs';

$someValue = 'no indentation';
ihor-sviziev marked this conversation as resolved.
Show resolved Hide resolved

return $someValue;
}
}
7 changes: 7 additions & 0 deletions Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento2\Tests\Whitespace;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
Expand All @@ -25,6 +27,11 @@ public function getWarningList()
return [
6 => 1,
12 => 1,
15 => 1,
19 => 1,
23 => 1,
63 => 1,
75 => 1,
];
}
}