Skip to content
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

[TokenRunner] Handle crash on BlockFinder when using LineLengthFixer + NewWithParenthesesFixer #45

Merged
merged 1 commit into from
May 29, 2024
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
5 changes: 5 additions & 0 deletions src/TokenRunner/Analyzer/FixerAnalyzer/BlockFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public function findInTokensByEdge(Tokens $tokens, int $position): ?BlockInfo

if ($token->isGivenKind([T_FUNCTION, CT::T_USE_LAMBDA, T_NEW])) {
$position = $tokens->getNextTokenOfKind($position, ['(', ';']);

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

/** @var Token $token */
$token = $tokens[$position];

Expand Down
21 changes: 21 additions & 0 deletions tests/Issues/Fixture/line_length_parentheses.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types=1);

final class LineLengtParentheses
{
public function __construct(
private DateTimeImmutable $dateTimeImmutable = new DateTimeImmutable()
) {}
}

?>
-----
<?php declare(strict_types=1);

final class LineLengtParentheses
{
public function __construct(
private DateTimeImmutable $dateTimeImmutable = new DateTimeImmutable
) {}
}

?>
28 changes: 28 additions & 0 deletions tests/Issues/LineLengthParenthesesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Symplify\CodingStandard\Tests\Issues;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Symplify\EasyCodingStandard\Testing\PHPUnit\AbstractCheckerTestCase;

final class LineLengthParenthesesTest extends AbstractCheckerTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
yield [__DIR__ . '/Fixture/line_length_parentheses.php.inc'];
}

public function provideConfig(): string
{
return __DIR__ . '/config/line_length_parentheses.php';
}
}
18 changes: 18 additions & 0 deletions tests/Issues/config/line_length_parentheses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

use PhpCsFixer\Fixer\Operator\NewWithParenthesesFixer;
use Symplify\CodingStandard\Fixer\LineLength\LineLengthFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return static function (ECSConfig $ecsConfig): void {
$ecsConfig->rules([
LineLengthFixer::class,
]);

$ecsConfig->ruleWithConfiguration(NewWithParenthesesFixer::class, [
'anonymous_class' => false,
'named_class' => false,
]);
};
Loading