Skip to content

Commit 1b062d8

Browse files
authored
Merge pull request #4 from jackd248/align-rule
refactor(!): remove 'package' annotation from DocBlock examples
2 parents ba41a5a + a745d5e commit 1b062d8

File tree

6 files changed

+16
-12
lines changed

6 files changed

+16
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/.php-cs-fixer.cache
44
/.phpunit.result.cache
55
/.build
6+
/coverage-report
67
/php-cs-fixer.xml
78
/phpstan.xml
89
/site

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class MyClass
3636
* MyClass.
3737
*
3838
* @author Your Name <your@email.org>
39-
* @package MyPackage
4039
*/
4140
class MyClass
4241
{
@@ -58,6 +57,9 @@ composer require --dev konradmichalik/php-doc-block-header-fixer
5857

5958
Add the PHP-CS-Fixer rule in your `.php-cs-fixer.php` file:
6059

60+
> [!NOTE]
61+
> This fixer is compatible with standard PHP-CS-Fixer rules. It avoids adding annotations that conflict with rules like `phpdoc_no_package` and follows spacing conventions compatible with `phpdoc_separation`.
62+
6163
```php
6264
<?php
6365
// ...
@@ -71,7 +73,6 @@ return (new PhpCsFixer\Config())
7173
'annotations' => [
7274
'author' => 'Konrad Michalik <hej@konradmichalik.dev>',
7375
'license' => 'GPL-3.0-or-later',
74-
'package' => 'PhpDocBlockHeaderFixer',
7576
],
7677
'preserve_existing' => true,
7778
'separate' => 'none',
@@ -96,7 +97,6 @@ return (new PhpCsFixer\Config())
9697
[
9798
'author' => 'Konrad Michalik <hej@konradmichalik.dev>',
9899
'license' => 'GPL-3.0-or-later',
99-
'package' => 'PhpDocBlockHeaderFixer',
100100
],
101101
preserveExisting: true,
102102
separate: \KonradMichalik\PhpDocBlockHeaderFixer\Enum\Separate::None,

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "konradmichalik/php-doc-block-header-fixer",
3-
"description": "",
3+
"description": "This package contains a PHP-CS-Fixer rule to automatically fix the class header regarding PHP DocBlocks.",
44
"license": "GPL-3.0-or-later",
55
"type": "library",
66
"authors": [

src/Rules/DocBlockHeaderFixer.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function getConfigurationDefinition(): FixerConfigurationResolverInterfac
7979
->getOption(),
8080
(new FixerOptionBuilder('separate', 'Separate the comment'))
8181
->setAllowedValues(Separate::getList())
82-
->setDefault(Separate::Both->value)
82+
->setDefault(Separate::None->value)
8383
->getOption(),
8484
(new FixerOptionBuilder('add_class_name', 'Add class name before annotations'))
8585
->setAllowedTypes(['bool'])
@@ -202,7 +202,7 @@ private function replaceDocBlock(Tokens $tokens, int $docBlockIndex, array $anno
202202
*/
203203
private function insertNewDocBlock(Tokens $tokens, int $classIndex, array $annotations, string $className): void
204204
{
205-
$separate = $this->resolvedConfiguration['separate'] ?? 'both';
205+
$separate = $this->resolvedConfiguration['separate'] ?? 'none';
206206
$insertIndex = $this->findInsertPosition($tokens, $classIndex);
207207

208208
$tokensToInsert = [];
@@ -216,9 +216,14 @@ private function insertNewDocBlock(Tokens $tokens, int $classIndex, array $annot
216216
$docBlock = $this->buildDocBlock($annotations, $className);
217217
$tokensToInsert[] = new Token([T_DOC_COMMENT, $docBlock]);
218218

219-
// Add separation after comment if needed
219+
// For compatibility with no_blank_lines_after_phpdoc, only add bottom separation when 'separate' is not 'none'
220+
// This prevents conflicts with PHP-CS-Fixer rules that manage DocBlock spacing
220221
if (in_array($separate, ['bottom', 'both'], true)) {
221-
$tokensToInsert[] = new Token([T_WHITESPACE, "\n"]);
222+
// Check if there's already whitespace after the class declaration
223+
$nextToken = $tokens[$classIndex] ?? null;
224+
if (null !== $nextToken && !$nextToken->isWhitespace()) {
225+
$tokensToInsert[] = new Token([T_WHITESPACE, "\n"]);
226+
}
222227
}
223228

224229
$tokens->insertAt($insertIndex, $tokensToInsert);
@@ -296,7 +301,7 @@ private function buildDocBlock(array $annotations, string $className): string
296301
if ($addClassName && !empty($className)) {
297302
$docBlock .= " * {$className}.\n";
298303

299-
// Add empty line after class name if there are annotations
304+
// Add empty line after class name if there are annotations - compatible with phpdoc_separation
300305
if (!empty($annotations)) {
301306
$docBlock .= " *\n";
302307
}

tests/src/Generators/DocBlockHeaderTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ public function testValidateAnnotationsWithValidKeys(): void
125125
'license' => 'MIT',
126126
'version' => '1.0.0',
127127
'since' => '1.0.0',
128-
'package' => 'MyPackage',
129128
'subpackage' => 'SubPackage',
130129
'see' => 'https://example.com',
131130
'link' => 'https://example.com',

tests/src/Rules/DocBlockHeaderFixerTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,9 @@ public function testBuildDocBlockWithMultipleAnnotations(): void
9797
$result = $method->invoke($this->fixer, [
9898
'author' => 'John Doe <john@example.com>',
9999
'license' => 'MIT',
100-
'package' => 'MyPackage',
101100
], '');
102101

103-
$expected = "/**\n * @author John Doe <john@example.com>\n * @license MIT\n * @package MyPackage\n */";
102+
$expected = "/**\n * @author John Doe <john@example.com>\n * @license MIT\n */";
104103
self::assertSame($expected, $result);
105104
}
106105

0 commit comments

Comments
 (0)