Skip to content

Commit

Permalink
[BUGFIX] Use recommended naming for config
Browse files Browse the repository at this point in the history
The PHP CS-Fixer docs recommends using .php-cs-fixer.dist.php to commit
to the repository and using .php-cs-fixer.php for a local override see
https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/07430f5aca4874476bf175317cfb6284cd80421a/doc/config.rst
  • Loading branch information
gilbertsoft committed Nov 17, 2021
1 parent 3bc03c6 commit 2ae91cd
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 18 deletions.
17 changes: 11 additions & 6 deletions src/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,24 @@ private function copyPhpCsFixerConfig(bool $force, string $typePrefix): bool
{
$errors = false;

if (!$force && file_exists($this->rootPath . '/.php_cs') && !file_exists($this->rootPath . '/.php-cs-fixer.php')) {
rename($this->rootPath . '/.php_cs', $this->rootPath . '/.php-cs-fixer.php');
echo "Found deprecated .php_cs file and renamed it to .php-cs-fixer.php.\n";
if (!$force && file_exists($this->rootPath . '/.php_cs') && !file_exists($this->rootPath . '/.php-cs-fixer.dist.php')) {
rename($this->rootPath . '/.php_cs', $this->rootPath . '/.php-cs-fixer.dist.php');
echo "Found deprecated .php_cs file and renamed it to .php-cs-fixer.dist.php.\n";
}

if (!$force && file_exists($this->rootPath . '/.php-cs-fixer.php') && !file_exists($this->rootPath . '/.php-cs-fixer.dist.php')) {
rename($this->rootPath . '/.php-cs-fixer.php', $this->rootPath . '/.php-cs-fixer.dist.php');
echo "Found .php-cs-fixer.php file and renamed it to .php-cs-fixer.dist.php.\n";
}

if (
!$force
&& (file_exists($this->rootPath . '/.php_cs') || file_exists($this->rootPath . '/.php-cs-fixer.php'))
&& (file_exists($this->rootPath . '/.php_cs') || file_exists($this->rootPath . '/.php-cs-fixer.dist.php'))
) {
echo "A .php-cs-fixer.php file already exists in your main folder, but the -f option was not set. Nothing copied.\n";
echo "A .php-cs-fixer.dist.php file already exists in your main folder, but the -f option was not set. Nothing copied.\n";
$errors = true;
} else {
copy($this->templatesPath . '/' . $typePrefix . '_php-cs-fixer.dist.php', $this->rootPath . '/.php-cs-fixer.php');
copy($this->templatesPath . '/' . $typePrefix . '_php-cs-fixer.dist.php', $this->rootPath . '/.php-cs-fixer.dist.php');

if (file_exists($this->rootPath . '/.php_cs')) {
unlink($this->rootPath . '/.php_cs');
Expand Down
64 changes: 52 additions & 12 deletions tests/Unit/SetupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ public function testForExtensionScenarios(
public function scenariosProvider(): \Generator
{
$editorconfigWarning = "A .editorconfig file already exists in your main folder, but the -f option was not set. Nothing copied.\n";
$phpcsInformation = "Found deprecated .php_cs file and renamed it to .php-cs-fixer.php.\n";
$phpcsWarning = "A .php-cs-fixer.php file already exists in your main folder, but the -f option was not set. Nothing copied.\n";
$phpcsFoundDeprecatedInformation = "Found deprecated .php_cs file and renamed it to .php-cs-fixer.dist.php.\n";
$phpcsFoundInformation = "Found .php-cs-fixer.php file and renamed it to .php-cs-fixer.dist.php.\n";
$phpcsWarning = "A .php-cs-fixer.dist.php file already exists in your main folder, but the -f option was not set. Nothing copied.\n";

yield 'all files are created' => [
'existingFiles' => [],
Expand All @@ -119,13 +120,15 @@ public function scenariosProvider(): \Generator
'expectedOutput' => '',
'expectedFiles' => [
'.editorconfig' => 'TPL:editorconfig.dist',
'.php-cs-fixer.php' => 'TPL:{$typePrefix}_php-cs-fixer.dist.php',
'.php-cs-fixer.dist.php' => 'TPL:{$typePrefix}_php-cs-fixer.dist.php',
'.php-cs-fixer.php' => false,
'.php_cs' => false,
],
];
yield 'files are not overwritten' => [
'existingFiles' => [
'.editorconfig' => 'FIX:editorconfig.dist',
'.php-cs-fixer.dist.php' => 'FIX:php-cs-fixer.dist.php',
'.php-cs-fixer.php' => 'FIX:php-cs-fixer.dist.php',
'.php_cs' => 'FIX:php-cs-fixer.dist.php',
],
Expand All @@ -134,6 +137,7 @@ public function scenariosProvider(): \Generator
'expectedOutput' => $editorconfigWarning . $phpcsWarning,
'expectedFiles' => [
'.editorconfig' => 'FIX:editorconfig.dist',
'.php-cs-fixer.dist.php' => 'FIX:php-cs-fixer.dist.php',
'.php-cs-fixer.php' => 'FIX:php-cs-fixer.dist.php',
'.php_cs' => 'FIX:php-cs-fixer.dist.php',
],
Expand All @@ -147,20 +151,36 @@ public function scenariosProvider(): \Generator
'expectedOutput' => $editorconfigWarning,
'expectedFiles' => [
'.editorconfig' => 'FIX:editorconfig.dist',
'.php-cs-fixer.php' => 'TPL:{$typePrefix}_php-cs-fixer.dist.php',
'.php-cs-fixer.dist.php' => 'TPL:{$typePrefix}_php-cs-fixer.dist.php',
'.php-cs-fixer.php' => false,
'.php_cs' => false,
],
];
yield 'php-cs-fixer.php is not overwritten' => [
yield 'php-cs-fixer.dist.php is not overwritten' => [
'existingFiles' => [
'.php-cs-fixer.php' => 'FIX:php-cs-fixer.dist.php',
'.php-cs-fixer.dist.php' => 'FIX:php-cs-fixer.dist.php',
],
'force' => false,
'expectedResult' => 1,
'expectedOutput' => $phpcsWarning,
'expectedFiles' => [
'.editorconfig' => 'TPL:editorconfig.dist',
'.php-cs-fixer.dist.php' => 'FIX:php-cs-fixer.dist.php',
'.php-cs-fixer.php' => false,
'.php_cs' => false,
],
];
yield 'php-cs-fixer.php is not overwritten' => [
'existingFiles' => [
'.php-cs-fixer.php' => 'FIX:php-cs-fixer.dist.php',
],
'force' => false,
'expectedResult' => 1,
'expectedOutput' => $phpcsFoundInformation . $phpcsWarning,
'expectedFiles' => [
'.editorconfig' => 'TPL:editorconfig.dist',
'.php-cs-fixer.dist.php' => 'FIX:php-cs-fixer.dist.php',
'.php-cs-fixer.php' => false,
'.php_cs' => false,
],
];
Expand All @@ -170,16 +190,18 @@ public function scenariosProvider(): \Generator
],
'force' => false,
'expectedResult' => 1,
'expectedOutput' => $phpcsInformation . $phpcsWarning,
'expectedOutput' => $phpcsFoundDeprecatedInformation . $phpcsWarning,
'expectedFiles' => [
'.editorconfig' => 'TPL:editorconfig.dist',
'.php-cs-fixer.php' => 'FIX:php-cs-fixer.dist.php',
'.php-cs-fixer.dist.php' => 'FIX:php-cs-fixer.dist.php',
'.php-cs-fixer.php' => false,
'.php_cs' => false,
],
];
yield 'all files are overwritten' => [
'existingFiles' => [
'.editorconfig' => 'FIX:editorconfig.dist',
'.php-cs-fixer.dist.php' => 'FIX:php-cs-fixer.dist.php',
'.php-cs-fixer.php' => 'FIX:php-cs-fixer.dist.php',
'.php_cs' => 'FIX:php-cs-fixer.dist.php',
],
Expand All @@ -188,7 +210,8 @@ public function scenariosProvider(): \Generator
'expectedOutput' => '',
'expectedFiles' => [
'.editorconfig' => 'TPL:editorconfig.dist',
'.php-cs-fixer.php' => 'TPL:{$typePrefix}_php-cs-fixer.dist.php',
'.php-cs-fixer.dist.php' => 'TPL:{$typePrefix}_php-cs-fixer.dist.php',
'.php-cs-fixer.php' => 'FIX:php-cs-fixer.dist.php',
'.php_cs' => false,
],
];
Expand All @@ -201,11 +224,26 @@ public function scenariosProvider(): \Generator
'expectedOutput' => '',
'expectedFiles' => [
'.editorconfig' => 'TPL:editorconfig.dist',
'.php-cs-fixer.php' => 'TPL:{$typePrefix}_php-cs-fixer.dist.php',
'.php-cs-fixer.dist.php' => 'TPL:{$typePrefix}_php-cs-fixer.dist.php',
'.php-cs-fixer.php' => false,
'.php_cs' => false,
],
];
yield 'php-cs-fixer.dist.php is overwritten' => [
'existingFiles' => [
'.php-cs-fixer.dist.php' => 'FIX:php-cs-fixer.dist.php',
],
'force' => true,
'expectedResult' => 0,
'expectedOutput' => '',
'expectedFiles' => [
'.editorconfig' => 'TPL:editorconfig.dist',
'.php-cs-fixer.dist.php' => 'TPL:{$typePrefix}_php-cs-fixer.dist.php',
'.php-cs-fixer.php' => false,
'.php_cs' => false,
],
];
yield 'php-cs-fixer.php is preserved' => [
'existingFiles' => [
'.php-cs-fixer.php' => 'FIX:php-cs-fixer.dist.php',
],
Expand All @@ -214,7 +252,8 @@ public function scenariosProvider(): \Generator
'expectedOutput' => '',
'expectedFiles' => [
'.editorconfig' => 'TPL:editorconfig.dist',
'.php-cs-fixer.php' => 'TPL:{$typePrefix}_php-cs-fixer.dist.php',
'.php-cs-fixer.dist.php' => 'TPL:{$typePrefix}_php-cs-fixer.dist.php',
'.php-cs-fixer.php' => 'FIX:php-cs-fixer.dist.php',
'.php_cs' => false,
],
];
Expand All @@ -227,7 +266,8 @@ public function scenariosProvider(): \Generator
'expectedOutput' => '',
'expectedFiles' => [
'.editorconfig' => 'TPL:editorconfig.dist',
'.php-cs-fixer.php' => 'TPL:{$typePrefix}_php-cs-fixer.dist.php',
'.php-cs-fixer.dist.php' => 'TPL:{$typePrefix}_php-cs-fixer.dist.php',
'.php-cs-fixer.php' => false,
'.php_cs' => false,
],
];
Expand Down

0 comments on commit 2ae91cd

Please sign in to comment.