Skip to content

Commit f514893

Browse files
authored
Add PhpdocTypesCommaSpacesFixer (#753)
1 parent 7a6dcef commit f514893

File tree

4 files changed

+164
-1
lines changed

4 files changed

+164
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# CHANGELOG for PHP CS Fixer: custom fixers
22

3+
## v3.9.0
4+
- Add PhpdocTypesCommaSpacesFixer
5+
36
## v3.8.0
47
- Update minimum PHP version to 7.4
58
- Update minimum PHP CS Fixer version to 3.6.0

README.md

Lines changed: 8 additions & 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-3377-brightgreen.svg)
6+
![Tests](https://img.shields.io/badge/tests-3405-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)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions)
@@ -520,6 +520,13 @@ The `@var` annotations must be on a single line if they are the only content.
520520
}
521521
```
522522

523+
#### PhpdocTypesCommaSpacesFixer
524+
PHPDoc types commas must not be preceded by whitespace, and must be succeeded by single whitespace.
525+
```diff
526+
-<?php /** @var array<int,string> */
527+
+<?php /** @var array<int, string> */
528+
```
529+
523530
#### PhpdocTypesTrimFixer
524531
PHPDoc types must be trimmed.
525532
```diff
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php declare(strict_types=1);
2+
3+
/*
4+
* This file is part of PHP CS Fixer: custom fixers.
5+
*
6+
* (c) 2018 Kuba Werłos
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace PhpCsFixerCustomFixers\Fixer;
13+
14+
use PhpCsFixer\FixerDefinition\CodeSample;
15+
use PhpCsFixer\FixerDefinition\FixerDefinition;
16+
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
17+
use PhpCsFixer\Preg;
18+
19+
final class PhpdocTypesCommaSpacesFixer extends AbstractTypesFixer
20+
{
21+
public function getDefinition(): FixerDefinitionInterface
22+
{
23+
return new FixerDefinition(
24+
'PHPDoc types commas must not be preceded by whitespace, and must be succeeded by single whitespace.',
25+
[new CodeSample("<?php /** @var array<int,string> */\n")]
26+
);
27+
}
28+
29+
public function getPriority(): int
30+
{
31+
return 0;
32+
}
33+
34+
protected function fixType(string $type): string
35+
{
36+
$newType = Preg::replace('/\h*,\s*/', ', ', $type);
37+
38+
if ($newType === $type) {
39+
return $type;
40+
}
41+
42+
return $this->fixType($newType);
43+
}
44+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php declare(strict_types=1);
2+
3+
/*
4+
* This file is part of PHP CS Fixer: custom fixers.
5+
*
6+
* (c) 2018 Kuba Werłos
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace Tests\Fixer;
13+
14+
/**
15+
* @internal
16+
*
17+
* @covers \PhpCsFixerCustomFixers\Fixer\PhpdocTypesCommaSpacesFixer
18+
*/
19+
final class PhpdocTypesCommaSpacesFixerTest extends AbstractFixerTestCase
20+
{
21+
public function testIsRisky(): void
22+
{
23+
self::assertFalse($this->fixer->isRisky());
24+
}
25+
26+
/**
27+
* @dataProvider provideFixCases
28+
*/
29+
public function testFix(string $expected, ?string $input = null): void
30+
{
31+
$this->doTest($expected, $input);
32+
}
33+
34+
/**
35+
* @return iterable<array{0: string, 1?: string}>
36+
*/
37+
public static function provideFixCases(): iterable
38+
{
39+
yield ['<?php /** @var int $commaCount Number of "," in text. */'];
40+
41+
yield [
42+
'<?php /** @var array<int, string> */',
43+
'<?php /** @var array<int,string> */',
44+
];
45+
46+
yield [
47+
'<?php /** @var array<int, string> */',
48+
'<?php /** @var array<int , string> */',
49+
];
50+
51+
yield [
52+
'<?php
53+
/** @var array<int, string> $a */
54+
/** @var array<int, string> $b */
55+
/** @var array<int, string> $c */
56+
/** @var array<int, string> $d */
57+
',
58+
'<?php
59+
/** @var array<int ,string> $a */
60+
/** @var array<int, string> $b */
61+
/** @var array<int, string> $c */
62+
/** @var array<int , string> $d */
63+
',
64+
];
65+
66+
yield [
67+
'<?php /**
68+
* @param array<int, string> $a
69+
* @param array<int, string> $b
70+
* @param array<int, string> $c
71+
* @param array<int, array<int, array<int, string>>> $d
72+
*/',
73+
'<?php /**
74+
* @param array<int,string> $a
75+
* @param array<int ,string> $b
76+
* @param array<int , string> $c
77+
* @param array<int , array<int , array<int , string>>> $d
78+
*/',
79+
];
80+
81+
yield [
82+
'<?php /**
83+
* @return array< Foo, Bar, Baz >
84+
*/',
85+
'<?php /**
86+
* @return array< Foo , Bar , Baz >
87+
*/',
88+
];
89+
90+
yield [
91+
'<?php /**
92+
* The "," in here should not be touched
93+
*
94+
* @param array<int, int> $x Comma in type should be fixed, but this one: "," should not
95+
* @param array<int, int> $y Comma in type should be fixed, but this one: "," and "," should not
96+
*
97+
* @return array<string, Foo> Description having "," should not be touched
98+
*/',
99+
'<?php /**
100+
* The "," in here should not be touched
101+
*
102+
* @param array<int,int> $x Comma in type should be fixed, but this one: "," should not
103+
* @param array<int , int> $y Comma in type should be fixed, but this one: "," and "," should not
104+
*
105+
* @return array<string,Foo> Description having "," should not be touched
106+
*/',
107+
];
108+
}
109+
}

0 commit comments

Comments
 (0)