Skip to content

Commit f89430d

Browse files
💄 Improvements
1 parent cd9e31c commit f89430d

9 files changed

+74
-41
lines changed

TwigCS/Linter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Linter
2727
/**
2828
* @var SniffsExtension
2929
*/
30-
protected $sniffExtension;
30+
protected $sniffsExtension;
3131

3232
/**
3333
* @var TokenizerInterface
@@ -41,7 +41,7 @@ class Linter
4141
public function __construct(Environment $env, TokenizerInterface $tokenizer)
4242
{
4343
$this->env = $env;
44-
$this->sniffExtension = $this->env->getExtension('TwigCS\Extension\SniffsExtension');
44+
$this->sniffsExtension = $this->env->getExtension('TwigCS\Extension\SniffsExtension');
4545
$this->tokenizer = $tokenizer;
4646
}
4747

@@ -80,7 +80,7 @@ public function run($files, Ruleset $ruleset)
8080

8181
foreach ($ruleset->getSniffs() as $sniff) {
8282
if ($sniff instanceof PostParserSniffInterface) {
83-
$this->sniffExtension->addSniff($sniff);
83+
$this->sniffsExtension->addSniff($sniff);
8484
}
8585

8686
$sniff->enable($report);
@@ -98,7 +98,7 @@ public function run($files, Ruleset $ruleset)
9898
restore_error_handler();
9999
foreach ($ruleset->getSniffs() as $sniff) {
100100
if ($sniff instanceof PostParserSniffInterface) {
101-
$this->sniffExtension->removeSniff($sniff);
101+
$this->sniffsExtension->removeSniff($sniff);
102102
}
103103

104104
$sniff->disable();

TwigCS/Report/Report.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function addMessage(SniffViolation $sniffViolation)
7777
*/
7878
public function getMessages($filters = [])
7979
{
80-
if (!$filters) {
80+
if (empty($filters)) {
8181
// Return all messages, without filtering.
8282
return $this->messages;
8383
}

TwigCS/Sniff/AbstractPostParserSniff.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ public function isNodeMatching(Node $node, $type, $name = null)
116116
},
117117
'tag' => function (Node $node, $type, $name) {
118118
return $node->getNodeTag() === $name;
119-
/* && $node->hasAttribute('name') && $name === $node->getAttribute('name') */
120119
},
121120
];
122121

TwigCS/Sniff/AbstractPreParserSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/**
99
* Base for all pre-parser sniff.
1010
*
11-
* A post parser sniff should be useful to check code formatting mainly such as:
11+
* A pre parser sniff should be useful to check code formatting mainly such as:
1212
* whitespaces, too many blank lines or trailing commas;
1313
*
1414
* Use `AbstractPostParserSniff` for higher-order checks.
@@ -24,7 +24,7 @@ public function getType()
2424
}
2525

2626
/**
27-
* Helper method to match a token of a given $type and $value.
27+
* Helper method to match a token of a given type and value.
2828
*
2929
* @param Token $token
3030
* @param int $type

TwigCS/Sniff/AbstractSniff.php

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,6 @@
99
*/
1010
abstract class AbstractSniff implements SniffInterface
1111
{
12-
/**
13-
* Default options for all sniffs.
14-
*
15-
* @var array
16-
*/
17-
protected static $defaultOptions = [];
18-
19-
/**
20-
* Computed options of this sniffs.
21-
*
22-
* @var array
23-
*/
24-
protected $options;
25-
2612
/**
2713
* When process is called, it will fill this report with the potential violations.
2814
*
@@ -35,26 +21,10 @@ abstract class AbstractSniff implements SniffInterface
3521
*/
3622
protected $messages;
3723

38-
/**
39-
* @param array $options Each sniff can defined its options.
40-
*/
41-
public function __construct($options = [])
24+
public function __construct()
4225
{
4326
$this->messages = [];
4427
$this->report = null;
45-
$this->options = array_merge(self::$defaultOptions, $options);
46-
47-
$this->configure();
48-
}
49-
50-
/**
51-
* Configure this sniff based on its options.
52-
*
53-
* @return void
54-
*/
55-
public function configure()
56-
{
57-
// Nothing.
5828
}
5929

6030
/**
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace TwigCS\Sniff\Standard;
4+
5+
use TwigCS\Sniff\AbstractPreParserSniff;
6+
use TwigCS\Token\Token;
7+
8+
/**
9+
* Ensure that files ends with one blank line.
10+
*/
11+
class EnsureBlankAtEOFSniff extends AbstractPreParserSniff
12+
{
13+
/**
14+
* @param Token $token
15+
* @param int $tokenPosition
16+
* @param Token[] $tokens
17+
*
18+
* @return Token
19+
*/
20+
public function process(Token $token, $tokenPosition, $tokens)
21+
{
22+
if ($this->isTokenMatching($token, Token::EOF_TYPE)) {
23+
$i = 0;
24+
while (isset($tokens[$tokenPosition - ($i + 1)])
25+
&& $this->isTokenMatching($tokens[$tokenPosition - ($i + 1)], Token::EOL_TYPE)
26+
) {
27+
++$i;
28+
}
29+
30+
if (1 !== $i) {
31+
// Either 0 or 2+ blank lines.
32+
$this->addMessage(
33+
$this::MESSAGE_TYPE_ERROR,
34+
sprintf('A file must end with 1 blank line; found %d', $i),
35+
$token
36+
);
37+
}
38+
}
39+
40+
return $token;
41+
}
42+
}

TwigCS/Tests/AbstractSniffTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ public function setUp()
4444
/**
4545
* @param string $filename
4646
* @param SniffInterface $sniff
47-
* @param string[] $expects
47+
* @param array $expects
4848
*
4949
* @throws \Exception
5050
*/
51-
protected function checkGenericSniff($filename, SniffInterface $sniff, $expects)
51+
protected function checkGenericSniff($filename, SniffInterface $sniff, array $expects)
5252
{
5353
$file = __DIR__.'/Fixtures/'.$filename;
5454

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class="error">
2+
</div>
3+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace TwigCS\Tests\Sniff;
4+
5+
use TwigCS\Sniff\Standard\EnsureBlankAtEOFSniff;
6+
use TwigCS\Tests\AbstractSniffTest;
7+
8+
/**
9+
* Class EnsureBlankAtEOFTest
10+
*/
11+
class EnsureBlankAtEOFTest extends AbstractSniffTest
12+
{
13+
public function testSniff1()
14+
{
15+
$this->checkGenericSniff('ensureBlankAtEOF.twig', new EnsureBlankAtEOFSniff(), [
16+
[4, 1],
17+
]);
18+
}
19+
}

0 commit comments

Comments
 (0)