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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/.php-cs-fixer.cache
/.phpunit.result.cache
/.build
/coverage-report
/php-cs-fixer.xml
/phpstan.xml
/site
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class MyClass
* MyClass.
*
* @author Your Name <your@email.org>
* @package MyPackage
*/
class MyClass
{
Expand All @@ -58,6 +57,9 @@ composer require --dev konradmichalik/php-doc-block-header-fixer

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

> [!NOTE]
> 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`.

```php
<?php
// ...
Expand All @@ -71,7 +73,6 @@ return (new PhpCsFixer\Config())
'annotations' => [
'author' => 'Konrad Michalik <hej@konradmichalik.dev>',
'license' => 'GPL-3.0-or-later',
'package' => 'PhpDocBlockHeaderFixer',
],
'preserve_existing' => true,
'separate' => 'none',
Expand All @@ -96,7 +97,6 @@ return (new PhpCsFixer\Config())
[
'author' => 'Konrad Michalik <hej@konradmichalik.dev>',
'license' => 'GPL-3.0-or-later',
'package' => 'PhpDocBlockHeaderFixer',
],
preserveExisting: true,
separate: \KonradMichalik\PhpDocBlockHeaderFixer\Enum\Separate::None,
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "konradmichalik/php-doc-block-header-fixer",
"description": "",
"description": "This package contains a PHP-CS-Fixer rule to automatically fix the class header regarding PHP DocBlocks.",
"license": "GPL-3.0-or-later",
"type": "library",
"authors": [
Expand Down
15 changes: 10 additions & 5 deletions src/Rules/DocBlockHeaderFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function getConfigurationDefinition(): FixerConfigurationResolverInterfac
->getOption(),
(new FixerOptionBuilder('separate', 'Separate the comment'))
->setAllowedValues(Separate::getList())
->setDefault(Separate::Both->value)
->setDefault(Separate::None->value)
->getOption(),
(new FixerOptionBuilder('add_class_name', 'Add class name before annotations'))
->setAllowedTypes(['bool'])
Expand Down Expand Up @@ -202,7 +202,7 @@ private function replaceDocBlock(Tokens $tokens, int $docBlockIndex, array $anno
*/
private function insertNewDocBlock(Tokens $tokens, int $classIndex, array $annotations, string $className): void
{
$separate = $this->resolvedConfiguration['separate'] ?? 'both';
$separate = $this->resolvedConfiguration['separate'] ?? 'none';
$insertIndex = $this->findInsertPosition($tokens, $classIndex);

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

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

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

// Add empty line after class name if there are annotations
// Add empty line after class name if there are annotations - compatible with phpdoc_separation
if (!empty($annotations)) {
$docBlock .= " *\n";
}
Expand Down
1 change: 0 additions & 1 deletion tests/src/Generators/DocBlockHeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ public function testValidateAnnotationsWithValidKeys(): void
'license' => 'MIT',
'version' => '1.0.0',
'since' => '1.0.0',
'package' => 'MyPackage',
'subpackage' => 'SubPackage',
'see' => 'https://example.com',
'link' => 'https://example.com',
Expand Down
3 changes: 1 addition & 2 deletions tests/src/Rules/DocBlockHeaderFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,9 @@ public function testBuildDocBlockWithMultipleAnnotations(): void
$result = $method->invoke($this->fixer, [
'author' => 'John Doe <john@example.com>',
'license' => 'MIT',
'package' => 'MyPackage',
], '');

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

Expand Down