Skip to content

Commit

Permalink
feat: add inline-attributes option to PedroTroller/line_break_between…
Browse files Browse the repository at this point in the history
…_method_arguments
  • Loading branch information
PedroTroller committed Jan 5, 2023
1 parent 8376adf commit b25f454
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 4 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,9 @@ If the declaration of a method is too long, the arguments of this method MUST BE
- `automatic-argument-merge` (*optional*): If both conditions are met (the line is not too long and there are not too many arguments), then the arguments are put back inline
- default: `true`

- `inline-attributes` (*optional*): In the case of a split, the declaration of the attributes of the arguments of the method will be on the same line as the arguments themselves
- default: `false`

### Configuration examples

```php
Expand All @@ -531,7 +534,7 @@ $config = new PhpCsFixer\Config();
$config->setRules(
[
// ...
'PedroTroller/line_break_between_method_arguments' => [ 'max-args' => 4, 'max-length' => 120, 'automatic-argument-merge' => true ],
'PedroTroller/line_break_between_method_arguments' => [ 'max-args' => 4, 'max-length' => 120, 'automatic-argument-merge' => true, 'inline-attributes' => true ],
// ...
]
);
Expand All @@ -549,7 +552,7 @@ $config = new PhpCsFixer\Config();
// ...
$config->setRules(
PedroTroller\CS\Fixer\RuleSetFactory::create()
->enable('PedroTroller/line_break_between_method_arguments', [ 'max-args' => 4, 'max-length' => 120, 'automatic-argument-merge' => true ])
->enable('PedroTroller/line_break_between_method_arguments', [ 'max-args' => 4, 'max-length' => 120, 'automatic-argument-merge' => true, 'inline-attributes' => true ])
->getRules()
);
$config->registerCustomFixers(new PedroTroller\CS\Fixer\Fixers());
Expand Down Expand Up @@ -601,7 +604,7 @@ $config = new PhpCsFixer\Config();
$config->setRules(
[
// ...
'PedroTroller/line_break_between_method_arguments' => [ 'max-args' => false, 'max-length' => 120, 'automatic-argument-merge' => true ],
'PedroTroller/line_break_between_method_arguments' => [ 'max-args' => false, 'max-length' => 120, 'automatic-argument-merge' => true, 'inline-attributes' => true ],
// ...
]
);
Expand All @@ -619,7 +622,7 @@ $config = new PhpCsFixer\Config();
// ...
$config->setRules(
PedroTroller\CS\Fixer\RuleSetFactory::create()
->enable('PedroTroller/line_break_between_method_arguments', [ 'max-args' => false, 'max-length' => 120, 'automatic-argument-merge' => true ])
->enable('PedroTroller/line_break_between_method_arguments', [ 'max-args' => false, 'max-length' => 120, 'automatic-argument-merge' => true, 'inline-attributes' => true ])
->getRules()
);
$config->registerCustomFixers(new PedroTroller\CS\Fixer\Fixers());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ public function getSampleConfigurations(): array
'max-args' => 4,
'max-length' => 120,
'automatic-argument-merge' => true,
'inline-attributes' => true,
],
[
'max-args' => false,
'max-length' => 120,
'automatic-argument-merge' => true,
'inline-attributes' => true,
],
];
}
Expand Down Expand Up @@ -87,6 +89,9 @@ public function getConfigurationDefinition(): FixerConfigurationResolverInterfac
(new FixerOptionBuilder('automatic-argument-merge', 'If both conditions are met (the line is not too long and there are not too many arguments), then the arguments are put back inline'))
->setDefault(true)
->getOption(),
(new FixerOptionBuilder('inline-attributes', 'In the case of a split, the declaration of the attributes of the arguments of the method will be on the same line as the arguments themselves'))
->setDefault(false)
->getOption(),
]);
}

Expand Down Expand Up @@ -188,6 +193,12 @@ private function splitArgs(Tokens $tokens, $index): void
if ($tokens[$i]->equals(',')) {
$linebreaks[] = $i;
}

if (false === $this->configuration['inline-attributes'] && $tokens[$i]->isGivenKind(T_ATTRIBUTE)) {
$i = $this->analyze($tokens)->getClosingAttribute($i);

$linebreaks[] = $i;
}
}

sort($linebreaks);
Expand Down
28 changes: 28 additions & 0 deletions src/PedroTroller/CS/Fixer/TokensAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,34 @@ public function getClosingCurlyBracket($index)
}
}

/**
* @param int $index
*
* @return null|int
*/
public function getClosingAttribute($index)
{
if (false === $this->tokens[$index]->isGivenKind(T_ATTRIBUTE)) {
throw new Exception(sprintf('Expected token: T_ATTRIBUTE Token %d id contains %s.', $index, $this->tokens[$index]->getContent()));
}

for ($i = $index + 1; $i < $this->tokens->count(); ++$i) {
if ($this->tokens[$i]->isGivenKind(T_ATTRIBUTE)) {
$i = $this->getClosingAttribute($i);

if (null === $i) {
return null;
}

continue;
}

if ($this->tokens[$i]->isGivenKind(CT::T_ATTRIBUTE_CLOSE)) {
return $i;
}
}
}

/**
* @param int $index
*
Expand Down
98 changes: 98 additions & 0 deletions tests/UseCase/LineBreakBetweenMethods/Regression/Case7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

namespace tests\UseCase\LineBreakBetweenMethods\Regression;

use PedroTroller\CS\Fixer\CodingStyle\LineBreakBetweenMethodArgumentsFixer;
use tests\UseCase;

/**
* https://github.com/PedroTroller/PhpCSFixer-Custom-Fixers/issues/169.
*/
final class Case7 implements UseCase
{
public function getFixers(): iterable
{
$fixer = new LineBreakBetweenMethodArgumentsFixer();

$fixer->configure([
'inline-attributes' => false,
]);

yield $fixer;
}

public function getRawScript(): string
{
return <<<'PHP'
<?php
use Doctrine\Orm\Mapping as ORM;
class Foo {
public function __construct(
#[ORM\Id]
#[ORM\Column(type: 'uuid')]
private readonly UuidInterface $id,
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: false)]
private readonly AuthUser $authUser,
#[ORM\Column(length: 40, nullable: false)]
private readonly string $accessToken,
#[ORM\Column(length: 256, nullable: false)]
private readonly string $refreshToken,
#[ORM\Column(length: 255, nullable: false)]
private readonly string $deviceUserAgent,
#[ORM\Column(length: 100)]
private readonly string $deviceType,
#[ORM\Column(length: 100)]
private readonly string $deviceOs,
#[ORM\Column(length: 100)]
private readonly string $deviceBrowser
) {
}
}
PHP;
}

public function getExpectation(): string
{
return <<<'PHP'
<?php
use Doctrine\Orm\Mapping as ORM;
class Foo {
public function __construct(
#[ORM\Id]
#[ORM\Column(type: 'uuid')]
private readonly UuidInterface $id,
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: false)]
private readonly AuthUser $authUser,
#[ORM\Column(length: 40, nullable: false)]
private readonly string $accessToken,
#[ORM\Column(length: 256, nullable: false)]
private readonly string $refreshToken,
#[ORM\Column(length: 255, nullable: false)]
private readonly string $deviceUserAgent,
#[ORM\Column(length: 100)]
private readonly string $deviceType,
#[ORM\Column(length: 100)]
private readonly string $deviceOs,
#[ORM\Column(length: 100)]
private readonly string $deviceBrowser
) {
}
}
PHP;
}

public function getMinSupportedPhpVersion(): int
{
return 80100;
}
}
88 changes: 88 additions & 0 deletions tests/UseCase/LineBreakBetweenMethods/Regression/Case8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

namespace tests\UseCase\LineBreakBetweenMethods\Regression;

use PedroTroller\CS\Fixer\CodingStyle\LineBreakBetweenMethodArgumentsFixer;
use tests\UseCase;

/**
* https://github.com/PedroTroller/PhpCSFixer-Custom-Fixers/issues/169.
*/
final class Case8 implements UseCase
{
public function getFixers(): iterable
{
$fixer = new LineBreakBetweenMethodArgumentsFixer();

$fixer->configure([
'inline-attributes' => true,
]);

yield $fixer;
}

public function getRawScript(): string
{
return <<<'PHP'
<?php
use Doctrine\Orm\Mapping as ORM;
class Foo {
public function __construct(
#[ORM\Id]
#[ORM\Column(type: 'uuid')]
private readonly UuidInterface $id,
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: false)]
private readonly AuthUser $authUser,
#[ORM\Column(length: 40, nullable: false)]
private readonly string $accessToken,
#[ORM\Column(length: 256, nullable: false)]
private readonly string $refreshToken,
#[ORM\Column(length: 255, nullable: false)]
private readonly string $deviceUserAgent,
#[ORM\Column(length: 100)]
private readonly string $deviceType,
#[ORM\Column(length: 100)]
private readonly string $deviceOs,
#[ORM\Column(length: 100)]
private readonly string $deviceBrowser
) {
}
}
PHP;
}

public function getExpectation(): string
{
return <<<'PHP'
<?php
use Doctrine\Orm\Mapping as ORM;
class Foo {
public function __construct(
#[ORM\Id] #[ORM\Column(type: 'uuid')] private readonly UuidInterface $id,
#[ORM\ManyToOne] #[ORM\JoinColumn(nullable: false)] private readonly AuthUser $authUser,
#[ORM\Column(length: 40, nullable: false)] private readonly string $accessToken,
#[ORM\Column(length: 256, nullable: false)] private readonly string $refreshToken,
#[ORM\Column(length: 255, nullable: false)] private readonly string $deviceUserAgent,
#[ORM\Column(length: 100)] private readonly string $deviceType,
#[ORM\Column(length: 100)] private readonly string $deviceOs,
#[ORM\Column(length: 100)] private readonly string $deviceBrowser
) {
}
}
PHP;
}

public function getMinSupportedPhpVersion(): int
{
return 80100;
}
}

0 comments on commit b25f454

Please sign in to comment.