Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .dev-tools/src/Readme/ReadmeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,19 @@ private function fixers(): string
$fixer->configure(['force' => true]);
}
if ($fixer->isRisky()) {
$riskyDescription = $fixer->getDefinition()->getRiskyDescription();
$starts = [
'Risky when' => 'when',
'Fixer could be risky if' => 'when',
];
foreach ($starts as $from => $to) {
if (\str_starts_with($riskyDescription, $from)) {
$riskyDescription = $to . \substr($riskyDescription, \strlen($from));
}
}
$output .= \sprintf(
"\n *Risky: %s.*",
$fixer->getDefinition()->getRiskyDescription(),
\lcfirst(\rtrim($riskyDescription, '.')),
);
}
if ($fixer instanceof DataProviderStaticFixer) {
Expand Down
46 changes: 25 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ Constructor's empty braces must be on a single line.
#### DataProviderNameFixer
Data provider names must match the name of the test.
DEPRECATED: use `php_unit_data_provider_name` instead.
*Risky: when relying on name of data provider function.*
*Risky: when one is calling data provider by name as function.*
Configuration options:
- `prefix` (`string`): prefix that replaces "test"; defaults to `'provide'`
- `suffix` (`string`): suffix to be added at the end"; defaults to `'Cases'`
- `suffix` (`string`): suffix to be present at the end; defaults to `'Cases'`
```diff
<?php
class FooTest extends TestCase {
Expand All @@ -114,7 +114,7 @@ Configuration options:
#### DataProviderReturnTypeFixer
The return type of PHPUnit data provider must be `iterable`.
DEPRECATED: use `php_unit_data_provider_return_type` instead.
*Risky: when relying on signature of data provider.*
*Risky: when relying on signature of the data provider.*
```diff
<?php
class FooTest extends TestCase {
Expand All @@ -130,9 +130,9 @@ The return type of PHPUnit data provider must be `iterable`.
#### DataProviderStaticFixer
Data providers must be static.
DEPRECATED: use `php_unit_data_provider_static` instead.
*Risky: when `force` is set to `true`.*
*Risky: when one is calling data provider function dynamically.*
Configuration options:
- `force` (`bool`): whether to make static data providers having dynamic class calls; defaults to `false`
- `force` (`bool`): whether to make the data providers static even if they have a dynamic class call (may introduce fatal error "using $this when not in object context", and you may have to adjust the code manually by converting dynamic calls to static ones); defaults to `false`
```diff
<?php
class FooTest extends TestCase {
Expand Down Expand Up @@ -178,12 +178,12 @@ Value from `foreach` must not be used if possible.
```

#### InternalClassCasingFixer
Classes defined internally by an extension or the core must be referenced with the correct case.
When referencing an internal class it must be written using the correct casing.
DEPRECATED: use `class_reference_name_casing` instead.
```diff
<?php
-$foo = new STDClass();
+$foo = new stdClass();
-throw new \exception();
+throw new \Exception();
```

#### IssetToArrayKeyExistsFixer
Expand Down Expand Up @@ -513,15 +513,16 @@ Assertions and attributes for PHP and PHPUnit versions must have explicit versio
```

#### PhpdocArrayStyleFixer
Generic array style should be used in PHPDoc.
PHPDoc `array<T>` type must be used instead of `T[]`.
DEPRECATED: use `phpdoc_array_type` instead.
```diff
<?php
/**
- * @return int[]
+ * @return array<int>
- * @param int[] $x
- * @param string[][] $y
+ * @param array<int> $x
+ * @param array<array<string>> $y
*/
function foo() { return [1, 2]; }
```

#### PhpdocNoIncorrectVarAnnotationFixer
Expand Down Expand Up @@ -566,12 +567,14 @@ Orders all `@param` annotations in DocBlocks according to method signature.
```diff
<?php
/**
+ * @param int $a
* @param int $b
- * @param int $a
* @param int $c
* Annotations in wrong order
*
* @param int $a
+ * @param array $b
* @param Foo $c
- * @param array $b
*/
function foo($a, $b, $c) {}
function m($a, array $b, Foo $c) {}
```

#### PhpdocParamTypeFixer
Expand Down Expand Up @@ -630,15 +633,16 @@ Configuration options:
```

#### PhpdocTypeListFixer
PHPDoc type `list` must be used instead of `array` without a key type.
PHPDoc `list` type must be used instead of `array` without a key.
DEPRECATED: use `phpdoc_list_type` instead.
```diff
<?php
/**
- * @param array<string>
+ * @param list<string>
- * @param array<int> $x
- * @param array<array<string>> $y
+ * @param list<int> $x
+ * @param list<list<string>> $y
*/
function foo($x) {}
```

#### PhpdocTypesCommaSpacesFixer
Expand Down
44 changes: 3 additions & 41 deletions src/Fixer/DataProviderNameFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,20 @@
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\Fixer\DeprecatedFixerInterface;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitDataProviderNameFixer;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Tokens;

/**
* @deprecated
*
* @implements ConfigurableFixerInterface<_InputConfig, _Config>
*
* @phpstan-type _InputConfig array{prefix?: string, suffix?: string}
* @phpstan-type _Config array{prefix: string, suffix: string}
* @implements ConfigurableFixerInterface<array{prefix?: string, suffix?: string}, array{prefix: string, suffix: string}>
*
* @no-named-arguments
*/
final class DataProviderNameFixer extends AbstractFixer implements ConfigurableFixerInterface, DeprecatedFixerInterface
{
private PhpUnitDataProviderNameFixer $phpUnitDataProviderNameFixer;
private string $prefix = 'provide';
private string $suffix = 'Cases';

public function __construct()
{
Expand All @@ -45,38 +36,12 @@ public function __construct()

public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
$this->phpUnitDataProviderNameFixer->getDefinition()->getSummary(),
[
new CodeSample(
'<?php
class FooTest extends TestCase {
/**
* @dataProvider dataProvider
*/
public function testSomething($expected, $actual) {}
public function dataProvider() {}
}
',
),
],
'',
'when relying on name of data provider function',
);
return $this->phpUnitDataProviderNameFixer->getDefinition();
}

public function getConfigurationDefinition(): FixerConfigurationResolverInterface
{
return new FixerConfigurationResolver([
(new FixerOptionBuilder('prefix', 'prefix that replaces "test"'))
->setAllowedTypes(['string'])
->setDefault($this->prefix)
->getOption(),
(new FixerOptionBuilder('suffix', 'suffix to be added at the end"'))
->setAllowedTypes(['string'])
->setDefault($this->suffix)
->getOption(),
]);
return $this->phpUnitDataProviderNameFixer->getConfigurationDefinition();
}

public function configure(array $configuration): void
Expand Down Expand Up @@ -104,9 +69,6 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
$this->phpUnitDataProviderNameFixer->fix($file, $tokens);
}

/**
* @return list<string>
*/
public function getSuccessorsNames(): array
{
return [$this->phpUnitDataProviderNameFixer->getName()];
Expand Down
24 changes: 1 addition & 23 deletions src/Fixer/DataProviderReturnTypeFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

use PhpCsFixer\Fixer\DeprecatedFixerInterface;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitDataProviderReturnTypeFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Tokens;

Expand All @@ -34,24 +32,7 @@ public function __construct()

public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
$this->phpUnitDataProviderReturnTypeFixer->getDefinition()->getSummary(),
[
new CodeSample(
'<?php
class FooTest extends TestCase {
/**
* @dataProvider provideSomethingCases
*/
public function testSomething($expected, $actual) {}
public function provideSomethingCases(): array {}
}
',
),
],
'',
'when relying on signature of data provider',
);
return $this->phpUnitDataProviderReturnTypeFixer->getDefinition();
}

/**
Expand All @@ -77,9 +58,6 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
$this->phpUnitDataProviderReturnTypeFixer->fix($file, $tokens);
}

/**
* @return list<string>
*/
public function getSuccessorsNames(): array
{
return [$this->phpUnitDataProviderReturnTypeFixer->getName()];
Expand Down
47 changes: 4 additions & 43 deletions src/Fixer/DataProviderStaticFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,19 @@
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\Fixer\DeprecatedFixerInterface;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitDataProviderStaticFixer;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Tokens;

/**
* @deprecated
*
* @implements ConfigurableFixerInterface<_InputConfig, _Config>
*
* @phpstan-type _InputConfig array{force?: bool}
* @phpstan-type _Config array{force: bool}
* @implements ConfigurableFixerInterface<array{force?: bool}, array{force: bool}>
*
* @no-named-arguments
*/
final class DataProviderStaticFixer extends AbstractFixer implements ConfigurableFixerInterface, DeprecatedFixerInterface
{
private bool $force = false;
private PhpUnitDataProviderStaticFixer $phpUnitDataProviderStaticFixer;

public function __construct()
Expand All @@ -44,45 +36,17 @@ public function __construct()

public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
$this->phpUnitDataProviderStaticFixer->getDefinition()->getSummary(),
[
new CodeSample(
'<?php
class FooTest extends TestCase {
/**
* @dataProvider provideSomethingCases
*/
public function testSomething($expected, $actual) {}
public function provideSomethingCases() {}
}
',
),
],
'',
'when `force` is set to `true`',
);
return $this->phpUnitDataProviderStaticFixer->getDefinition();
}

public function getConfigurationDefinition(): FixerConfigurationResolverInterface
{
return new FixerConfigurationResolver([
(new FixerOptionBuilder('force', 'whether to make static data providers having dynamic class calls'))
->setAllowedTypes(['bool'])
->setDefault($this->force)
->getOption(),
]);
return $this->phpUnitDataProviderStaticFixer->getConfigurationDefinition();
}

/**
* @param array<string, bool> $configuration
*/
public function configure(array $configuration): void
{
if (\array_key_exists('force', $configuration)) {
$this->force = $configuration['force'];
}
$this->phpUnitDataProviderStaticFixer->configure(['force' => $this->force]);
$this->phpUnitDataProviderStaticFixer->configure($configuration);
}

public function getPriority(): int
Expand All @@ -105,9 +69,6 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
$this->phpUnitDataProviderStaticFixer->fix($file, $tokens);
}

/**
* @return list<string>
*/
public function getSuccessorsNames(): array
{
return [$this->phpUnitDataProviderStaticFixer->getName()];
Expand Down
11 changes: 1 addition & 10 deletions src/Fixer/InternalClassCasingFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

use PhpCsFixer\Fixer\Casing\ClassReferenceNameCasingFixer;
use PhpCsFixer\Fixer\DeprecatedFixerInterface;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Tokens;

Expand All @@ -34,11 +32,7 @@ public function __construct()

public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'Classes defined internally by an extension or the core must be referenced with the correct case.',
[new CodeSample("<?php\n\$foo = new STDClass();\n")],
'',
);
return $this->classReferenceNameCasingFixer->getDefinition();
}

public function getPriority(): int
Expand All @@ -61,9 +55,6 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
$this->classReferenceNameCasingFixer->fix($file, $tokens);
}

/**
* @return list<string>
*/
public function getSuccessorsNames(): array
{
return [$this->classReferenceNameCasingFixer->getName()];
Expand Down
Loading