Skip to content

Commit

Permalink
DX: Add tests for type colon in backed enums (#6497)
Browse files Browse the repository at this point in the history
Add tests for backed enum types
  • Loading branch information
jrmajor committed Jul 21, 2022
1 parent 277c55a commit f408220
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion doc/list.rst
Expand Up @@ -2603,7 +2603,7 @@ List of Available Rules
`Source PhpCsFixer\\Fixer\\ReturnNotation\\ReturnAssignmentFixer <./../src/Fixer/ReturnNotation/ReturnAssignmentFixer.php>`_
- `return_type_declaration <./rules/function_notation/return_type_declaration.rst>`_

There should be one or no space before colon, and one space after it in return type declarations, according to configuration.
Adjust spacing around colon in return type declarations and backed enum types.

Rule is applied only in a PHP 7+ environment.

Expand Down
3 changes: 1 addition & 2 deletions doc/rules/function_notation/return_type_declaration.rst
Expand Up @@ -2,8 +2,7 @@
Rule ``return_type_declaration``
================================

There should be one or no space before colon, and one space after it in return
type declarations, according to configuration.
Adjust spacing around colon in return type declarations and backed enum types.

Description
-----------
Expand Down
2 changes: 1 addition & 1 deletion doc/rules/index.rst
Expand Up @@ -375,7 +375,7 @@ Function Notation
Callables must be called without using ``call_user_func*`` when possible.
- `return_type_declaration <./function_notation/return_type_declaration.rst>`_

There should be one or no space before colon, and one space after it in return type declarations, according to configuration.
Adjust spacing around colon in return type declarations and backed enum types.
- `single_line_throw <./function_notation/single_line_throw.rst>`_

Throwing exception must be done in single line.
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/FunctionNotation/ReturnTypeDeclarationFixer.php
Expand Up @@ -37,7 +37,7 @@ final class ReturnTypeDeclarationFixer extends AbstractFixer implements Configur
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'There should be one or no space before colon, and one space after it in return type declarations, according to configuration.',
'Adjust spacing around colon in return type declarations and backed enum types.',
[
new CodeSample(
"<?php\nfunction foo(int \$a):string {};\n"
Expand Down
17 changes: 17 additions & 0 deletions tests/Fixer/FunctionNotation/ReturnTypeDeclarationFixerTest.php
Expand Up @@ -172,4 +172,21 @@ public function provideFix80Cases(): iterable
'<?php class A { public function foo() :static{}}',
];
}

/**
* @dataProvider provideFix81Cases
* @requires PHP 8.1
*/
public function testFix81(string $expected, string $input): void
{
$this->doTest($expected, $input);
}

public function provideFix81Cases(): iterable
{
yield [
'<?php enum Foo: int {}',
'<?php enum Foo : int {}',
];
}
}
Expand Up @@ -3235,4 +3235,32 @@ public function provideTypeColonCases(): iterable
'<?php $a=1; $f = fn (): array => [];',
];
}

/**
* @dataProvider provideEnumTypeColonCases
* @requires PHP 8.1
*/
public function testEnumTypeColon(string $expected, string $input): void
{
$this->fixer->configure([
'constructs' => [
'type_colon',
],
]);

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

public function provideEnumTypeColonCases(): iterable
{
yield [
'<?php enum Foo: int {}',
"<?php enum Foo:\nint {}",
];

yield [
'<?php enum Foo: string {}',
'<?php enum Foo:string {}',
];
}
}

0 comments on commit f408220

Please sign in to comment.