-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rule about file double extension (.html.twig) (#173)
- Loading branch information
Showing
10 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,3 +43,4 @@ | |
- Twig | ||
- DirectoryNameRule | ||
- FileNameRule | ||
- FileExtensionRule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TwigCsFixer\Rules\File; | ||
|
||
use TwigCsFixer\File\FileHelper; | ||
use TwigCsFixer\Rules\AbstractRule; | ||
|
||
/** | ||
* Ensures file name uses two extensions (e.g. index.html.twig). | ||
*/ | ||
final class FileExtensionRule extends AbstractRule | ||
{ | ||
protected function process(int $tokenPosition, array $tokens): void | ||
{ | ||
if (0 !== $tokenPosition) { | ||
return; | ||
} | ||
|
||
$token = $tokens[$tokenPosition]; | ||
$fileName = FileHelper::getFileName($token->getFilename()); | ||
$fileParts = explode('.', $fileName ?? ''); | ||
|
||
$fileExtension = array_pop($fileParts); | ||
if ('twig' !== $fileExtension) { | ||
return; | ||
} | ||
|
||
$formatExtension = array_pop($fileParts); | ||
if ([] === $fileParts || null === $formatExtension) { | ||
$this->addFileError( | ||
sprintf('The file must use two extensions; found ".%s".', $fileExtension), | ||
$token, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Nothing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Nothing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rules\File\FileExtension; | ||
|
||
use TwigCsFixer\Rules\File\FileExtensionRule; | ||
use TwigCsFixer\Tests\Rules\AbstractRuleTestCase; | ||
|
||
final class FileExtensionRuleTest extends AbstractRuleTestCase | ||
{ | ||
public function testRule(): void | ||
{ | ||
$this->checkRule(new FileExtensionRule(), ['FileExtension.Error']); | ||
} | ||
|
||
public function testRuleIgnoredFile(): void | ||
{ | ||
$this->checkRule(new FileExtensionRule(), [], __DIR__.'/FileExtensionRuleTest.php'); | ||
} | ||
|
||
public function testRuleValidFile(): void | ||
{ | ||
$this->checkRule(new FileExtensionRule(), [], __DIR__.'/file_extension_rule_test.html.twig'); | ||
} | ||
|
||
public function testRuleValidDotFile(): void | ||
{ | ||
$this->checkRule(new FileExtensionRule(), [], __DIR__.'/.dotfile.twig'); | ||
} | ||
|
||
public function testRuleValidDotFileWithFormatExtension(): void | ||
{ | ||
$this->checkRule(new FileExtensionRule(), [], __DIR__.'/.dotfile.html.twig'); | ||
} | ||
|
||
public function testRuleMissingFormatExtension(): void | ||
{ | ||
$this->checkRule(new FileExtensionRule(), ['FileExtension.Error'], __DIR__.'/file_extension_rule_test_missing.twig'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Nothing |
1 change: 1 addition & 0 deletions
1
tests/Rules/File/FileExtension/file_extension_rule_test.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Nothing |
1 change: 1 addition & 0 deletions
1
tests/Rules/File/FileExtension/file_extension_rule_test_missing.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Nothing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters