Skip to content

Commit

Permalink
DX: new config filename
Browse files Browse the repository at this point in the history
  • Loading branch information
keradus committed Apr 13, 2021
1 parent 81a5c17 commit c704d13
Show file tree
Hide file tree
Showing 26 changed files with 143 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,4 +1,4 @@
/.php_cs
/.php-cs-fixer.php
/.phpunit.result.cache
/box.json
/composer.lock
Expand Down
63 changes: 63 additions & 0 deletions .php-cs-fixer.dist.php
@@ -0,0 +1,63 @@
<?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.
*/

$header = <<<'EOF'
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.
EOF;

$finder = PhpCsFixer\Finder::create()
->exclude('tests/Fixtures')
->in(__DIR__)
->append([
__DIR__.'/dev-tools/doc.php',
__DIR__.'/php-cs-fixer',
])
;

$config = new PhpCsFixer\Config();
$config
->setRiskyAllowed(true)
->setRules([
'@PHP56Migration' => true,
'@PHPUnit75Migration:risky' => true,
'@PhpCsFixer' => true,
'@PhpCsFixer:risky' => true,
'header_comment' => ['header' => $header],
'list_syntax' => ['syntax' => 'long'],
])
->setFinder($finder)
;

// special handling of fabbot.io service if it's using too old PHP CS Fixer version
if (false !== getenv('FABBOT_IO')) {
try {
PhpCsFixer\FixerFactory::create()
->registerBuiltInFixers()
->registerCustomFixers($config->getCustomFixers())
->useRuleSet(new PhpCsFixer\RuleSet($config->getRules()))
;
} catch (PhpCsFixer\ConfigurationException\InvalidConfigurationException $e) {
$config->setRules([]);
} catch (UnexpectedValueException $e) {
$config->setRules([]);
} catch (InvalidArgumentException $e) {
$config->setRules([]);
}
}

return $config;
15 changes: 14 additions & 1 deletion .php_cs.dist
@@ -1,5 +1,17 @@
<?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.
*/

// HARD copy of .php-cs-fixer.dist.php file for fabbot.io compatibility for PHP CS Fixer v2

$header = <<<'EOF'
This file is part of PHP CS Fixer.
Expand Down Expand Up @@ -39,7 +51,8 @@ if (false !== getenv('FABBOT_IO')) {
PhpCsFixer\FixerFactory::create()
->registerBuiltInFixers()
->registerCustomFixers($config->getCustomFixers())
->useRuleSet(new PhpCsFixer\RuleSet($config->getRules()));
->useRuleSet(new PhpCsFixer\RuleSet($config->getRules()))
;
} catch (PhpCsFixer\ConfigurationException\InvalidConfigurationException $e) {
$config->setRules([]);
} catch (UnexpectedValueException $e) {
Expand Down
4 changes: 2 additions & 2 deletions ci-integration.sh
Expand Up @@ -4,5 +4,5 @@ set -eu
IFS='
'
CHANGED_FILES=$(git diff --name-only --diff-filter=ACMRTUXB "${COMMIT_RANGE}")
if ! echo "${CHANGED_FILES}" | grep -qE "^(\\.php_cs(\\.dist)?|composer\\.lock)$"; then EXTRA_ARGS=$(printf -- '--path-mode=intersection\n--\n%s' "${CHANGED_FILES}"); else EXTRA_ARGS=''; fi
vendor/bin/php-cs-fixer fix --config=.php_cs.dist -v --dry-run --stop-on-violation --using-cache=no ${EXTRA_ARGS}
if ! echo "${CHANGED_FILES}" | grep -qE "^(\\.php-cs-fixer(\\.dist)?\\.php|composer\\.lock)$"; then EXTRA_ARGS=$(printf -- '--path-mode=intersection\n--\n%s' "${CHANGED_FILES}"); else EXTRA_ARGS=''; fi
vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php -v --dry-run --stop-on-violation --using-cache=no ${EXTRA_ARGS}
6 changes: 3 additions & 3 deletions doc/config.rst
Expand Up @@ -3,14 +3,14 @@ Config file
===========

Instead of using command line options to customize rules and rule sets, you can save the
project configuration in a ``.php_cs.dist`` file in the root directory of your project.
project configuration in a ``.php-cs-fixer.dist.php`` file in the root directory of your project.
The file must return an instance of `PhpCsFixer\\ConfigInterface <../src/ConfigInterface.php>`_
which lets you configure the rules, the files and directories that
need to be analyzed. You may also create ``.php_cs`` file, which is
need to be analyzed. You may also create ``.php-cs-fixer.php`` file, which is
the local configuration that will be used instead of the project configuration. It
is a good practice to add that file into your ``.gitignore`` file.
With the ``--config`` option you can specify the path to the
``.php_cs`` file.
``.php-cs-fixer.php`` file.

The example below will add two rules to the default list of PSR2 set rules:

Expand Down
4 changes: 2 additions & 2 deletions doc/usage.rst
Expand Up @@ -179,8 +179,8 @@ Then, add the following command to your CI:
$ IFS='
$ '
$ CHANGED_FILES=$(git diff --name-only --diff-filter=ACMRTUXB "${COMMIT_RANGE}")
$ if ! echo "${CHANGED_FILES}" | grep -qE "^(\\.php_cs(\\.dist)?|composer\\.lock)$"; then EXTRA_ARGS=$(printf -- '--path-mode=intersection\n--\n%s' "${CHANGED_FILES}"); else EXTRA_ARGS=''; fi
$ vendor/bin/php-cs-fixer fix --config=.php_cs.dist -v --dry-run --stop-on-violation --using-cache=no ${EXTRA_ARGS}
$ if ! echo "${CHANGED_FILES}" | grep -qE "^(\\.php-cs-fixer(\\.dist)?\\.php|composer\\.lock)$"; then EXTRA_ARGS=$(printf -- '--path-mode=intersection\n--\n%s' "${CHANGED_FILES}"); else EXTRA_ARGS=''; fi
$ vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php -v --dry-run --stop-on-violation --using-cache=no ${EXTRA_ARGS}
Where ``$COMMIT_RANGE`` is your range of commits, e.g. ``$TRAVIS_COMMIT_RANGE`` or ``HEAD~..HEAD``.

Expand Down
2 changes: 1 addition & 1 deletion src/Console/Command/FixCommand.php
Expand Up @@ -100,7 +100,7 @@ protected function configure()
new InputArgument('path', InputArgument::IS_ARRAY, 'The path.'),
new InputOption('path-mode', '', InputOption::VALUE_REQUIRED, 'Specify path mode (can be override or intersection).', 'override'),
new InputOption('allow-risky', '', InputOption::VALUE_REQUIRED, 'Are risky fixers allowed (can be yes or no).'),
new InputOption('config', '', InputOption::VALUE_REQUIRED, 'The path to a .php_cs file.'),
new InputOption('config', '', InputOption::VALUE_REQUIRED, 'The path to a .php-cs-fixer.php file.'),
new InputOption('dry-run', '', InputOption::VALUE_NONE, 'Only shows which files would have been modified.'),
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).'),
Expand Down
6 changes: 3 additions & 3 deletions src/Console/Command/HelpCommand.php
Expand Up @@ -167,14 +167,14 @@ public static function getHelpCopy()
-----------
Instead of using command line options to customize the rule, you can save the
project configuration in a <comment>.php_cs.dist</comment> file in the root directory of your project.
project configuration in a <comment>.php-cs-fixer.dist.php</comment> file in the root directory of your project.
The file must return an instance of `PhpCsFixer\ConfigInterface` (<url>%%%CONFIG_INTERFACE_URL%%%</url>)
which lets you configure the rules, the files and directories that
need to be analyzed. You may also create <comment>.php_cs</comment> file, which is
need to be analyzed. You may also create <comment>.php-cs-fixer.php</comment> file, which is
the local configuration that will be used instead of the project configuration. It
is a good practice to add that file into your <comment>.gitignore</comment> file.
With the <comment>--config</comment> option you can specify the path to the
<comment>.php_cs</comment> file.
<comment>.php-cs-fixer.php</comment> file.
The example below will add two rules to the default list of PSR2 set rules:
Expand Down
23 changes: 19 additions & 4 deletions src/Console/ConfigurationResolver.php
Expand Up @@ -227,6 +227,17 @@ public function getConfig()
continue;
}

$configFileBasename = basename($configFile);
$deprecatedConfigs = [
'.php_cs' => '.php-cs-fixer.php',
'.php_cs.dist' => '.php-cs-fixer.dist.php',
];

if (isset($deprecatedConfigs[$configFileBasename])) {
$message = "Configuration file `{$configFileBasename}` is deprecated, rename to `{$deprecatedConfigs[$configFileBasename]}`.";
Utils::triggerDeprecation($message, InvalidConfigurationException::class);
}

$config = self::separatedContextLessInclude($configFile);

// verify that the config has an instance of Config
Expand Down Expand Up @@ -605,13 +616,17 @@ private function computeConfigFiles()
}

$candidates = [
$configDir.\DIRECTORY_SEPARATOR.'.php_cs',
$configDir.\DIRECTORY_SEPARATOR.'.php_cs.dist',
$configDir.\DIRECTORY_SEPARATOR.'.php-cs-fixer.php',
$configDir.\DIRECTORY_SEPARATOR.'.php-cs-fixer.dist.php',
$configDir.\DIRECTORY_SEPARATOR.'.php_cs', // old v2 config, present here only to throw nice error message later
$configDir.\DIRECTORY_SEPARATOR.'.php_cs.dist', // old v2 config, present here only to throw nice error message later
];

if ($configDir !== $this->cwd) {
$candidates[] = $this->cwd.\DIRECTORY_SEPARATOR.'.php_cs';
$candidates[] = $this->cwd.\DIRECTORY_SEPARATOR.'.php_cs.dist';
$candidates[] = $this->cwd.\DIRECTORY_SEPARATOR.'.php-cs-fixer.php';
$candidates[] = $this->cwd.\DIRECTORY_SEPARATOR.'.php-cs-fixer.dist.php';
$candidates[] = $this->cwd.\DIRECTORY_SEPARATOR.'.php_cs'; // old v2 config, present here only to throw nice error message later
$candidates[] = $this->cwd.\DIRECTORY_SEPARATOR.'.php_cs.dist'; // old v2 config, present here only to throw nice error message later
}

return $candidates;
Expand Down
4 changes: 2 additions & 2 deletions tests/AutoReview/ProjectFixerConfigurationTest.php
Expand Up @@ -51,14 +51,14 @@ public function testRuleDefinedAlpha()
{
$rules = $rulesSorted = array_keys($this->loadConfig()->getRules());
sort($rulesSorted);
static::assertSame($rulesSorted, $rules, 'Please sort the "rules" in `.php_cs.dist` of this project.');
static::assertSame($rulesSorted, $rules, 'Please sort the "rules" in `.php-cs-fixer.dist.php` of this project.');
}

/**
* @return Config
*/
private function loadConfig()
{
return require __DIR__.'/../../.php_cs.dist';
return require __DIR__.'/../../.php-cs-fixer.dist.php';
}
}
2 changes: 1 addition & 1 deletion tests/ConfigTest.php
Expand Up @@ -94,7 +94,7 @@ public function testConfigRulesUsingInvalidJson()

public function testCustomConfig()
{
$customConfigFile = __DIR__.'/Fixtures/.php_cs_custom.php';
$customConfigFile = __DIR__.'/Fixtures/.php-cs-fixer.custom.php';

$application = new Application();
$application->add(new FixCommand(new ToolInfo()));
Expand Down

0 comments on commit c704d13

Please sign in to comment.