Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ parameters:
checkMissingCallableSignature: true
paths:
- src
- tests
182 changes: 182 additions & 0 deletions src/lib/PhpCsFixer/Rule/MultilineParametersFixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\CodeStyle\PhpCsFixer\Rule;

use PhpCsFixer\AbstractFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

final class MultilineParametersFixer extends AbstractFixer
{
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'Methods and functions with 2+ parameters must use multiline format',
[
new CodeSample(
'<?php function foo(string $a, int $b): void {}'
),
]
);
}

public function isCandidate(Tokens $tokens): bool
{
return $tokens->isTokenKindFound(T_FUNCTION);
}

protected function applyFix(
\SplFileInfo $file,
Tokens $tokens
): void {
for ($index = $tokens->count() - 1; $index >= 0; --$index) {
if (!$tokens[$index]->isGivenKind(T_FUNCTION)) {
continue;
}

$openParentIndex = $tokens->getNextTokenOfKind($index, ['(']);
if ($openParentIndex === null) {
continue;
}

$closeParentIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openParentIndex);

// Count commas to determine parameter count
$paramCount = $this->countParameters($tokens, $openParentIndex, $closeParentIndex);

// Only process if 2+ parameters
if ($paramCount < 2) {
continue;
}

// Check if already multiline
if ($this->isMultiline($tokens, $openParentIndex, $closeParentIndex)) {
continue;
}

// Apply multiline formatting
$this->makeMultiline($tokens, $openParentIndex, $closeParentIndex);
}
}

private function countParameters(
Tokens $tokens,
int $start,
int $end
): int {
$count = 0;
$depth = 0;

for ($i = $start + 1; $i < $end; ++$i) {
if ($tokens[$i]->equals('(') || $tokens[$i]->equals('[')) {
++$depth;
} elseif ($tokens[$i]->equals(')') || $tokens[$i]->equals(']')) {
--$depth;
} elseif ($depth === 0 && $tokens[$i]->equals(',')) {
++$count;
}
}

// If we found any commas, param count is commas + 1
// If no commas but there's content, it's 1 param
if ($count > 0) {
return $count + 1;
}

// Check if there's any non-whitespace content
for ($i = $start + 1; $i < $end; ++$i) {
if (!$tokens[$i]->isWhitespace()) {
return 1;
}
}

return 0;
}

private function isMultiline(
Tokens $tokens,
int $start,
int $end
): bool {
for ($i = $start; $i <= $end; ++$i) {
if ($tokens[$i]->isGivenKind(T_WHITESPACE) && str_contains($tokens[$i]->getContent(), "\n")) {
return true;
}
}

return false;
}

private function makeMultiline(
Tokens $tokens,
int $openParentIndex,
int $closeParentIndex
): void {
$indent = $this->detectIndent($tokens, $openParentIndex);
$lineIndent = str_repeat(' ', 4); // 4 spaces for parameters

// Add newline after opening parenthesis
$tokens->insertAt($openParentIndex + 1, new Token([T_WHITESPACE, "\n" . $indent . $lineIndent]));
++$closeParentIndex;

// Find all commas and add newlines after them
$depth = 0;
for ($i = $openParentIndex + 1; $i < $closeParentIndex; ++$i) {
if ($tokens[$i]->equals('(') || $tokens[$i]->equals('[')) {
++$depth;
} elseif ($tokens[$i]->equals(')') || $tokens[$i]->equals(']')) {
--$depth;
} elseif ($depth === 0 && $tokens[$i]->equals(',')) {
// Remove any whitespace after comma
$nextIndex = $i + 1;
while ($nextIndex < $closeParentIndex && $tokens[$nextIndex]->isWhitespace()) {
$tokens->clearAt($nextIndex);
++$nextIndex;
}

// Insert newline with proper indentation
$tokens->insertAt($i + 1, new Token([T_WHITESPACE, "\n" . $indent . $lineIndent]));
$closeParentIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openParentIndex);
}
}

// Add newline before closing parenthesis
$tokens->insertAt($closeParentIndex, new Token([T_WHITESPACE, "\n" . $indent]));

// Handle the opening brace
$nextNonWhitespace = $tokens->getNextNonWhitespace($closeParentIndex);
if ($nextNonWhitespace !== null && $tokens[$nextNonWhitespace]->equals('{')) {
$tokens->ensureWhitespaceAtIndex($nextNonWhitespace - 1, 1, ' ');
}
}

private function detectIndent(
Tokens $tokens,
int $index
): string {
// Look backwards to find the indentation of the current line
for ($i = $index - 1; $i >= 0; --$i) {
if ($tokens[$i]->isGivenKind(T_WHITESPACE) && str_contains($tokens[$i]->getContent(), "\n")) {
$lines = explode("\n", $tokens[$i]->getContent());

return end($lines);
}
}

return '';
}

public function getName(): string
{
return 'Ibexa/multiline_parameters';
}
}
4 changes: 3 additions & 1 deletion src/lib/PhpCsFixer/Sets/AbstractIbexaRuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Ibexa\CodeStyle\PhpCsFixer\Sets;

use AdamWojs\PhpCsFixerPhpdocForceFQCN\Fixer\Phpdoc\ForceFQCNFixer;
use Ibexa\CodeStyle\PhpCsFixer\Rule\MultilineParametersFixer;
use PhpCsFixer\Config;

abstract class AbstractIbexaRuleSet implements RuleSetInterface
Expand All @@ -26,6 +27,7 @@ public function getRules(): array
return [
'@PSR12' => false,
'AdamWojs/phpdoc_force_fqcn_fixer' => true,
'Ibexa/multiline_parameters' => true,
'array_syntax' => [
'syntax' => 'short',
],
Expand Down Expand Up @@ -204,7 +206,7 @@ public function getRules(): array
public function buildConfig(): Config
{
$config = new Config();
$config->registerCustomFixers([new ForceFQCNFixer()]);
$config->registerCustomFixers([new ForceFQCNFixer(), new MultilineParametersFixer()]);

$config->setRules(array_merge(
$config->getRules(),
Expand Down
12 changes: 10 additions & 2 deletions tests/lib/PhpCsFixer/InternalConfigFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,22 @@ protected function setUp(): void

/**
* @dataProvider provideRuleSetTestCases
*
* @param array{name: string, version: string, pretty_version?: string} $package
* @param class-string $expectedRuleSetClass
*/
public function testVersionBasedRuleSetSelection(array $package, string $expectedRuleSetClass): void
{
public function testVersionBasedRuleSetSelection(
array $package,
string $expectedRuleSetClass
): void {
$ruleSet = $this->createRuleSetFromPackage->invoke($this->factory, $package);

self::assertInstanceOf($expectedRuleSetClass, $ruleSet);
}

/**
* @return array<string, array{0: array{name: string, version: string, pretty_version?: string}, 1: class-string}>
*/
public function provideRuleSetTestCases(): array
{
return [
Expand Down
116 changes: 116 additions & 0 deletions tests/lib/PhpCsFixer/Rule/MultilineParametersFixerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\CodeStyle\PhpCsFixer\Rule;

use Ibexa\CodeStyle\PhpCsFixer\Rule\MultilineParametersFixer;
use PhpCsFixer\Tokenizer\Tokens;
use PHPUnit\Framework\TestCase;
use SplFileInfo;

final class MultilineParametersFixerTest extends TestCase
{
private MultilineParametersFixer $fixer;

protected function setUp(): void
{
$this->fixer = new MultilineParametersFixer();
}

/**
* @dataProvider provideFixCases
*/
public function testFix(
string $input,
string $expected
): void {
$tokens = Tokens::fromCode($input);
$this->fixer->fix(new SplFileInfo(__FILE__), $tokens);

self::assertSame($expected, $tokens->generateCode());
}

/**
* @return iterable<string, array{0: string, 1: string}>
*/
public static function provideFixCases(): iterable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing iterable type. Looks like phpstan.neon never got updated paths when we introduced ./tests directory 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, that was the case, added to phpstan configuration and fixed all issues.
Added tests folder to phpstan

{
yield 'single parameter should not be modified' => [
'<?php
function bar(array $package): void {
}',
'<?php
function bar(array $package): void {
}',
];

yield 'single parameter with type hints should not be modified' => [
'<?php
function bar(?string $package = null): void {
}',
'<?php
function bar(?string $package = null): void {
}',
];

yield 'multiple parameters should be split' => [
'<?php
function foo(array $package, string $expectedRuleSetClass): void {
}',
'<?php
function foo(
array $package,
string $expectedRuleSetClass
): void {
}',
];

yield 'multiple parameters with type hints should be split' => [
'<?php
function test(?string $foo = null, int $bar = 42): string {
}',
'<?php
function test(
?string $foo = null,
int $bar = 42
): string {
}',
];

yield 'constructor with properties should be split' => [
'<?php
class Test {
public function __construct(string $foo, int $bar) {
}
}',
'<?php
class Test {
public function __construct(
string $foo,
int $bar
) {
}
}',
];

yield 'already multiline should not be modified' => [
'<?php
function test(
string $foo,
int $bar
): void {
}',
'<?php
function test(
string $foo,
int $bar
): void {
}',
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,5 @@
'visibility_required' => true,
'whitespace_after_comma_in_array' => true,
'yoda_style' => false,
'Ibexa/multiline_parameters' => true,
];
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,5 @@
'no_trailing_comma_in_singleline_array' => true,
'no_unneeded_curly_braces' => true,
'single_blank_line_before_namespace' => true,
'Ibexa/multiline_parameters' => true,
];
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,5 @@
'visibility_required' => true,
'whitespace_after_comma_in_array' => true,
'yoda_style' => false,
'Ibexa/multiline_parameters' => true,
];
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,5 @@
'types_spaces' => [
'space' => 'single',
],
'Ibexa/multiline_parameters' => true,
];