Skip to content

Commit

Permalink
[TASK] Refactor validate method of rules
Browse files Browse the repository at this point in the history
Resolves: #21
  • Loading branch information
a-r-m-i-n committed Dec 3, 2023
1 parent 3e0a216 commit fd4c75c
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 35 deletions.
7 changes: 2 additions & 5 deletions src/EditorConfig/Rules/File/CharsetRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,17 @@ public function __construct(string $filePath, string $fileContent, string $expec
parent::__construct($filePath, $fileContent);
}

protected function validate(string $content): bool
protected function validate(string $content): void
{
$actualEncoding = FileEncodingUtility::detect($content);
$result = $this->expectedEncoding === $actualEncoding;
if (!$result) {
if ($this->expectedEncoding !== $actualEncoding) {
$this->addError(
null,
'This file has invalid encoding given! Expected: "%s", Given: "%s"',
$this->expectedEncoding,
$actualEncoding
);
}

return $result;
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/EditorConfig/Rules/File/EndOfLineRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function getEndOfLine(): string
return $this->expectedEndOfLine;
}

protected function validate(string $content): bool
protected function validate(string $content): void
{
$whitespacesOnly = (string)preg_replace('/[^\r\n]/i', '', $content);

Expand All @@ -53,8 +53,6 @@ protected function validate(string $content): bool
$this->endOfLine
);
}

return $result;
}

public function fixContent(string $content): string
Expand Down
9 changes: 3 additions & 6 deletions src/EditorConfig/Rules/File/InsertFinalNewLineRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,16 @@ public function __construct(string $filePath, string $fileContent, ?string $newL
parent::__construct($filePath, $fileContent);
}

protected function validate(string $content): bool
protected function validate(string $content): void
{
if ('' === $content) {
return true;
return;
}

$lastChar = substr($content, -1);
$result = in_array($lastChar, ["\r", "\n"], true);
if (!$result) {
if (!in_array($lastChar, ["\r", "\n"], true)) {
$this->addError(null, 'This file has no final new line given');
}

return $result;
}

public function fixContent(string $content): string
Expand Down
8 changes: 2 additions & 6 deletions src/EditorConfig/Rules/File/TrimTrailingWhitespaceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public function __construct(string $filePath, string $fileContent, bool $insertF
parent::__construct($filePath, $fileContent);
}

protected function validate(string $content): bool
protected function validate(string $content): void
{
$trim = rtrim($content);
if ('' === $content) {
return true;
return;
}
if ($this->insertFinalNewLine) {
$insertFinalNewLineRule = new InsertFinalNewLineRule($this->filePath, $content);
Expand All @@ -34,11 +34,7 @@ protected function validate(string $content): bool
}
if ($content !== $trim) {
$this->addError(null, 'This file has trailing whitespaces');

return false;
}

return true;
}

public function fixContent(string $content): string
Expand Down
7 changes: 1 addition & 6 deletions src/EditorConfig/Rules/Line/IndentionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(string $filePath, string $fileContent, string $style
parent::__construct($filePath, $fileContent);
}

protected function validate(string $content): bool
protected function validate(string $content): void
{
$lineEnding = LineEndingUtility::detectLineEnding($content, false);
if (empty($lineEnding)) {
Expand All @@ -44,7 +44,6 @@ protected function validate(string $content): bool
$lines = explode($lineEnding, $content);

$lineCount = 0;
$isValid = true;
foreach ($lines as $line) {
++$lineCount;
$lineValid = true;
Expand All @@ -61,12 +60,10 @@ protected function validate(string $content): bool

if ('tab' === $this->style && $whitespaces !== $beginningWhitespaces) {
$this->addError($lineCount, 'Expected indention style "tab" but found "spaces"');
$isValid = false;
$lineValid = false;
}
if ('space' === $this->style && false !== strpos($whitespaces, "\t")) {
$this->addError($lineCount, 'Expected indention style "space" but found "tabs"');
$isValid = false;
$lineValid = false;
}

Expand All @@ -79,8 +76,6 @@ protected function validate(string $content): bool
}
}
}

return $isValid;
}

public function fixContent(string $content): string
Expand Down
5 changes: 1 addition & 4 deletions src/EditorConfig/Rules/Line/MaxLineLengthRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(string $filePath, string $fileContent = null, int $m
parent::__construct($filePath, $fileContent);
}

protected function validate(string $content): bool
protected function validate(string $content): void
{
$lineEnding = LineEndingUtility::detectLineEnding($content, false);
if (empty($lineEnding)) {
Expand All @@ -31,16 +31,13 @@ protected function validate(string $content): bool
$lines = explode($lineEnding, $content);

$lineCount = 0;
$isValid = true;
foreach ($lines as $line) {
++$lineCount;
$lineLength = strlen($line);
if ($lineLength > $this->maxLineLength) {
$this->addError($lineCount, 'Max line length (%d chars) exceeded by %d chars', $this->maxLineLength, $lineLength);
}
}

return $isValid;
}

public function fixContent(string $content): string
Expand Down
5 changes: 1 addition & 4 deletions src/EditorConfig/Rules/Line/TrimTrailingWhitespaceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class TrimTrailingWhitespaceRule extends Rule
{
protected function validate(string $content): bool
protected function validate(string $content): void
{
$lineEnding = LineEndingUtility::detectLineEnding($content, false);
if (empty($lineEnding)) {
Expand All @@ -19,7 +19,6 @@ protected function validate(string $content): bool
$lines = explode($lineEnding, $content);

$lineCount = 0;
$isValid = true;
foreach ($lines as $line) {
++$lineCount;

Expand All @@ -28,8 +27,6 @@ protected function validate(string $content): bool
$this->addError($lineCount, 'Trailing whitespaces found');
}
}

return $isValid;
}

public function fixContent(string $content): string
Expand Down
2 changes: 1 addition & 1 deletion src/EditorConfig/Rules/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __construct(string $filePath, string $fileContent = null)
$this->validate($fileContent ?? (string)file_get_contents($this->filePath));
}

abstract protected function validate(string $content): bool;
abstract protected function validate(string $content): void;

public function getFilePath(): string
{
Expand Down

0 comments on commit fd4c75c

Please sign in to comment.