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

PhpdocTypesFixer - allow for configuration #4019

Merged
merged 1 commit into from
Oct 18, 2018
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
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1464,6 +1464,11 @@ Choose from the list of available rules:

The correct case must be used for standard PHP types in PHPDoc.

Configuration options:

- ``groups`` (a subset of ``['simple', 'alias', 'meta']``): type groups to fix;
defaults to ``['simple', 'alias', 'meta']``

* **phpdoc_types_order**

Sorts PHPDoc types.
Expand Down
102 changes: 73 additions & 29 deletions src/Fixer/Phpdoc/PhpdocTypesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,74 @@
namespace PhpCsFixer\Fixer\Phpdoc;

use PhpCsFixer\AbstractPhpdocTypesFixer;
use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
use PhpCsFixer\FixerConfiguration\AllowedValueSubset;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;

/**
* @author Graham Campbell <graham@alt-three.com>
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*/
final class PhpdocTypesFixer extends AbstractPhpdocTypesFixer
final class PhpdocTypesFixer extends AbstractPhpdocTypesFixer implements ConfigurationDefinitionFixerInterface
{
/**
* The types to process.
* Available types, grouped.
*
* @var string[]
* @var array<string,string[]>
*/
private static $types = [
'array',
'bool',
'boolean',
'callable',
'callback',
'double',
'false',
'float',
'int',
'integer',
'iterable',
'mixed',
'null',
'object',
'parent',
'real',
'resource',
'scalar',
'self',
'static',
'string',
'true',
'void',
'$this',
private static $possibleTypes = [
'simple' => [
'array',
'bool',
'callable',
'float',
'int',
'iterable',
'null',
'object',
'string',
],
'alias' => [
'boolean',
'callback',
'double',
'integer',
'real',
],
'meta' => [
'$this',
'false',
'mixed',
'parent',
'resource',
'scalar',
'self',
'static',
'true',
'void',
],
];

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

/**
* {@inheritdoc}
*/
public function configure(array $configuration = null)
{
parent::configure($configuration);

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

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -94,10 +122,26 @@ protected function normalize($type)
{
$lower = strtolower($type);

if (\in_array($lower, self::$types, true)) {
if (\in_array($lower, $this->typesToFix, true)) {
return $lower;
}

return $type;
}

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

return new FixerConfigurationResolver([
(new FixerOptionBuilder('groups', 'Type groups to fix.'))
->setAllowedTypes(['array'])
->setAllowedValues([new AllowedValueSubset($possibleGroups)])
->setDefault($possibleGroups)
->getOption(),
]);
}
}
35 changes: 35 additions & 0 deletions tests/Fixer/Phpdoc/PhpdocTypesFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

/**
* @author Graham Campbell <graham@alt-three.com>
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @internal
*
Expand Down Expand Up @@ -198,4 +199,38 @@ public function testInlineDoc()

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

public function testWithConfig()
{
$expected = <<<'EOF'
<?php
/**
* @param self|array|Foo $bar
*
* @return int|float|callback
*/

EOF;

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

EOF;

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

public function testWrongConfig()
{
$this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
$this->expectExceptionMessageRegExp('/^\[phpdoc_types\] Invalid configuration: The option "groups" .*\.$/');

$this->fixer->configure(['groups' => ['__TEST__']]);
}
}