Skip to content

Commit

Permalink
Add max_length option
Browse files Browse the repository at this point in the history
  • Loading branch information
gordinskiy committed Nov 28, 2023
1 parent c9281fe commit 28a87f6
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions src/Rules/LineLengthLimit.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,41 @@

namespace Gordinskiy\LineLengthChecker\Rules;

use PhpCsFixer\Fixer\FixerInterface;
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

class LineLengthLimit implements FixerInterface
class LineLengthLimit implements ConfigurableFixerInterface
{
private int $maxLength = 120;

/** @var string */
private const OPTION_MAX_LENGTH = 'max_length';

public function getConfigurationDefinition(): FixerConfigurationResolverInterface
{
return new FixerConfigurationResolver([
(new FixerOptionBuilder(self::OPTION_MAX_LENGTH, 'Maximum line length.'))
->setAllowedTypes(['int'])
->setDefault(120)
->getOption(),
]);
}

public function configure(array $configuration): void
{
$configuration = $this->getConfigurationDefinition()->resolve($configuration);

if ($maxLength = $configuration[self::OPTION_MAX_LENGTH] ?? null) {
$this->maxLength = $maxLength;
}
}

public function getName(): string
{
return 'Gordinskiy/line_length_limit';
Expand Down Expand Up @@ -56,12 +83,12 @@ public function isCandidate(Tokens $tokens): bool
$previousLineEnd = array_shift($lines);
$nextLineBegin = array_pop($lines);

if (!self::isValidLength($lineLength + strlen($previousLineEnd))) {
if (!$this->isValidLength($lineLength + strlen($previousLineEnd))) {
return true;
}

foreach ($lines as $line) {
if (!self::isValidLength(strlen($line))) {
if (!$this->isValidLength(strlen($line))) {
return true;
}
}
Expand All @@ -75,9 +102,9 @@ public function isCandidate(Tokens $tokens): bool
return false;
}

private static function isValidLength(int $length): bool
private function isValidLength(int $length): bool
{
return $length <= 120;
return $length <= $this->maxLength;
}

public function fix(\SplFileInfo $file, Tokens $tokens): void
Expand All @@ -97,7 +124,7 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
if ($lineBreaksCount === 0) {
$lineLength += strlen($tokenContent);
} elseif ($lineBreaksCount === 1) {
if (!self::isValidLength($lineLength)) {
if (!$this->isValidLength($lineLength)) {
$tokens->insertAt($index, [
new Token([T_WHITESPACE, ' ']),
new Token([T_COMMENT, '# Line too long'])
Expand All @@ -112,13 +139,13 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
$previousLineEnd = array_shift($lines);
$nextLineBegin = array_pop($lines);

if (!self::isValidLength($lineLength + strlen($previousLineEnd))) {
if (!$this->isValidLength($lineLength + strlen($previousLineEnd))) {
$buffer[0] = $buffer[0] . ' # Line too long';
$mustBeRebuild = true;
}

foreach ($lines as $key => $line) {
if (!self::isValidLength(strlen($line))) {
if (!$this->isValidLength(strlen($line))) {
$buffer[$key + 1] .= ' # Line too long';
$mustBeRebuild = true;
}
Expand Down

0 comments on commit 28a87f6

Please sign in to comment.