Skip to content

Commit e03b07a

Browse files
authored
Add PhpdocTypeListFixer (#938)
1 parent 2f12f8c commit e03b07a

12 files changed

+209
-6
lines changed

.dev-tools/src/Priority/PriorityCollection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PhpCsFixer\FixerFactory;
1616
use PhpCsFixerCustomFixers\Fixer\CommentSurroundedBySpacesFixer;
1717
use PhpCsFixerCustomFixers\Fixer\NoCommentedOutCodeFixer;
18+
use PhpCsFixerCustomFixers\Fixer\PhpdocTypeListFixer;
1819
use PhpCsFixerCustomFixers\Fixers;
1920
use Tests\PriorityTest;
2021

@@ -89,6 +90,7 @@ private function getFirstPriorityFixerWithoutPriority(): ?PriorityFixer
8990
static $firstFixers = [
9091
CommentSurroundedBySpacesFixer::class,
9192
NoCommentedOutCodeFixer::class,
93+
PhpdocTypeListFixer::class,
9294
];
9395

9496
$priorityFixersWithoutPriorities = \array_filter(

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.18.0
4+
- Add PhpdocTypeListFixer
5+
36
## v3.17.0
47
- PhpdocNoIncorrectVarAnnotationFixer - support promoted properties
58

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![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)
66
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
77
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
8-
![Tests](https://img.shields.io/badge/tests-3511-brightgreen.svg)
8+
![Tests](https://img.shields.io/badge/tests-3551-brightgreen.svg)
99
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
1010

1111
[![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)
@@ -545,6 +545,17 @@ The `@var` annotation must be on a single line if it is the only content.
545545
}
546546
```
547547

548+
#### PhpdocTypeListFixer
549+
PHPDoc type `list` must be used instead of `array` without a key type.
550+
```diff
551+
<?php
552+
/**
553+
- * @param array<string>
554+
+ * @param list<string>
555+
*/
556+
function foo($x) {}
557+
```
558+
548559
#### PhpdocTypesCommaSpacesFixer
549560
PHPDoc types commas must not be preceded by a whitespace, and must be succeeded by a single whitespace.
550561
```diff

src/Fixer/PhpUnitAssertArgumentsOrderFixer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
7878
{
7979
$phpUnitTestCaseIndicator = new PhpUnitTestCaseIndicator();
8080

81-
/** @var array<int> $indices */
81+
/** @var list<int> $indices */
8282
foreach ($phpUnitTestCaseIndicator->findPhpUnitClasses($tokens) as $indices) {
8383
$this->fixArgumentsOrder($tokens, $indices[0], $indices[1]);
8484
}

src/Fixer/PhpdocArrayStyleFixer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ function foo() { return [1, 2]; }
3737
}
3838

3939
/**
40-
* Must run before PhpdocAlignFixer, PhpdocTypesOrderFixer.
40+
* Must run before PhpdocAlignFixer, PhpdocTypeListFixer, PhpdocTypesOrderFixer.
4141
*/
4242
public function getPriority(): int
4343
{
44-
return 1;
44+
return 2;
4545
}
4646

4747
protected function fixType(string $type): string

src/Fixer/PhpdocTypeListFixer.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 PhpdocTypeListFixer extends AbstractTypesFixer
20+
{
21+
public function getDefinition(): FixerDefinitionInterface
22+
{
23+
return new FixerDefinition(
24+
'PHPDoc type `list` must be used instead of `array` without a key type.',
25+
[new CodeSample('<?php
26+
/**
27+
* @param array<string>
28+
*/
29+
function foo($x) {}
30+
')],
31+
'',
32+
);
33+
}
34+
35+
/**
36+
* Must run before PhpdocAlignFixer, PhpdocTypesOrderFixer.
37+
* Must run after CommentToPhpdocFixer, PhpdocArrayStyleFixer.
38+
*/
39+
public function getPriority(): int
40+
{
41+
return 1;
42+
}
43+
44+
protected function fixType(string $type): string
45+
{
46+
return Preg::replace('/array(?=<[^,]+(>|<|{|\\())/', 'list', $type);
47+
}
48+
}

tests/Analyzer/Analysis/ConstructorAnalysisTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function __construct(Foo $x = null, ?Bar $y = null, float $z = 3.14) {}
6464
}
6565

6666
/**
67-
* @param array<string> $expected
67+
* @param list<string> $expected
6868
*
6969
* @dataProvider provideGettingConstructorParameterNames80Cases
7070
*
@@ -80,7 +80,7 @@ public function testGettingConstructorParameterNames80(array $expected, string $
8080
}
8181

8282
/**
83-
* @return iterable<array{array<string>, string}>
83+
* @return iterable<array{list<string>, string}>
8484
*/
8585
public static function provideGettingConstructorParameterNames80Cases(): iterable
8686
{
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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\PhpdocTypeListFixer
18+
*/
19+
final class PhpdocTypeListFixerTest extends AbstractFixerTestCase
20+
{
21+
public function testIsRisky(): void
22+
{
23+
self::assertRiskiness(false);
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 [
40+
'<?php
41+
/** @var array<int, string> */
42+
/** @var array<int, array<string, bool>> */
43+
/** @var array<int, array{string, string, string}> */
44+
',
45+
];
46+
47+
yield [
48+
'<?php /** @var list<string> */',
49+
'<?php /** @var array<string> */',
50+
];
51+
52+
yield [
53+
'<?php /** @var list<list<list<string>>> */',
54+
'<?php /** @var array<array<array<string>>> */',
55+
];
56+
57+
yield [
58+
'<?php /** @var list<array<int, list<bool>>> */',
59+
'<?php /** @var array<array<int, array<bool>>> */',
60+
];
61+
62+
yield [
63+
'<?php /** @var array<int, list<array<string, bool>>> */',
64+
'<?php /** @var array<int, array<array<string, bool>>> */',
65+
];
66+
67+
yield [
68+
'<?php /** @var list<callable(int, int): string> */',
69+
'<?php /** @var array<callable(int, int): string> */',
70+
];
71+
72+
yield [
73+
'<?php /** @var non-empty-list<Type> */',
74+
'<?php /** @var non-empty-array<Type> */',
75+
];
76+
}
77+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--CONFIGURATION--
2+
{ "PhpCsFixerCustomFixers/phpdoc_array_style": true, "PhpCsFixerCustomFixers/phpdoc_type_list": true }
3+
--EXPECTED--
4+
<?php
5+
/**
6+
* @return list<int>
7+
*/
8+
function bar() {}
9+
10+
--INPUT--
11+
<?php
12+
/**
13+
* @return int[]
14+
*/
15+
function bar() {}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--CONFIGURATION--
2+
{ "PhpCsFixerCustomFixers/phpdoc_type_list": true, "phpdoc_align": true }
3+
--EXPECTED--
4+
<?php
5+
/**
6+
* @param list<int> $x
7+
* @param string $y
8+
*/
9+
function foo($x, $y) {}
10+
11+
--INPUT--
12+
<?php
13+
/**
14+
* @param array<int> $x
15+
* @param string $y
16+
*/
17+
function foo($x, $y) {}

0 commit comments

Comments
 (0)