Skip to content

Commit be16d93

Browse files
committed
use default php-cs-config file and binary if it exists
1 parent b6aa002 commit be16d93

File tree

3 files changed

+81
-44
lines changed

3 files changed

+81
-44
lines changed

src/Util/TemplateLinter.php

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,11 @@
2222
final class TemplateLinter
2323
{
2424
public function __construct(
25-
private ?string $binaryPath = null,
26-
private ?string $configPath = null,
25+
private ?string $phpCsFixerBinaryPath = null,
26+
private ?string $phpCsFixerConfigPath = null,
2727
) {
28-
if (null === $this->binaryPath) {
29-
$this->binaryPath = \dirname(__DIR__).'/Bin/php-cs-fixer-v3.13.0.phar';
30-
}
31-
32-
if (null === $this->configPath) {
33-
$this->configPath = \dirname(__DIR__).'/Resources/config/php-cs-fixer.config.php';
34-
}
35-
36-
if (!file_exists($this->binaryPath) || !file_exists($this->configPath)) {
37-
throw new RuntimeCommandException('Either the config or binary for PHP_CS_FIXER does not exist.');
38-
}
28+
$this->setConfig();
29+
$this->setBinary();
3930
}
4031

4132
public function lintPhpTemplate(string|array $templateFilePath): void
@@ -45,8 +36,55 @@ public function lintPhpTemplate(string|array $templateFilePath): void
4536
}
4637

4738
foreach ($templateFilePath as $filePath) {
48-
$process = Process::fromShellCommandline(sprintf('php %s --config=%s --using-cache=no fix %s', $this->binaryPath, $this->configPath, $filePath));
39+
$process = Process::fromShellCommandline(sprintf('php %s --config=%s --using-cache=no fix %s', $this->phpCsFixerBinaryPath, $this->phpCsFixerConfigPath, $filePath));
4940
$process->run();
5041
}
5142
}
43+
44+
private function setBinary(): void
45+
{
46+
if (null !== $this->phpCsFixerBinaryPath) {
47+
$this->checkPathExists($this->phpCsFixerBinaryPath, false);
48+
49+
return;
50+
}
51+
52+
$defaultBinaryPath = 'bin/php-cs-fixer';
53+
54+
if (file_exists($defaultBinaryPath)) {
55+
$this->phpCsFixerBinaryPath = $defaultBinaryPath;
56+
57+
return;
58+
}
59+
60+
$this->phpCsFixerBinaryPath = \dirname(__DIR__).'/Bin/php-cs-fixer-v3.13.0.phar';
61+
}
62+
63+
private function setConfig(): void
64+
{
65+
if (null !== $this->phpCsFixerConfigPath) {
66+
$this->checkPathExists($this->phpCsFixerConfigPath, true);
67+
68+
return;
69+
}
70+
71+
$defaultConfigPath = '.php-cs-fixer.dist.php';
72+
73+
if (file_exists($defaultConfigPath)) {
74+
$this->phpCsFixerConfigPath = $defaultConfigPath;
75+
76+
return;
77+
}
78+
79+
$this->phpCsFixerConfigPath = \dirname(__DIR__).'/Resources/config/php-cs-fixer.config.php';
80+
}
81+
82+
private function checkPathExists(string $path, bool $isConfigPath): void
83+
{
84+
if (file_exists($path)) {
85+
return;
86+
}
87+
88+
throw new RuntimeCommandException(sprintf('The %s provided: %s does not exist.', $isConfigPath ? 'MAKER_PHP_CS_FIXER_CONFIG_PATH' : 'MAKER_PHP_CS_FIXER_BINARY_PATH', $path));
89+
}
5290
}

tests/Maker/TemplateLinterTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,28 @@ public function getTestDetails(): \Generator
5252
self::assertStringContainsString('Linted by custom php-cs-config', $generatedTemplate);
5353
}),
5454
];
55+
56+
yield 'lints_templates_with_flex_generated_config_file' => [$this->createMakerTest()
57+
->addExtraDependencies('friendsofphp/php-cs-fixer')
58+
->run(function (MakerTestRunner $runner) {
59+
$runner->replaceInFile(
60+
'.php-cs-fixer.dist.php',
61+
'\'@Symfony\' => true,',
62+
<<< 'EOT'
63+
'@Symfony' => true,
64+
'header_comment' => [
65+
'header' => 'Linted with stock php-cs-config',
66+
],
67+
EOT
68+
);
69+
70+
// Voter class name
71+
$runner->runMaker(['FooBar']);
72+
73+
$generatedTemplate = file_get_contents($runner->getPath('src/Security/Voter/FooBarVoter.php'));
74+
75+
self::assertStringContainsString('Linted with stock php-cs-config', $generatedTemplate);
76+
}),
77+
];
5578
}
5679
}

tests/Util/TemplateLinterTest.php

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,23 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Bundle\MakerBundle\Util\TemplateLinter;
16-
use Symfony\Component\Filesystem\Filesystem;
1716

1817
/**
1918
* @author Jesse Rushlow <[email protected]>
2019
*/
2120
class TemplateLinterTest extends TestCase
2221
{
23-
public function testLinterFixesPhpFile(): void
22+
public function testExceptionBinaryPathDoesntExist(): void
2423
{
25-
$linter = new TemplateLinter();
24+
$this->expectExceptionMessage('The MAKER_PHP_CS_FIXER_BINARY_PATH provided: /some/bad/path does not exist.');
2625

27-
$fixture = $this->copySourceToTemp('TemplateLinterController.php');
28-
29-
$linter->lintPhpTemplate($fixture);
30-
31-
self::assertFileEquals(__DIR__.'/fixtures/template_linter/ExpectedTemplateLinterController.php', $fixture);
32-
}
33-
34-
/** @dataProvider pathProvider */
35-
public function testLinterThrowsExceptionIfBinaryOrArgumentDoNotExist(string $binaryPath, string $configPath): void
36-
{
37-
$this->expectExceptionMessage('Either the config or binary for PHP_CS_FIXER does not exist.');
38-
39-
new TemplateLinter($binaryPath, $configPath);
26+
new TemplateLinter(phpCsFixerBinaryPath: '/some/bad/path');
4027
}
4128

42-
public function pathProvider(): \Generator
29+
public function testExceptionThrownIfConfigPathDoesntExist(): void
4330
{
44-
yield 'Binary Path Wrong' => ['/bad/path', \dirname(__DIR__, 2).'/src/Resources/config/php-cs-fixer.config.php'];
45-
yield 'Config Path Wrong' => [\dirname(__DIR__, 2).'/src/Bin/php-cs-fixer-v3.13.0.phar', '/bad/path'];
46-
yield 'Both Paths Wrong' => ['/bad/path', '/bad/path'];
47-
}
48-
49-
private function copySourceToTemp(string $sourceFileName): string
50-
{
51-
$file = new Filesystem();
52-
$sourcePath = __DIR__.'/fixtures/source/';
53-
$tmpLocation = \dirname(__DIR__).'/tmp/cache/linter-test/';
54-
55-
$file->copy($sourcePath.$sourceFileName, $tmpLocation.$sourceFileName);
31+
$this->expectExceptionMessage('The MAKER_PHP_CS_FIXER_CONFIG_PATH provided: /bad/config/path does not exist.');
5632

57-
return $tmpLocation.$sourceFileName;
33+
new TemplateLinter(phpCsFixerConfigPath: '/bad/config/path');
5834
}
5935
}

0 commit comments

Comments
 (0)