Browse Source

Merge branch '2.2'

Dariusz Ruminski 8 years ago
parent
commit
3804cd03cc

+ 1 - 0
README.rst

@@ -180,6 +180,7 @@ Complete configuration for rules can be supplied using a ``json`` formatted stri
 
 A combination of ``--dry-run`` and ``--diff`` will
 display a summary of proposed fixes, leaving your files unchanged.
+Optionally the ``--diff`` can be used to specify the diff. output format; ``--diff=sbd`` (default) or ``--diff=sbd-short``.
 
 The ``--allow-risky`` option allows you to set whether risky rules may run. Default value is taken from config file.
 Risky rule is a rule, which could change code behaviour. By default no risky rules are run.

+ 1 - 1
composer.json

@@ -17,7 +17,7 @@
         "php": "^5.3.6 || >=7.0 <7.2",
         "doctrine/annotations": "^1.2",
         "ext-tokenizer": "*",
-        "sebastian/diff": "^1.1",
+        "sebastian/diff": "^1.4",
         "symfony/console": "^2.4 || ^3.0",
         "symfony/event-dispatcher": "^2.1 || ^3.0",
         "symfony/filesystem": "^2.4 || ^3.0",

+ 1 - 0
src/Console/Command/CommandHelp.php

@@ -81,6 +81,7 @@ Complete configuration for rules can be supplied using a ``json`` formatted stri
 
 A combination of <comment>--dry-run</comment> and <comment>--diff</comment> will
 display a summary of proposed fixes, leaving your files unchanged.
+Optionally the <comment>--diff</comment> can be used to specify the diff. output format; ``--diff=sbd`` (default) or ``--diff=sbd-short``.
 
 The <comment>--allow-risky</comment> option allows you to set whether risky rules may run. Default value is taken from config file.
 Risky rule is a rule, which could change code behaviour. By default no risky rules are run.

+ 1 - 1
src/Console/Command/FixCommand.php

@@ -100,7 +100,7 @@ final class FixCommand extends Command
                     new InputOption('rules', '', InputOption::VALUE_REQUIRED, 'The rules.'),
                     new InputOption('using-cache', '', InputOption::VALUE_REQUIRED, 'Does cache should be used (can be yes or no).'),
                     new InputOption('cache-file', '', InputOption::VALUE_REQUIRED, 'The path to the cache file.'),
-                    new InputOption('diff', '', InputOption::VALUE_NONE, 'Also produce diff for each file.'),
+                    new InputOption('diff', '', InputOption::VALUE_OPTIONAL, 'Also produce diff for each file.', 'sbd'),
                     new InputOption('format', '', InputOption::VALUE_REQUIRED, 'To output results in other formats.'),
                     new InputOption('stop-on-violation', '', InputOption::VALUE_NONE, 'Stop execution on first violation.'),
                     new InputOption('show-progress', '', InputOption::VALUE_REQUIRED, 'Type of progress indicator (none, run-in, or estimating).'),

+ 25 - 6
src/Console/ConfigurationResolver.php

@@ -24,6 +24,7 @@ use PhpCsFixer\ConfigurationException\InvalidConfigurationException;
 use PhpCsFixer\Differ\DifferInterface;
 use PhpCsFixer\Differ\NullDiffer;
 use PhpCsFixer\Differ\SebastianBergmannDiffer;
+use PhpCsFixer\Differ\SebastianBergmannShortDiffer;
 use PhpCsFixer\Finder;
 use PhpCsFixer\Fixer\FixerInterface;
 use PhpCsFixer\FixerFactory;
@@ -103,18 +104,18 @@ final class ConfigurationResolver
      */
     private $options = array(
         'allow-risky' => null,
+        'cache-file' => null,
         'config' => null,
+        'diff' => null,
         'dry-run' => null,
         'format' => null,
         'path' => array(),
         'path-mode' => self::PATH_MODE_OVERRIDE,
-        'using-cache' => null,
-        'cache-file' => null,
         'rules' => null,
-        'diff' => null,
-        'verbosity' => null,
-        'stop-on-violation' => null,
         'show-progress' => null,
+        'stop-on-violation' => null,
+        'using-cache' => null,
+        'verbosity' => null,
     );
 
     private $cacheFile;
@@ -253,7 +254,25 @@ final class ConfigurationResolver
     public function getDiffer()
     {
         if (null === $this->differ) {
-            $this->differ = false === $this->options['diff'] ? new NullDiffer() : new SebastianBergmannDiffer();
+            switch ($this->options['diff']) {
+                case false:
+                    $this->differ = new NullDiffer();
+
+                    break;
+                case 'sbd':
+                    $this->differ = new SebastianBergmannDiffer();
+
+                    break;
+                case 'sbd-short':
+                    $this->differ = new SebastianBergmannShortDiffer();
+
+                    break;
+                default:
+                    throw new InvalidConfigurationException(sprintf(
+                        'Differ must be "sbd" or "sbd-short", got "%s".',
+                        $this->options['diff']
+                    ));
+            }
         }
 
         return $this->differ;

+ 39 - 0
src/Differ/SebastianBergmannShortDiffer.php

@@ -0,0 +1,39 @@
+<?php
+
+/*
+ * This file is part of PHP CS Fixer.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace PhpCsFixer\Differ;
+
+use SebastianBergmann\Diff\Differ;
+
+/**
+ * @author SpacePossum
+ */
+final class SebastianBergmannShortDiffer implements DifferInterface
+{
+    /**
+     * @var Differ
+     */
+    private $differ;
+
+    public function __construct()
+    {
+        $this->differ = new Differ("--- Original\n+++ New\n", false);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function diff($old, $new)
+    {
+        return $this->differ->diff($old, $new);
+    }
+}

+ 1 - 0
tests/AutoReview/ProjectCodeTest.php

@@ -44,6 +44,7 @@ final class ProjectCodeTest extends \PHPUnit_Framework_TestCase
         'PhpCsFixer\Differ\DiffConsoleFormatter',
         'PhpCsFixer\Differ\NullDiffer',
         'PhpCsFixer\Differ\SebastianBergmannDiffer',
+        'PhpCsFixer\Differ\SebastianBergmannShortDiffer',
         'PhpCsFixer\Doctrine\Annotation\Token',
         'PhpCsFixer\Doctrine\Annotation\Tokens',
         'PhpCsFixer\FileRemoval',

+ 55 - 0
tests/Console/ConfigurationResolverTest.php

@@ -1021,6 +1021,61 @@ final class ConfigurationResolverTest extends \PHPUnit_Framework_TestCase
         $this->assertSame('json', $resolver->getReporter()->getFormat());
     }
 
+    /**
+     * @param string      $expected
+     * @param string|bool $differConfig
+     *
+     * @dataProvider provideDifferCases
+     */
+    public function testResolveDiffer($expected, $differConfig)
+    {
+        $resolver = new ConfigurationResolver(
+            $this->config,
+            array('diff' => $differConfig),
+            ''
+        );
+
+        $this->assertInstanceOf($expected, $resolver->getDiffer());
+    }
+
+    public function provideDifferCases()
+    {
+        return array(
+            array(
+                '\PhpCsFixer\Differ\NullDiffer',
+                false,
+            ),
+            array(
+                '\PhpCsFixer\Differ\SebastianBergmannDiffer',
+                true,
+            ),
+            array(
+                '\PhpCsFixer\Differ\SebastianBergmannDiffer',
+                'sbd',
+            ),
+            array(
+                '\PhpCsFixer\Differ\SebastianBergmannShortDiffer',
+                'sbd-short',
+            ),
+        );
+    }
+
+    public function testUnknownDiffConfiguration()
+    {
+        $resolver = new ConfigurationResolver(
+            $this->config,
+            array('diff' => '_unknown_'),
+            ''
+        );
+
+        $this->setExpectedExceptionRegExp(
+            '\PhpCsFixer\ConfigurationException\InvalidConfigurationException',
+            '#^Differ must be "sbd" or "sbd-short", got "_unknown_"\.$#'
+        );
+
+        $resolver->getDiffer();
+    }
+
     public function testResolveConfigFileOverridesDefault()
     {
         $dir = __DIR__.'/../Fixtures/ConfigurationResolverConfigFile/case_8';