Skip to content

Commit

Permalink
feature #5123 PhpdocTypesFixer - support generic types (kubawerlos)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the master branch.

Discussion
----------

PhpdocTypesFixer - support generic types

Commits
-------

c04067d PhpdocTypesFixer - support generic types
  • Loading branch information
keradus committed Oct 17, 2021
2 parents 2c1027f + c04067d commit 66d103e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
36 changes: 27 additions & 9 deletions src/Fixer/Phpdoc/PhpdocTypesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Preg;

/**
* @author Graham Campbell <hello@gjcampbell.co.uk>
Expand All @@ -35,7 +36,7 @@ final class PhpdocTypesFixer extends AbstractPhpdocTypesFixer implements Configu
*
* @var array<string,string[]>
*/
private static $possibleTypes = [
private const POSSIBLE_TYPES = [
'simple' => [
'array',
'bool',
Expand Down Expand Up @@ -69,9 +70,9 @@ final class PhpdocTypesFixer extends AbstractPhpdocTypesFixer implements Configu
];

/**
* @var array string[]
* @var string
*/
private $typesToFix = [];
private $patternToFix = '';

/**
* {@inheritdoc}
Expand All @@ -80,9 +81,22 @@ public function configure(array $configuration): void
{
parent::configure($configuration);

$this->typesToFix = array_merge(...array_map(static function (string $group): array {
return self::$possibleTypes[$group];
$typesToFix = array_merge(...array_map(static function (string $group): array {
return self::POSSIBLE_TYPES[$group];
}, $this->configuration['groups']));

$this->patternToFix = sprintf(
'/(?<![a-zA-Z0-9_\x80-\xff]\\\\)(\b|.(?=\$))(%s)\b(?!\\\\)/i',
implode(
'|',
array_map(
function (string $type): string {
return preg_quote($type, '/');
},
$typesToFix
)
)
);
}

/**
Expand Down Expand Up @@ -140,17 +154,21 @@ public function getPriority(): int
*/
protected function normalize(string $type): string
{
$lower = strtolower($type);

return \in_array($lower, $this->typesToFix, true) ? $lower : $type;
return Preg::replaceCallback(
$this->patternToFix,
function (array $matches): string {
return strtolower($matches[0]);
},
$type
);
}

/**
* {@inheritdoc}
*/
protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
{
$possibleGroups = array_keys(self::$possibleTypes);
$possibleGroups = array_keys(self::POSSIBLE_TYPES);

return new FixerConfigurationResolver([
(new FixerOptionBuilder('groups', 'Type groups to fix.'))
Expand Down
29 changes: 29 additions & 0 deletions tests/Fixer/Phpdoc/PhpdocTypesFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,35 @@ public function testWithConfig(): void
$this->doTest($expected, $input);
}

public function testGenerics(): void
{
$this->fixer->configure(['groups' => ['simple', 'meta']]);
$this->doTest(
'<?php
/**
* @param array<int, object> $a
* @param array<iterable> $b
* @param array<parent|$this|self> $c
* @param array<\int, \object> $d
* @param iterable<Foo\Int\Bar|Foo\Int|Int\Bar> $thisShouldNotBeChanged
* @param iterable<BOOLBOOLBOOL|INTINTINT|ARRAY_BOOL_INT_STRING_> $thisShouldNotBeChangedNeither
*
* @return array<int, array<string, array<int, DoNotChangeThisAsThisIsAClass>>>
*/',
'<?php
/**
* @param ARRAY<INT, OBJECT> $a
* @param ARRAY<ITERABLE> $b
* @param array<Parent|$This|Self> $c
* @param ARRAY<\INT, \OBJECT> $d
* @param iterable<Foo\Int\Bar|Foo\Int|Int\Bar> $thisShouldNotBeChanged
* @param iterable<BOOLBOOLBOOL|INTINTINT|ARRAY_BOOL_INT_STRING_> $thisShouldNotBeChangedNeither
*
* @return ARRAY<INT, ARRAY<STRING, ARRAY<INT, DoNotChangeThisAsThisIsAClass>>>
*/'
);
}

public function testWrongConfig(): void
{
$this->expectException(InvalidFixerConfigurationException::class);
Expand Down

0 comments on commit 66d103e

Please sign in to comment.