Browse Source

ConfigInterface cleanup

SpacePossum 8 years ago
parent
commit
a3de8b12d4

+ 12 - 23
src/Config.php

@@ -19,23 +19,20 @@ namespace PhpCsFixer;
  */
 class Config implements ConfigInterface
 {
-    protected $name;
-    protected $description;
-    protected $finder;
-    protected $format = 'txt';
-    protected $dir;
-    protected $customFixers = array();
-    protected $usingCache = true;
-    protected $hideProgress = false;
-    protected $cacheFile = '.php_cs.cache';
-    protected $phpExecutable;
-    protected $isRiskyAllowed = false;
-    protected $rules = array('@PSR2' => true);
-
-    public function __construct($name = 'default', $description = 'A default configuration')
+    private $cacheFile = '.php_cs.cache';
+    private $customFixers = array();
+    private $finder;
+    private $format = 'txt';
+    private $hideProgress = false;
+    private $isRiskyAllowed = false;
+    private $name;
+    private $phpExecutable;
+    private $rules = array('@PSR2' => true);
+    private $usingCache = true;
+
+    public function __construct($name = 'default')
     {
         $this->name = $name;
-        $this->description = $description;
     }
 
     /**
@@ -62,14 +59,6 @@ class Config implements ConfigInterface
         return $this->customFixers;
     }
 
-    /**
-     * {@inheritdoc}
-     */
-    public function getDescription()
-    {
-        return $this->description;
-    }
-
     /**
      * {@inheritdoc}
      */

+ 4 - 13
src/ConfigInterface.php

@@ -21,7 +21,7 @@ interface ConfigInterface
     /**
      * Returns the path to the cache file.
      *
-     * @return string
+     * @return string|null Returns null if not using cache
      */
     public function getCacheFile();
 
@@ -32,19 +32,10 @@ interface ConfigInterface
      */
     public function getCustomFixers();
 
-    /**
-     * Returns the description of the configuration.
-     *
-     * A short one-line description for the configuration.
-     *
-     * @return string The description of the configuration
-     */
-    public function getDescription();
-
     /**
      * Returns files to scan.
      *
-     * @return iterable|Traversable|string[] $fixers
+     * @return iterable|\Traversable|string[]
      */
     public function getFinder();
 
@@ -102,7 +93,7 @@ interface ConfigInterface
     /**
      * Adds a suite of custom fixers.
      *
-     * @param iterable|Traversable|FixerInterface[] $fixers
+     * @param iterable|\Traversable|FixerInterface[] $fixers
      */
     public function registerCustomFixers($fixers);
 
@@ -116,7 +107,7 @@ interface ConfigInterface
     public function setCacheFile($cacheFile);
 
     /**
-     * @param iterable|Traversable|string[] $finder
+     * @param iterable|\Traversable|string[] $finder
      *
      * @return self
      */

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

@@ -320,8 +320,8 @@ EOF
 
         $configFile = $resolver->getConfigFile();
 
-        if (null !== $stdErr && $configFile) {
-            $stdErr->writeln(sprintf('Loaded config from "%s".', $configFile));
+        if (null !== $stdErr) {
+            $stdErr->writeln(sprintf('Loaded config <comment>%s</comment>%s.', $resolver->getConfig()->getName(), null === $configFile ? '' : ' from "'.$configFile.'"'));
         }
 
         if (null !== $stdErr && $resolver->getUsingCache()) {

+ 4 - 3
src/Console/ConfigurationResolver.php

@@ -12,6 +12,7 @@
 
 namespace PhpCsFixer\Console;
 
+use PhpCsFixer\Cache\CacheManagerInterface;
 use PhpCsFixer\Cache\FileCacheManager;
 use PhpCsFixer\Cache\FileHandler;
 use PhpCsFixer\Cache\NullCacheManager;
@@ -233,7 +234,7 @@ final class ConfigurationResolver
                     )
                 );
 
-                if (!empty($riskyFixers)) {
+                if (count($riskyFixers)) {
                     throw new InvalidConfigurationException(sprintf('The rules contain risky fixers (%s), but they are not allowed to run. Perhaps you forget to use --allow-risky option?', implode(', ', $riskyFixers)));
                 }
             }
@@ -527,7 +528,7 @@ final class ConfigurationResolver
             $this->getPath()
         ));
 
-        if (empty($paths)) {
+        if (!count($paths)) {
             if ($isIntersectionPathMode) {
                 return new \ArrayIterator(array());
             }
@@ -604,7 +605,7 @@ final class ConfigurationResolver
     /**
      * @param iterable $iterable
      *
-     * @return Traversable
+     * @return \Traversable
      */
     private function iterableToTraversable($iterable)
     {

+ 1 - 1
src/Report/ReportSummary.php

@@ -106,7 +106,7 @@ final class ReportSummary
     }
 
     /**
-     * @return time
+     * @return int
      */
     public function getTime()
     {

+ 25 - 0
tests/ConfigTest.php

@@ -13,7 +13,10 @@
 namespace PhpCsFixer\Tests;
 
 use PhpCsFixer\Config;
+use PhpCsFixer\Console\Command\FixCommand;
 use PhpCsFixer\Finder;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Tester\CommandTester;
 use Symfony\Component\Finder\Finder as SymfonyFinder;
 
 /**
@@ -21,6 +24,28 @@ use Symfony\Component\Finder\Finder as SymfonyFinder;
  */
 final class ConfigTest extends \PHPUnit_Framework_TestCase
 {
+    public function testCustomConfig()
+    {
+        $customConfigFile = __DIR__.'/Fixtures/.php_cs_custom.php';
+        $command = new FixCommand();
+        $commandTester = new CommandTester($command);
+        $commandTester->execute(
+            array(
+                'path' => array($customConfigFile),
+                '--dry-run' => true,
+                '--config' => $customConfigFile,
+            ),
+            array(
+                'decorated' => false,
+                'verbosity' => OutputInterface::VERBOSITY_VERY_VERBOSE,
+            )
+        );
+        $this->assertStringMatchesFormat(
+            sprintf('%%ALoaded config custom_config_test from "%s".%%A', $customConfigFile),
+            $commandTester->getDisplay(true)
+        );
+    }
+
     public function testThatFinderWorksWithDirSetOnConfig()
     {
         $config = new Config();

+ 178 - 0
tests/Fixtures/.php_cs_custom.php

@@ -0,0 +1,178 @@
+<?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.
+ */
+
+use PhpCsFixer\ConfigInterface;
+
+/**
+ * Custom config class/file for PHPUnit test.
+ *
+ * This class does NOT represent a good/sane configuration and is therefor NOT a example.
+ *
+ * @internal
+ *
+ * @author SpacePossum
+ */
+final class CustomConfig implements ConfigInterface
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function getCacheFile()
+    {
+        return null;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getCustomFixers()
+    {
+        return array();
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getFinder()
+    {
+        return array(__FILE__);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getFormat()
+    {
+        return 'txt';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getHideProgress()
+    {
+        return false;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getName()
+    {
+        return 'custom_config_test';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getPhpExecutable()
+    {
+        return null;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getRiskyAllowed()
+    {
+        return true;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getRules()
+    {
+        return array('concat_without_spaces' => true);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getUsingCache()
+    {
+        return false;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function registerCustomFixers($fixers)
+    {
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function setCacheFile($cacheFile)
+    {
+        return $this;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function setFinder($finder)
+    {
+        return $this;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function setFormat($format)
+    {
+        return $this;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function setHideProgress($hideProgress)
+    {
+        return $this;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function setPhpExecutable($phpExecutable)
+    {
+        return $this;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function setRiskyAllowed($isRiskyAllowed)
+    {
+        return $this;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function setRules(array $rules)
+    {
+        return $this;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function setUsingCache($usingCache)
+    {
+        return $this;
+    }
+}
+
+return new CustomConfig();