Skip to content

Commit c8f4d11

Browse files
authored
Improve DeclareAfterOpeningTagFixer (#658)
1 parent 97b47b7 commit c8f4d11

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Latest stable version](https://img.shields.io/packagist/v/kubawerlos/php-cs-fixer-custom-fixers.svg?label=current%20version)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
44
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
55
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
6-
![Tests](https://img.shields.io/badge/tests-3058-brightgreen.svg)
6+
![Tests](https://img.shields.io/badge/tests-3060-brightgreen.svg)
77
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
88

99
[![CI Status](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/workflows/CI/badge.svg?branch=main&event=push)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions)

src/Fixer/DeclareAfterOpeningTagFixer.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,24 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
5454
}
5555

5656
$openingTagTokenContent = $tokens[0]->getContent();
57-
if ($openingTagTokenContent === '<?php ' && $tokens[1]->isGivenKind(\T_DECLARE)) {
57+
58+
$tokens[0] = new Token([\T_OPEN_TAG, \substr($openingTagTokenContent, 0, 5) . ' ']);
59+
60+
/** @var int $declareIndex */
61+
$declareIndex = $tokens->getNextTokenOfKind(0, [[\T_DECLARE]]);
62+
if ($declareIndex <= 2) {
63+
$tokens->clearRange(1, $declareIndex - 1);
64+
5865
return;
5966
}
6067

61-
$tokens[0] = new Token([\T_OPEN_TAG, \substr($openingTagTokenContent, 0, 5) . ' ']);
6268
if ($tokens[1]->isGivenKind(\T_WHITESPACE)) {
6369
$tokens[1] = new Token([\T_WHITESPACE, \substr($openingTagTokenContent, 5) . $tokens[1]->getContent()]);
6470
} else {
6571
$tokens->insertAt(1, new Token([\T_WHITESPACE, \substr($openingTagTokenContent, 5)]));
72+
$declareIndex++;
6673
}
6774

68-
/** @var int $declareIndex */
69-
$declareIndex = $tokens->getNextTokenOfKind(0, [[\T_DECLARE]]);
70-
7175
/** @var int $openParenthesisIndex */
7276
$openParenthesisIndex = $tokens->getNextMeaningfulToken($declareIndex);
7377
$closeParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openParenthesisIndex);

tests/Fixer/DeclareAfterOpeningTagFixerTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,19 @@ public static function provideFixCases(): iterable
5050
',
5151
];
5252

53-
yield 'fix and clean up empty lines' => [
53+
yield 'fix uppercase opening tag' => [
54+
'<?PHP declare(strict_types=1);
55+
// Foo
56+
class Foo {}
57+
',
58+
'<?PHP
59+
// Foo
60+
declare(strict_types=1);
61+
class Foo {}
62+
',
63+
];
64+
65+
yield 'fix and clean up empty lines left' => [
5466
'<?php declare(strict_types=1);
5567
5668
/*
@@ -70,5 +82,19 @@ public static function provideFixCases(): iterable
7082
// code starts here
7183
',
7284
];
85+
86+
yield 'fix and clean up empty lines above' => [
87+
'<?php declare(strict_types=1);
88+
89+
// Foo
90+
',
91+
'<?php
92+
93+
94+
declare(strict_types=1);
95+
96+
// Foo
97+
',
98+
];
7399
}
74100
}

tests/priority_fixtures/blank_line_after_opening_tag,Custom_declare_after_opening_tag.test

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
{ "blank_line_after_opening_tag": true, "PhpCsFixerCustomFixers/declare_after_opening_tag": true }
33
--EXPECTED--
44
<?php declare(strict_types=1);
5-
65
class Foo {}
76

87
--INPUT--

0 commit comments

Comments
 (0)