Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: PhpdocTypesFixer - do not change case of array keys #6810

Merged
merged 4 commits into from
Mar 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Fixer/Phpdoc/PhpdocTypesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function configure(array $configuration): void
}, $this->configuration['groups']));

$this->patternToFix = sprintf(
'/(?<![a-zA-Z0-9_\x80-\xff]\\\\)(\b|.(?=\$))(%s)\b(?!\\\\)/i',
'/(?<![a-zA-Z0-9_\x80-\xff]\\\\)(\b|.(?=\$))(%s)\b(?!(\\\\|:))/i',
implode(
'|',
array_map(
Expand Down
196 changes: 77 additions & 119 deletions tests/Fixer/Phpdoc/PhpdocTypesFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,186 +30,150 @@ final class PhpdocTypesFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*
* @param array<string, mixed> $configuration
*/
public function testFix(string $expected, ?string $input = null): void
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);

$this->doTest($expected, $input);
}

public function testWindowsLinebreaks(): void
public static function provideFixCases(): iterable
{
$this->doTest(
yield 'windows line breaks' => [
"<?php /**\r\n * @param string|string[] \$bar\r\n *\r\n * @return int[]\r\n */\r\n",
"<?php /**\r\n * @param STRING|String[] \$bar\r\n *\r\n * @return inT[]\r\n */\r\n"
);
}
"<?php /**\r\n * @param STRING|String[] \$bar\r\n *\r\n * @return inT[]\r\n */\r\n",
];

public function testConversion(): void
{
$expected = <<<'EOF'
<?php
yield 'conversion' => [
'<?php
/**
* @param boolean|array|Foo $bar
*
* @return int|float
*/

EOF;

$input = <<<'EOF'
<?php
',
'<?php
/**
* @param Boolean|Array|Foo $bar
*
* @return inT|Float
*/

EOF;
$this->doTest($expected, $input);
}
',
];

public function testArrayStuff(): void
{
$expected = <<<'EOF'
<?php
yield 'array stuff' => [
'<?php
/**
* @param string|string[] $bar
*
* @return int[]
*/

EOF;

$input = <<<'EOF'
<?php
',
'<?php
/**
* @param STRING|String[] $bar
*
* @return inT[]
*/

EOF;
$this->doTest($expected, $input);
}
',
];

public function testNestedArrayStuff(): void
{
$expected = <<<'EOF'
<?php
yield 'nested array stuff' => [
'<?php
/**
* @return int[][][]
*/
EOF;

$input = <<<'EOF'
<?php
',
'<?php
/**
* @return INT[][][]
*/
EOF;
$this->doTest($expected, $input);
}
',
];

public function testMixedAndVoid(): void
{
$expected = <<<'EOF'
<?php
yield 'mixed and void' => [
'<?php
/**
* @param mixed $foo
*
* @return void
*/

EOF;

$input = <<<'EOF'
<?php
',
'<?php
/**
* @param Mixed $foo
*
* @return Void
*/

EOF;
$this->doTest($expected, $input);
}
',
];

public function testIterableFix(): void
{
$expected = <<<'EOF'
<?php
yield 'iterable' => [
'<?php
/**
* @param iterable $foo
*
* @return Itter
*/

EOF;

$input = <<<'EOF'
<?php
',
'<?php
/**
* @param Iterable $foo
*
* @return Itter
*/

EOF;
$this->doTest($expected, $input);
}
',
];

public function testMethodAndPropertyFix(): void
{
$expected = <<<'EOF'
<?php
yield 'method and property' => [
'<?php
/**
* @method self foo()
* @property int $foo
* @property-read boolean $bar
* @property-write mixed $baz
*/

EOF;

$input = <<<'EOF'
<?php
',
'<?php
/**
* @method Self foo()
* @property Int $foo
* @property-read Boolean $bar
* @property-write MIXED $baz
*/

EOF;

$this->doTest($expected, $input);
}
',
];

public function testThrows(): void
{
$expected = <<<'EOF'
<?php
yield 'throws' => [
'<?php
/**
* @throws static
*/

EOF;

$input = <<<'EOF'
<?php
',
'<?php
/**
* @throws STATIC
*/

EOF;

$this->doTest($expected, $input);
}
',
];

public function testInlineDoc(): void
{
$expected = <<<'EOF'
<?php
yield 'inline doc' => [
'<?php
/**
* Does stuff with stuffs.
*
Expand All @@ -219,10 +183,8 @@ public function testInlineDoc(): void
* }
*/

EOF;

$input = <<<'EOF'
<?php
',
'<?php
/**
* Does stuff with stuffs.
*
Expand All @@ -232,41 +194,30 @@ public function testInlineDoc(): void
* }
*/

EOF;

$this->doTest($expected, $input);
}
',
];

public function testWithConfig(): void
{
$expected = <<<'EOF'
<?php
yield 'with config' => [
'<?php
/**
* @param self|array|Foo $bar
*
* @return int|float|callback
*/

EOF;

$input = <<<'EOF'
<?php
',
'<?php
/**
* @param SELF|Array|Foo $bar
*
* @return inT|Float|callback
*/

EOF;

$this->fixer->configure(['groups' => ['simple', 'meta']]);
$this->doTest($expected, $input);
}
',
['groups' => ['simple', 'meta']],
];

public function testGenerics(): void
{
$this->fixer->configure(['groups' => ['simple', 'meta']]);
$this->doTest(
yield 'generics' => [
'<?php
/**
* @param array<int, object> $a
Expand All @@ -288,12 +239,10 @@ public function testGenerics(): void
* @param iterable<BOOLBOOLBOOL|INTINTINT|ARRAY_BOOL_INT_STRING_> $thisShouldNotBeChangedNeither
*
* @return ARRAY<INT, ARRAY<STRING, ARRAY<INT, DoNotChangeThisAsThisIsAClass>>>
*/'
);
}
*/',
['groups' => ['simple', 'meta']],
];

public static function provideFixCases(): iterable
{
yield 'callable' => [
'<?php /**
* @param callable() $a
Expand All @@ -306,6 +255,15 @@ public static function provideFixCases(): iterable
* @param CALLABLE(BOOL, INT, STRING): FLOAT $c
*/',
];

yield 'array shape with key name being also type name' => [
'<?php /**
* @return array{FOO: bool, NULL: null|int, BAR: string|BAZ}
*/',
'<?php /**
* @return array{FOO: BOOL, NULL: NULL|INT, BAR: STRING|BAZ}
*/',
];
}

public function testWrongConfig(): void
Expand Down