Skip to content

Commit

Permalink
[TokenRunner] Handle crash on BlockFinder when using LineLengthFixer …
Browse files Browse the repository at this point in the history
…+ NewWithParenthesesFixer (#45)
  • Loading branch information
samsonasik committed May 29, 2024
1 parent 50599e1 commit abd81d4
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
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,
]);
};

0 comments on commit abd81d4

Please sign in to comment.