-
Notifications
You must be signed in to change notification settings - Fork 9
OPSRC-505 Support for Twigcs #13
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
Open
JakubWegner
wants to merge
14
commits into
BitBagCommerce:main
Choose a base branch
from
JakubWegner:OPSRC-505
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b58a011
Add twigcs support and basic ruleset
JakubWegner 4bdd781
Add quote html tag attribute rule
JakubWegner f39948c
Add no space between html tag attributes rule
JakubWegner 178ffc5
Add no newline at the end of the file rule
JakubWegner d3860f3
Add line length rule
JakubWegner f071a8b
Add multiple empty line rule
JakubWegner 06c7575
Better error messages
JakubWegner c77b881
Add new line after twig set rule
JakubWegner 5b4212b
Add apostrophe in twig rule, fix bug with twigcs tokens value
JakubWegner 639093b
Add twigcs conf to readme
JakubWegner c6dbbcb
Clean code
JakubWegner 2816b95
Fix readme
JakubWegner 6d4cde0
Clean code after review
JakubWegner 1ed025d
Add integration tests for Twigcs custom rules
JakubWegner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,19 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
|
|
||
| <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/8.5/phpunit.xsd" | ||
| backupGlobals="false" | ||
| colors="true" | ||
| bootstrap="vendor/autoload.php"> | ||
| <testsuites> | ||
| <testsuite name="BitBag CodingStandard Integration Test Suite"> | ||
| <directory>tests/Integration</directory> | ||
| </testsuite> | ||
| </testsuites> | ||
|
|
||
| <php> | ||
| <ini name="error_reporting" value="-1" /> | ||
| <env name="APP_ENV" value="test"/> | ||
| <env name="SHELL_VERBOSITY" value="-1" /> | ||
| </php> | ||
| </phpunit> |
This file contains hidden or 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,60 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file has been created by developers from BitBag. | ||
| * Feel free to contact us once you face any issues or want to start | ||
| * You can find more information about us on https://bitbag.io and write us | ||
| * an email on hello@bitbag.io. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace BitBag\CodingStandard\Twigcs\Dto; | ||
|
|
||
| final class HtmlTagDto | ||
| { | ||
| /** @var string */ | ||
| private $tag = ''; | ||
|
|
||
| /** @var string */ | ||
| private $html = ''; | ||
|
|
||
| /** @var int */ | ||
| private $offset = 0; | ||
|
|
||
| public function getTag(): string | ||
| { | ||
| return $this->tag; | ||
| } | ||
|
|
||
| public function setTag(string $tag): self | ||
| { | ||
| $this->tag = $tag; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| public function getHtml(): string | ||
| { | ||
| return $this->html; | ||
| } | ||
|
|
||
| public function setHtml(string $html): self | ||
| { | ||
| $this->html = $html; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| public function getOffset(): int | ||
| { | ||
| return $this->offset; | ||
| } | ||
|
|
||
| public function setOffset(int $offset): self | ||
| { | ||
| $this->offset = $offset; | ||
|
|
||
| return $this; | ||
| } | ||
| } |
This file contains hidden or 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,45 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file has been created by developers from BitBag. | ||
| * Feel free to contact us once you face any issues or want to start | ||
| * You can find more information about us on https://bitbag.io and write us | ||
| * an email on hello@bitbag.io. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace BitBag\CodingStandard\Twigcs\Dto; | ||
|
|
||
| final class OffsetDto | ||
| { | ||
| /** @var int */ | ||
| private $line = 0; | ||
|
|
||
| /** @var int */ | ||
| private $column = 0; | ||
|
|
||
| public function getLine(): int | ||
| { | ||
| return $this->line; | ||
| } | ||
|
|
||
| public function setLine(int $line): self | ||
| { | ||
| $this->line = $line; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| public function getColumn(): int | ||
| { | ||
| return $this->column; | ||
| } | ||
|
|
||
| public function setColumn(int $column): self | ||
| { | ||
| $this->column = $column; | ||
|
|
||
| return $this; | ||
| } | ||
| } |
This file contains hidden or 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,73 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file has been created by developers from BitBag. | ||
| * Feel free to contact us once you face any issues or want to start | ||
| * You can find more information about us on https://bitbag.io and write us | ||
| * an email on hello@bitbag.io. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace BitBag\CodingStandard\Twigcs\Rule; | ||
|
|
||
| use BitBag\CodingStandard\Twigcs\Ruleset\Ruleset; | ||
| use BitBag\CodingStandard\Twigcs\Util\HtmlUtil; | ||
| use FriendsOfTwig\Twigcs\Rule\AbstractRule; | ||
| use FriendsOfTwig\Twigcs\Rule\RuleInterface; | ||
| use FriendsOfTwig\Twigcs\TwigPort\TokenStream; | ||
| use FriendsOfTwig\Twigcs\Validator\Violation; | ||
|
|
||
| final class EmptyLinesRule extends AbstractRule implements RuleInterface | ||
| { | ||
| /** @var string */ | ||
| private $pattern = '#(?<offset>\n{3,})#s'; | ||
|
|
||
| /** @var HtmlUtil */ | ||
| private $htmlUtil; | ||
|
|
||
| /** @var int */ | ||
| private $lineNumberOffset = 2; | ||
|
|
||
| public function __construct(int $severity, HtmlUtil $htmlUtil) | ||
| { | ||
| parent::__construct($severity); | ||
|
|
||
| $this->htmlUtil = $htmlUtil; | ||
| } | ||
|
|
||
| /** | ||
| * @return Violation[] | ||
| */ | ||
| public function check(TokenStream $tokens): array | ||
| { | ||
| $violations = []; | ||
|
|
||
| $content = str_replace("\r", '', $tokens->getSourceContext()->getCode()); | ||
| $this->htmlUtil->stripUnnecessaryTagsAndSavePositions($content); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential SRP break |
||
|
|
||
| foreach ($this->getMultiLines($content) as $multiline) { | ||
| if ($this->htmlUtil->isInsideUnnecessaryTag($multiline['offset'][1])) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the |
||
| continue; | ||
| } | ||
|
|
||
| $offset = $this->htmlUtil->getTwigcsOffset($content, $multiline['offset'][1] + $this->lineNumberOffset); | ||
|
|
||
| $violations[] = $this->createViolation( | ||
| $tokens->getSourceContext()->getPath(), | ||
| $offset->getLine(), | ||
| 0, | ||
| Ruleset::ERROR_MULTIPLE_EMPTY_LINES | ||
| ); | ||
| } | ||
|
|
||
| return $violations; | ||
| } | ||
|
|
||
| private function getMultiLines(string $content): array | ||
| { | ||
| return preg_match_all($this->pattern, $content, $multilines, HtmlUtil::REGEX_FLAGS) | ||
| ? $multilines | ||
| : []; | ||
| } | ||
| } | ||
This file contains hidden or 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,66 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file has been created by developers from BitBag. | ||
| * Feel free to contact us once you face any issues or want to start | ||
| * You can find more information about us on https://bitbag.io and write us | ||
| * an email on hello@bitbag.io. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace BitBag\CodingStandard\Twigcs\Rule\Html; | ||
|
|
||
| use BitBag\CodingStandard\Twigcs\Ruleset\Ruleset; | ||
| use BitBag\CodingStandard\Twigcs\Util\HtmlUtil; | ||
| use FriendsOfTwig\Twigcs\Rule\AbstractRule; | ||
| use FriendsOfTwig\Twigcs\Rule\RuleInterface; | ||
| use FriendsOfTwig\Twigcs\TwigPort\TokenStream; | ||
| use FriendsOfTwig\Twigcs\Validator\Violation; | ||
|
|
||
| final class ApostropheInAttributesRule extends AbstractRule implements RuleInterface | ||
| { | ||
| /** @var string */ | ||
| private $pattern = "#=\s*(?<offset>')#"; | ||
|
|
||
| /** @var HtmlUtil */ | ||
| private $htmlUtil; | ||
|
|
||
| public function __construct(int $severity, HtmlUtil $htmlUtil) | ||
| { | ||
| parent::__construct($severity); | ||
|
|
||
| $this->htmlUtil = $htmlUtil; | ||
| } | ||
|
|
||
| /** | ||
| * @return Violation[] | ||
| */ | ||
| public function check(TokenStream $tokens): array | ||
| { | ||
| $violations = []; | ||
| $content = $this->htmlUtil->stripUnnecessaryTagsAndSavePositions($tokens->getSourceContext()->getCode()); | ||
|
|
||
| foreach ($this->htmlUtil->getParsedHtmlTags($content) as $tag) { | ||
| foreach ($this->getApostrophes($tag->getHtml()) as $apostrophe) { | ||
| $offset = $this->htmlUtil->getTwigcsOffset($content, $tag->getOffset() + $apostrophe['offset'][1]); | ||
|
|
||
| $violations[] = $this->createViolation( | ||
| $tokens->getSourceContext()->getPath(), | ||
| $offset->getLine(), | ||
| $offset->getColumn(), | ||
| sprintf(Ruleset::ERROR_APOSTROPHE_IN_ATTRIBUTE, $tag->getTag()) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return $violations; | ||
| } | ||
|
|
||
| private function getApostrophes(string $html): array | ||
| { | ||
| return preg_match_all($this->pattern, $html, $quotes, HtmlUtil::REGEX_FLAGS) | ||
| ? $quotes | ||
| : []; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the "and" in method tells us, it's not SRP friendly ;)