Skip to content

Commit 081d20b

Browse files
📦 Refacto Folder
1 parent 7f0152f commit 081d20b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+356
-32
lines changed

SymfonyCustom/Sniffs/FixerHelper.php renamed to SymfonyCustom/Helpers/FixerHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace SymfonyCustom\Sniffs;
5+
namespace SymfonyCustom\Helpers;
66

77
use PHP_CodeSniffer\Files\File;
88

SymfonyCustom/Sniffs/SniffHelper.php renamed to SymfonyCustom/Helpers/SniffHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace SymfonyCustom\Sniffs;
5+
namespace SymfonyCustom\Helpers;
66

77
use PHP_CodeSniffer\Files\File;
88
use PHP_CodeSniffer\Util\Tokens;

SymfonyCustom/Sniffs/Arrays/ArrayDeclarationSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PHP_CodeSniffer\Files\File;
88
use PHP_CodeSniffer\Sniffs\Sniff;
99
use PHP_CodeSniffer\Util\Tokens;
10-
use SymfonyCustom\Sniffs\FixerHelper;
10+
use SymfonyCustom\Helpers\FixerHelper;
1111

1212
/**
1313
* A test to ensure that arrays conform to the array coding standard.

SymfonyCustom/Sniffs/Commenting/DocCommentGroupSameTypeSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
use PHP_CodeSniffer\Files\File;
88
use PHP_CodeSniffer\Sniffs\Sniff;
9-
use SymfonyCustom\Sniffs\FixerHelper;
10-
use SymfonyCustom\Sniffs\SniffHelper;
9+
use SymfonyCustom\Helpers\FixerHelper;
10+
use SymfonyCustom\Helpers\SniffHelper;
1111

1212
/**
1313
* Throws errors if comments are not grouped by type with one blank line between them.

SymfonyCustom/Sniffs/Commenting/DocCommentSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use PHP_CodeSniffer\Files\File;
88
use PHP_CodeSniffer\Sniffs\Sniff;
9-
use SymfonyCustom\Sniffs\FixerHelper;
9+
use SymfonyCustom\Helpers\FixerHelper;
1010

1111
/**
1212
* Ensures doc blocks follow basic formatting.

SymfonyCustom/Sniffs/Namespaces/AlphabeticallySortedUseSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PHP_CodeSniffer\Files\File;
88
use PHP_CodeSniffer\Sniffs\Sniff;
99
use PHP_CodeSniffer\Util\Tokens;
10-
use SymfonyCustom\Sniffs\SniffHelper;
10+
use SymfonyCustom\Helpers\SniffHelper;
1111

1212
/**
1313
* Class AlphabeticallySortedUseSniff

SymfonyCustom/Sniffs/Namespaces/UnusedUseSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PHP_CodeSniffer\Files\File;
88
use PHP_CodeSniffer\Sniffs\Sniff;
99
use PHP_CodeSniffer\Util\Tokens;
10-
use SymfonyCustom\Sniffs\SniffHelper;
10+
use SymfonyCustom\Helpers\SniffHelper;
1111

1212
/**
1313
* Checks for "use" statements that are not needed in a file.

SymfonyCustom/Sniffs/WhiteSpace/DocCommentTagSpacingSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use PHP_CodeSniffer\Files\File;
88
use PHP_CodeSniffer\Sniffs\Sniff;
9-
use SymfonyCustom\Sniffs\SniffHelper;
9+
use SymfonyCustom\Helpers\SniffHelper;
1010

1111
/**
1212
* Checks that there are not 2 empty lines following each other.
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.

TwigCS/tests/AbstractSniffTest.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TwigCS\Tests;
6+
7+
use Exception;
8+
use PHPUnit\Framework\TestCase;
9+
use ReflectionClass;
10+
use TwigCS\Environment\StubbedEnvironment;
11+
use TwigCS\Report\Report;
12+
use TwigCS\Ruleset\Ruleset;
13+
use TwigCS\Runner\Fixer;
14+
use TwigCS\Runner\Linter;
15+
use TwigCS\Sniff\SniffInterface;
16+
use TwigCS\Token\Tokenizer;
17+
18+
/**
19+
* Class AbstractSniffTest
20+
*/
21+
abstract class AbstractSniffTest extends TestCase
22+
{
23+
/**
24+
* Should call $this->checkGenericSniff(new Sniff(), [...]);
25+
*/
26+
abstract public function testSniff(): void;
27+
28+
/**
29+
* @param SniffInterface $sniff
30+
* @param array $expects
31+
*/
32+
protected function checkGenericSniff(SniffInterface $sniff, array $expects): void
33+
{
34+
$env = new StubbedEnvironment();
35+
$tokenizer = new Tokenizer($env);
36+
$linter = new Linter($env, $tokenizer);
37+
$ruleset = new Ruleset();
38+
39+
try {
40+
$class = new ReflectionClass(get_called_class());
41+
$className = $class->getShortName();
42+
$file = __DIR__.'/Fixtures/'.$className.'.twig';
43+
44+
$ruleset->addSniff($sniff);
45+
$report = $linter->run([$file], $ruleset);
46+
} catch (Exception $e) {
47+
$this->fail($e->getMessage());
48+
49+
return;
50+
}
51+
52+
$messages = $report->getMessages();
53+
$messagePositions = [];
54+
55+
foreach ($messages as $message) {
56+
if (Report::MESSAGE_TYPE_FATAL === $message->getLevel()) {
57+
$errorMessage = $message->getMessage();
58+
$line = $message->getLine();
59+
60+
if (null !== $line) {
61+
$errorMessage = sprintf('Line %s: %s', $line, $errorMessage);
62+
}
63+
$this->fail($errorMessage);
64+
}
65+
66+
$messagePositions[] = [$message->getLine() => $message->getLinePosition()];
67+
}
68+
$this->assertEquals($expects, $messagePositions);
69+
70+
$fixedFile = __DIR__.'/Fixtures/'.$className.'.fixed.twig';
71+
if (file_exists($fixedFile)) {
72+
$fixer = new Fixer($ruleset, $tokenizer);
73+
$sniff->enableFixer($fixer);
74+
$fixer->fixFile($file);
75+
76+
$diff = $fixer->generateDiff($fixedFile);
77+
if ('' !== $diff) {
78+
$this->fail($diff);
79+
}
80+
}
81+
}
82+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<div class="error">
2+
</div>
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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{{ foo }}
2+
{# comment #}
3+
{% if foo %}{% endif %}
4+
5+
{{- foo -}}
6+
{#- comment -#}
7+
{%- if foo -%}{%- endif -%}
8+
9+
{{
10+
shouldNotCareAboutNewLine
11+
}}
12+
{%- if foo -%}{%- endif -%}
13+
14+
{{ foo({'bar': {'baz': 'shouldNotCareAboutDoubleHashes'}}) }}
15+
16+
<div>
17+
{{
18+
shouldNotCareAboutNewLine
19+
}}
20+
</div>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{{ foo }}
2+
{# comment #}
3+
{% if foo %}{% endif %}
4+
5+
{{- foo -}}
6+
{#- comment -#}
7+
{%- if foo -%}{%- endif -%}
8+
9+
{{
10+
shouldNotCareAboutNewLine
11+
}}
12+
{%-if foo -%}{%- endif-%}
13+
14+
{{ foo({'bar': {'baz': 'shouldNotCareAboutDoubleHashes'}}) }}
15+
16+
<div>
17+
{{
18+
shouldNotCareAboutNewLine
19+
}}
20+
</div>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div class="error">
2+
{# {% set var = 'This is a comment' %} #}
3+
</div>
4+
5+
<div class="success">
6+
{# 'This is a comment' #}
7+
</div>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class="error">
2+
3+
</div>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div class="error">
2+
3+
4+
</div>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{{ 1 + 2 }}
2+
{{ 1 - 2 }}
3+
{{ 1 / 2 }}
4+
{{ 1 * 2 }}
5+
{{ 1 % 2 }}
6+
{{ 1 // 2 }}
7+
{{ 1 ** 2 }}
8+
{{ foo ~ bar }}
9+
{{ true ? true : false }}
10+
{{ 1 == 2 }}
11+
{{ not true }}
12+
{{ false and true }}
13+
{{ false or true }}
14+
{{ a in array }}
15+
{{ a is array }}
16+
17+
Untouch +-/*%==:
18+
19+
{{ [1, 2, 3] }}
20+
{{ {'foo': 'bar'} }}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{{ 1+2 }}
2+
{{ 1-2 }}
3+
{{ 1/2 }}
4+
{{ 1*2 }}
5+
{{ 1%2 }}
6+
{{ 1//2 }}
7+
{{ 1**2 }}
8+
{{ foo~bar }}
9+
{{ true ? true : false }}
10+
{{ 1==2 }}
11+
{{ not true }}
12+
{{ false and true }}
13+
{{ false or true }}
14+
{{ a in array }}
15+
{{ a is array }}
16+
17+
Untouch +-/*%==:
18+
19+
{{ [1, 2, 3] }}
20+
{{ {'foo': 'bar'} }}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TwigCS\Tests\Ruleset\Generic;
6+
7+
use TwigCS\Ruleset\Generic\BlankEOFSniff;
8+
use TwigCS\Tests\AbstractSniffTest;
9+
10+
/**
11+
* Class BlankEOFTest
12+
*/
13+
class BlankEOFTest extends AbstractSniffTest
14+
{
15+
public function testSniff(): void
16+
{
17+
$this->checkGenericSniff(new BlankEOFSniff(), [
18+
[4 => 1],
19+
]);
20+
}
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TwigCS\Tests\Ruleset\Generic;
6+
7+
use TwigCS\Ruleset\Generic\DelimiterSpacingSniff;
8+
use TwigCS\Tests\AbstractSniffTest;
9+
10+
/**
11+
* Class DelimiterSpacingTest
12+
*/
13+
class DelimiterSpacingTest extends AbstractSniffTest
14+
{
15+
public function testSniff(): void
16+
{
17+
$this->checkGenericSniff(new DelimiterSpacingSniff(), [
18+
[12 => 1],
19+
[12 => 12],
20+
[12 => 15],
21+
[12 => 25],
22+
]);
23+
}
24+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TwigCS\Tests\Ruleset\Generic;
6+
7+
use TwigCS\Ruleset\Generic\DisallowCommentedCodeSniff;
8+
use TwigCS\Tests\AbstractSniffTest;
9+
10+
/**
11+
* Class DisallowCommentedCodeTest
12+
*/
13+
class DisallowCommentedCodeTest extends AbstractSniffTest
14+
{
15+
public function testSniff(): void
16+
{
17+
$this->checkGenericSniff(new DisallowCommentedCodeSniff(), [
18+
[2 => 5],
19+
]);
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TwigCS\Tests\Ruleset\Generic;
6+
7+
use TwigCS\Ruleset\Generic\EmptyLinesSniff;
8+
use TwigCS\Tests\AbstractSniffTest;
9+
10+
/**
11+
* Class EmptyLinesTest
12+
*/
13+
class EmptyLinesTest extends AbstractSniffTest
14+
{
15+
public function testSniff(): void
16+
{
17+
$this->checkGenericSniff(new EmptyLinesSniff(), [
18+
[3 => 1],
19+
]);
20+
}
21+
}

0 commit comments

Comments
 (0)