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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG for PHP CS Fixer: custom fixers

## v3.32.0
- Update minimum PHP CS Fixer version to 3.85.0

## v3.31.0
- Update minimum PHP CS Fixer version to 3.84.0

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ PHPDoc `list` type must be used instead of `array` without a key.
```

#### PhpdocTypesCommaSpacesFixer
PHPDoc types commas must not be preceded by a whitespace, and must be succeeded by a single whitespace.
PHPDoc types commas must not be preceded by a whitespace, and must be succeeded by a single whitespace or newline.
```diff
-<?php /** @var array<int,string> */
+<?php /** @var array<int, string> */
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"php": "^7.4 || ^8.0",
"ext-filter": "*",
"ext-tokenizer": "*",
"friendsofphp/php-cs-fixer": "^3.84"
"friendsofphp/php-cs-fixer": "^3.85"
},
"require-dev": {
"phpunit/phpunit": "^9.6.22 || 10.5.45 || ^11.5.7"
Expand Down
10 changes: 5 additions & 5 deletions src/Fixer/AbstractTypesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ final public function fix(\SplFileInfo $file, Tokens $tokens): void
continue;
}

$typeExpression = $annotation->getTypeExpression();
if ($typeExpression === null) {
$types = $annotation->getTypes();
if ($types === []) {
continue;
}

$type = $typeExpression->toString();
$type = $this->fixType($type);
$annotation->setTypes([$type]);
$types = \array_map(fn (string $x): string => $this->fixType($x), $types);

$annotation->setTypes($types);
}

$newContent = $docBlock->getContent();
Expand Down
4 changes: 2 additions & 2 deletions src/Fixer/PhpdocTypesCommaSpacesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class PhpdocTypesCommaSpacesFixer extends AbstractTypesFixer
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'PHPDoc types commas must not be preceded by a whitespace, and must be succeeded by a single whitespace.',
'PHPDoc types commas must not be preceded by a whitespace, and must be succeeded by a single whitespace or newline.',
[new CodeSample("<?php /** @var array<int,string> */\n")],
'',
);
Expand All @@ -37,6 +37,6 @@ public function getPriority(): int

protected function fixType(string $type): string
{
return Preg::replace('/\\h*,\\s*/', ', ', $type);
return Preg::replace('/,(?!\\R)\\s*/', ', ', Preg::replace('/\\h*,/', ',', $type));
}
}
21 changes: 21 additions & 0 deletions tests/Fixer/PhpdocTypesCommaSpacesFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,26 @@ public static function provideFixCases(): iterable
* @return array<string,Foo> Description having "," should not be touched
*/',
];

yield [
<<<'PHP'
<?php
/**
* @return array{
* foo: bool,
* bar: null|string,
* }
*/
PHP,
<<<'PHP'
<?php
/**
* @return array{
* foo: bool ,
* bar: null|string ,
* }
*/
PHP,
];
}
}
21 changes: 21 additions & 0 deletions tests/Fixer/PhpdocTypesTrimFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,5 +257,26 @@ function fooBar($x, $y) {}
function baz() {}
',
];

yield [
<<<'PHP'
<?php
/**
* @return array{
* foo: null|bool,
* bar: string|null,
* }
*/
PHP,
<<<'PHP'
<?php
/**
* @return array{
* foo: null | bool,
* bar: string | null,
* }
*/
PHP,
];
}
}
Loading