Skip to content

Commit

Permalink
ConfigInterface cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
SpacePossum authored and keradus committed Nov 3, 2016
1 parent f0e959e commit a3de8b1
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 42 deletions.
35 changes: 12 additions & 23 deletions src/Config.php
Expand Up @@ -19,23 +19,20 @@
*/
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;
}

/**
Expand All @@ -62,14 +59,6 @@ public function getCustomFixers()
return $this->customFixers;
}

/**
* {@inheritdoc}
*/
public function getDescription()
{
return $this->description;
}

/**
* {@inheritdoc}
*/
Expand Down
17 changes: 4 additions & 13 deletions src/ConfigInterface.php
Expand Up @@ -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();

Expand All @@ -32,19 +32,10 @@ public function getCacheFile();
*/
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();

Expand Down Expand Up @@ -102,7 +93,7 @@ public function getUsingCache();
/**
* Adds a suite of custom fixers.
*
* @param iterable|Traversable|FixerInterface[] $fixers
* @param iterable|\Traversable|FixerInterface[] $fixers
*/
public function registerCustomFixers($fixers);

Expand All @@ -116,7 +107,7 @@ public function registerCustomFixers($fixers);
public function setCacheFile($cacheFile);

/**
* @param iterable|Traversable|string[] $finder
* @param iterable|\Traversable|string[] $finder
*
* @return self
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Console/Command/FixCommand.php
Expand Up @@ -320,8 +320,8 @@ protected function execute(InputInterface $input, OutputInterface $output)

$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()) {
Expand Down
7 changes: 4 additions & 3 deletions src/Console/ConfigurationResolver.php
Expand Up @@ -12,6 +12,7 @@

namespace PhpCsFixer\Console;

use PhpCsFixer\Cache\CacheManagerInterface;
use PhpCsFixer\Cache\FileCacheManager;
use PhpCsFixer\Cache\FileHandler;
use PhpCsFixer\Cache\NullCacheManager;
Expand Down Expand Up @@ -233,7 +234,7 @@ function (FixerInterface $fixer) {
)
);

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)));
}
}
Expand Down Expand Up @@ -527,7 +528,7 @@ function ($path) {
$this->getPath()
));

if (empty($paths)) {
if (!count($paths)) {
if ($isIntersectionPathMode) {
return new \ArrayIterator(array());
}
Expand Down Expand Up @@ -604,7 +605,7 @@ private function isStdIn()
/**
* @param iterable $iterable
*
* @return Traversable
* @return \Traversable
*/
private function iterableToTraversable($iterable)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Report/ReportSummary.php
Expand Up @@ -106,7 +106,7 @@ public function getMemory()
}

/**
* @return time
* @return int
*/
public function getTime()
{
Expand Down
25 changes: 25 additions & 0 deletions tests/ConfigTest.php
Expand Up @@ -13,14 +13,39 @@
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;

/**
* @internal
*/
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();
Expand Down
178 changes: 178 additions & 0 deletions 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();

0 comments on commit a3de8b1

Please sign in to comment.