Browse Source

bug #3673 PhpdocScalarFixer - Add "types" option (julienfalque, keradus)

This PR was squashed before being merged into the 2.11 branch (closes #3673).

Discussion
----------

PhpdocScalarFixer - Add "types" option

Closes #3536.

Commits
-------

663b414e PhpdocScalarFixer - Add \"types\" option
Dariusz Ruminski 7 years ago
parent
commit
4b6b53e11f
3 changed files with 60 additions and 3 deletions
  1. 5 0
      README.rst
  2. 23 2
      src/Fixer/Phpdoc/PhpdocScalarFixer.php
  3. 32 1
      tests/Fixer/Phpdoc/PhpdocScalarFixerTest.php

+ 5 - 0
README.rst

@@ -1274,6 +1274,11 @@ Choose from the list of available rules:
   Scalar types should always be written in the same form. ``int`` not
   ``integer``, ``bool`` not ``boolean``, ``float`` not ``real`` or ``double``.
 
+  Configuration options:
+
+  - ``types``: a map of types to fix; defaults to ``['boolean', 'double',
+    'integer', 'real', 'str']``
+
 * **phpdoc_separation** [@Symfony]
 
   Annotations in phpdocs should be grouped together so that annotations of

+ 23 - 2
src/Fixer/Phpdoc/PhpdocScalarFixer.php

@@ -13,13 +13,17 @@
 namespace PhpCsFixer\Fixer\Phpdoc;
 
 use PhpCsFixer\AbstractPhpdocTypesFixer;
+use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
+use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
+use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
+use PhpCsFixer\FixerConfiguration\FixerOptionValidatorGenerator;
 use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
 
 /**
  * @author Graham Campbell <graham@alt-three.com>
  */
-final class PhpdocScalarFixer extends AbstractPhpdocTypesFixer
+final class PhpdocScalarFixer extends AbstractPhpdocTypesFixer implements ConfigurationDefinitionFixerInterface
 {
     /**
      * The types to fix.
@@ -72,12 +76,29 @@ function sample($a, $b, $c)
         return 15;
     }
 
+    /**
+     * {@inheritdoc}
+     */
+    protected function createConfigurationDefinition()
+    {
+        $generator = new FixerOptionValidatorGenerator();
+
+        return new FixerConfigurationResolver([
+            (new FixerOptionBuilder('types', 'A map of types to fix.'))
+                ->setAllowedValues([
+                    $generator->allowedValueIsSubsetOf(array_keys(self::$types)),
+                ])
+                ->setDefault(['boolean', 'double', 'integer', 'real', 'str']) // TODO 3.0 add "callback"
+                ->getOption(),
+        ]);
+    }
+
     /**
      * {@inheritdoc}
      */
     protected function normalize($type)
     {
-        if (array_key_exists($type, self::$types)) {
+        if (in_array($type, $this->configuration['types'], true)) {
             return self::$types[$type];
         }
 

+ 32 - 1
tests/Fixer/Phpdoc/PhpdocScalarFixerTest.php

@@ -52,7 +52,7 @@ EOF;
 /**
  * @method int foo()
  * @property int $foo
- * @property callable $foo
+ * @property callback $foo
  * @property-read bool $bar
  * @property-write float $baz
  */
@@ -319,4 +319,35 @@ EOF;
 
         $this->doTest($expected, $input);
     }
+
+    public function testFixCallback()
+    {
+        $expected = <<<'EOF'
+<?php
+/**
+ * @method int foo()
+ * @property int $foo
+ * @property callable $foo
+ * @property-read bool $bar
+ * @property-write float $baz
+ */
+
+EOF;
+
+        $input = <<<'EOF'
+<?php
+/**
+ * @method integer foo()
+ * @property integer $foo
+ * @property callback $foo
+ * @property-read boolean $bar
+ * @property-write double $baz
+ */
+
+EOF;
+
+        $this->fixer->configure(['types' => ['boolean', 'callback', 'double', 'integer', 'real', 'str']]);
+
+        $this->doTest($expected, $input);
+    }
 }