-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce
PhpdocListTypeFixer
(#7796)
- Loading branch information
1 parent
d12282c
commit a1848cb
Showing
14 changed files
with
222 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
========================= | ||
Rule ``phpdoc_list_type`` | ||
========================= | ||
|
||
PHPDoc ``list`` type must be used instead of ``array`` without a key. | ||
|
||
Warning | ||
------- | ||
|
||
Using this rule is risky | ||
~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Risky when ``array`` key should be present, but is missing. | ||
|
||
Examples | ||
-------- | ||
|
||
Example #1 | ||
~~~~~~~~~~ | ||
|
||
.. code-block:: diff | ||
--- Original | ||
+++ New | ||
<?php | ||
/** | ||
- * @param array<int> $x | ||
- * @param array<array<string>> $y | ||
+ * @param list<int> $x | ||
+ * @param list<list<string>> $y | ||
*/ | ||
References | ||
---------- | ||
|
||
- Fixer class: `PhpCsFixer\\Fixer\\Phpdoc\\PhpdocListTypeFixer <./../../../src/Fixer/Phpdoc/PhpdocListTypeFixer.php>`_ | ||
- Test class: `PhpCsFixer\\Tests\\Fixer\\Phpdoc\\PhpdocListTypeFixerTest <./../../../tests/Fixer/Phpdoc/PhpdocListTypeFixerTest.php>`_ | ||
|
||
The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of PHP CS Fixer. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* Dariusz Rumiński <dariusz.ruminski@gmail.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace PhpCsFixer\Fixer\Phpdoc; | ||
|
||
use PhpCsFixer\AbstractPhpdocTypesFixer; | ||
use PhpCsFixer\FixerDefinition\CodeSample; | ||
use PhpCsFixer\FixerDefinition\FixerDefinition; | ||
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; | ||
use PhpCsFixer\Preg; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
|
||
final class PhpdocListTypeFixer extends AbstractPhpdocTypesFixer | ||
{ | ||
public function isCandidate(Tokens $tokens): bool | ||
{ | ||
return $tokens->isTokenKindFound(T_DOC_COMMENT); | ||
} | ||
|
||
public function isRisky(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function getDefinition(): FixerDefinitionInterface | ||
{ | ||
return new FixerDefinition( | ||
'PHPDoc `list` type must be used instead of `array` without a key.', | ||
[ | ||
new CodeSample(<<<'PHP' | ||
<?php | ||
/** | ||
* @param array<int> $x | ||
* @param array<array<string>> $y | ||
*/ | ||
PHP), | ||
], | ||
null, | ||
'Risky when `array` key should be present, but is missing.' | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* Must run before PhpdocAlignFixer, PhpdocTypesOrderFixer. | ||
* Must run after AlignMultilineCommentFixer, CommentToPhpdocFixer, PhpdocIndentFixer, PhpdocScalarFixer, PhpdocToCommentFixer, PhpdocTypesFixer. | ||
*/ | ||
public function getPriority(): int | ||
{ | ||
return 1; | ||
} | ||
|
||
protected function normalize(string $type): string | ||
{ | ||
return Preg::replace('/array(?=<[^,]+(>|<|{|\\())/i', 'list', $type); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.