Skip to content
This repository has been archived by the owner on Nov 11, 2019. It is now read-only.

Commit

Permalink
Merge pull request #7 from hostnet/feature/empty-lines
Browse files Browse the repository at this point in the history
Added empty line sniffer
  • Loading branch information
yannickl88 authored Sep 4, 2017
2 parents f392642 + e0da06f commit a8c94ff
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/Sniff/EmptyLinesSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
/**
* @copyright 2017 Hostnet B.V.
*/
namespace Hostnet\Component\CssSniff\Sniff;

use Hostnet\Component\CssSniff\File;
use Hostnet\Component\CssSniff\SniffInterface;
use Yannickl88\Component\CSS\Token;

/**
* Checks that there are not more than two empty lines after each other. Only
* one is allowed.
*/
final class EmptyLinesSniff implements SniffInterface
{
private $newline_char;

public function __construct(string $newline_char = "\n")
{
$this->newline_char = $newline_char;
}

public function register(): array
{
return [
Token::T_WHITESPACE,
];
}

public function process(File $file, int $stack_ptr): void
{
$token = $file->get($stack_ptr);

if (substr_count($token->chars, $this->newline_char) > 2) {
$file->addViolation(
'More than two new lines found.',
$token->lines[0]
);
}
}
}
1 change: 1 addition & 0 deletions src/Standard/Hostnet.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<sniff class="\Hostnet\Component\CssSniff\Sniff\ColorSniff" />
<sniff class="\Hostnet\Component\CssSniff\Sniff\CurlySniff" />
<sniff class="\Hostnet\Component\CssSniff\Sniff\EmptyCommentSniff" />
<sniff class="\Hostnet\Component\CssSniff\Sniff\EmptyLinesSniff" />
<sniff class="\Hostnet\Component\CssSniff\Sniff\EmptySniff" />
<sniff class="\Hostnet\Component\CssSniff\Sniff\IdSniff" />
<sniff class="\Hostnet\Component\CssSniff\Sniff\IndentSniff" />
Expand Down
40 changes: 40 additions & 0 deletions test/Sniff/EmptyLinesSniffTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* @copyright 2017 Hostnet B.V.
*/
declare(strict_types=1);
namespace Hostnet\Component\CssSniff\Sniff;

use Hostnet\Component\CssSniff\File;
use Hostnet\Component\CssSniff\Sniffer;
use Hostnet\Component\CssSniff\Violation;
use PHPUnit\Framework\TestCase;
use Yannickl88\Component\CSS\Tokenizer;

/**
* @covers \Hostnet\Component\CssSniff\Sniff\EmptyLinesSniff
*/
class EmptyLinesSniffTest extends TestCase
{
/**
* @var Sniffer
*/
private $sniffer;

protected function setUp()
{
$this->sniffer = new Sniffer();
$this->sniffer->addSniff(new EmptyLinesSniff());
}

public function testSniff()
{
$file = new File((new Tokenizer())->tokenize(file_get_contents(__DIR__ . '/fixtures/newlines.less')));

$this->sniffer->process($file);

self::assertEquals([
new Violation('More than two new lines found.', 6),
], $file->getViolations());
}
}
6 changes: 6 additions & 0 deletions test/Sniff/fixtures/newlines.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// one line

// two lines


// end

0 comments on commit a8c94ff

Please sign in to comment.