Skip to content

Commit

Permalink
fix: PhpdocTypesFixer - handle more complex types (#7791)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubawerlos committed Feb 1, 2024
1 parent 21f5b42 commit d12282c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 40 deletions.
28 changes: 17 additions & 11 deletions src/AbstractPhpdocTypesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use PhpCsFixer\DocBlock\Annotation;
use PhpCsFixer\DocBlock\DocBlock;
use PhpCsFixer\DocBlock\TypeExpression;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

Expand Down Expand Up @@ -100,18 +101,23 @@ private function fixTypes(Annotation $annotation): void
private function normalizeTypes(array $types): array
{
return array_map(
fn (string $type): string => $this->normalizeType($type),
function (string $type): string {
$typeExpression = new TypeExpression($type, null, []);

$typeExpression->walkTypes(function (TypeExpression $type): void {
if (!$type->isUnionType()) {
$value = $this->normalize($type->toString());

// TODO TypeExpression should be immutable and walkTypes method should be changed to mapTypes method
\Closure::bind(static function () use ($type, $value): void {
$type->value = $value;
}, null, TypeExpression::class)();
}
});

return $typeExpression->toString();
},
$types
);
}

/**
* Prepare the type and normalize it.
*/
private function normalizeType(string $type): string
{
return str_ends_with($type, '[]')
? $this->normalizeType(substr($type, 0, -2)).'[]'
: $this->normalize($type);
}
}
10 changes: 8 additions & 2 deletions src/Fixer/Phpdoc/PhpdocScalarFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,16 @@ protected function createConfigurationDefinition(): FixerConfigurationResolverIn

protected function normalize(string $type): string
{
$suffix = '';
while (str_ends_with($type, '[]')) {
$type = substr($type, 0, -2);
$suffix .= '[]';
}

if (\in_array($type, $this->configuration['types'], true)) {
return self::$types[$type];
$type = self::$types[$type];
}

return $type;
return $type.$suffix;
}
}
39 changes: 12 additions & 27 deletions src/Fixer/Phpdoc/PhpdocTypesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
namespace PhpCsFixer\Fixer\Phpdoc;

use PhpCsFixer\AbstractPhpdocTypesFixer;
use PhpCsFixer\DocBlock\TypeExpression;
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\FixerConfiguration\AllowedValueSubset;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
Expand Down Expand Up @@ -129,32 +128,18 @@ public function getPriority(): int

protected function normalize(string $type): string
{
$typeExpression = new TypeExpression($type, null, []);

$typeExpression->walkTypes(function (TypeExpression $type): void {
if (!$type->isUnionType()) {
$value = $type->toString();
$valueLower = strtolower($value);
if (isset($this->typesSetToFix[$valueLower])) {
$value = $valueLower;
}

// normalize shape/callable/generic identifiers too
// TODO parse them as inner types and this will be not needed then
$value = Preg::replaceCallback(
'/^(\??\s*)([^()[\]{}<>\'"]+)(?<!\s)(\s*[\s()[\]{}<>])/',
fn ($matches) => $matches[1].$this->normalize($matches[2]).$matches[3],
$value
);

// TODO TypeExpression should be immutable and walkTypes method should be changed to mapTypes method
\Closure::bind(static function () use ($type, $value): void {
$type->value = $value;
}, null, TypeExpression::class)();
}
});

return $typeExpression->toString();
$typeLower = strtolower($type);
if (isset($this->typesSetToFix[$typeLower])) {
$type = $typeLower;
}

// normalize shape/callable/generic identifiers too
// TODO parse them as inner types and this will be not needed then
return Preg::replaceCallback(
'/^(\??\s*)([^()[\]{}<>\'"]+)(?<!\s)(\s*[\s()[\]{}<>])/',
fn ($matches) => $matches[1].$this->normalize($matches[2]).$matches[3],
$type
);
}

protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
Expand Down
20 changes: 20 additions & 0 deletions tests/Fixer/Phpdoc/PhpdocScalarFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,5 +269,25 @@ public static function provideFixCases(): iterable
*/
'),
];

yield [
'<?php /** @var array<int, bool> */',
'<?php /** @var array<integer, boolean> */',
];

yield [
'<?php /** @var array{bool, int, string} */',
'<?php /** @var array{boolean, integer, str} */',
];

yield [
'<?php /** @var array{int, array{string, bool}} */',
'<?php /** @var array{integer, array{str, boolean}} */',
];

yield [
'<?php /** @var array{index: int, isRequired: bool} */',
'<?php /** @var array{index: integer, isRequired: boolean} */',
];
}
}

0 comments on commit d12282c

Please sign in to comment.