8000 feature #1575 [make:*] Use a PHP-CS-Fixer shim rather than an externa… · symfony/maker-bundle@4735ff1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4735ff1

Browse files
theofidryjrushlow
andauthored
feature #1575 [make:*] Use a PHP-CS-Fixer shim rather than an external PHAR
* build: Use a PHP-CS-Fixer shim rather than an external PHAR * minor fixes * use sprintf * fix config file path * use file manager to get the root project path * php-cs-fixer expectations for makers w/ generated comparison * fix missing fileManager instance in test --------- Co-authored-by: Jesse Rushlow <jr@rushlow.dev>
1 parent 3cf70d8 commit 4735ff1

File tree

12 files changed

+31
-23
lines changed

12 files changed

+31
-23
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"php": ">=8.1",
1717
"doctrine/inflector": "^2.0",
1818
"nikic/php-parser": "^4.18|^5.0",
19+
"php-cs-fixer/shim": "^v3.64",
1920
"symfony/config": "^6.4|^7.0",
2021
"symfony/console": "^6.4|^7.0",
2122
"symfony/dependency-injection": "^6.4|^7.0",
-2.69 MB
Binary file not shown.

src/Resources/config/services.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
</service>
4242

4343
<service id="maker.template_linter" class="Symfony\Bundle\MakerBundle\Util\TemplateLinter">
44+
<argument type="service" id="maker.file_manager" />
4445
<argument>%env(default::string:MAKER_PHP_CS_FIXER_BINARY_PATH)%</argument>
4546
<argument>%env(default::string:MAKER_PHP_CS_FIXER_CONFIG_PATH)%</argument>
4647
</service>

src/Util/TemplateLinter.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\MakerBundle\Util;
1313

1414
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
15+
use Symfony\Bundle\MakerBundle\FileManager;
1516
use Symfony\Component\Console\Output\OutputInterface;
1617
use Symfony\Component\Process\ExecutableFinder;
1718
use Symfony\Component\Process\Process;
@@ -25,14 +26,12 @@
2526
*/
2627
final class TemplateLinter
2728
{
28-
// Version must match bundled version file name. e.g. php-cs-fixer-v3.49.9.phar
29-
public const BUNDLED_PHP_CS_FIXER_VERSION = '3.49.0';
30-
3129
private bool $usingBundledPhpCsFixer = true;
3230
private bool $usingBundledPhpCsFixerConfig = true;
3331
private bool $needsPhpCmdPrefix = true;
3432

3533
public function __construct(
34+
private FileManager $fileManager,
3635
private ?string $phpCsFixerBinaryPath = null,
3736
private ?string $phpCsFixerConfigPath = null,
3837
) {
@@ -98,9 +97,15 @@ public function writeLinterMessage(OutputInterface $output): void
9897

9998
private function setBinary(): void
10099
{
101-
// Use Bundled PHP-CS-Fixer
100+
// Use Bundled (shim) PHP-CS-Fixer
102101
if (null === $this->phpCsFixerBinaryPath) {
103-
$this->phpCsFixerBinaryPath = \sprintf('%s/Resources/bin/php-cs-fixer-v%s.phar', \dirname(__DIR__), self::BUNDLED_PHP_CS_FIXER_VERSION);
102+
$shimLocation = \sprintf('%s/vendor/bin/php-cs-fixer', \dirname(__DIR__, 2));
103+
104+
if (is_file($shimLocation)) {
105+
$this->phpCsFixerBinaryPath = $shimLocation;
106+
107+
return;
108+
}
104109

105110
return;
106111
}
@@ -129,7 +134,8 @@ private function setBinary(): void
129134
private function setConfig(): void
130135
{
131136
// No config provided, but there is a dist config file in the project dir
132-
if (null === $this->phpCsFixerConfigPath && file_exists($defaultConfigPath = '.php-cs-fixer.dist.php')) {
137+
$defaultConfigPath = \sprintf('%s/.php-cs-fixer.dist.php', $this->fileManager->getRootDirectory());
138+
if (null === $this->phpCsFixerConfigPath && file_exists($defaultConfigPath)) {
133139
$this->phpCsFixerConfigPath = $defaultConfigPath;
134140

135141
$this->usingBundledPhpCsFixerConfig = false;

tests/Command/MakerCommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testExceptionOnMissingDependencies(): void
4040

4141
$fileManager = $this->createMock(FileManager::class);
4242

43-
$command = new MakerCommand($maker, $fileManager, new Generator($fileManager, 'App'), new TemplateLinter());
43+
$command = new MakerCommand($maker, $fileManager, new Generator($fileManager, 'App'), new TemplateLinter($fileManager));
4444
// needed because it's normally set by the Application
4545
$command->setName('make:foo');
4646
$tester = new CommandTester($command);
@@ -53,7 +53,7 @@ public function testExceptionOnUnknownRootNamespace(): void
5353

5454
$fileManager = $this->createMock(FileManager::class);
5555

56-
$command = new MakerCommand($maker, $fileManager, new Generator($fileManager, 'Unknown'), new TemplateLinter());
56+
$command = new MakerCommand($maker, $fileManager, new Generator($fileManager, 'Unknown'), new TemplateLinter($fileManager));
5757
// needed because it's normally set by the Application
5858
$command->setName('make:foo');
5959
$tester = new CommandTester($command);

tests/Maker/TemplateLinterTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ protected function getMakerClass(): string
3333
public function getTestDetails(): \Generator
3434
{
3535
yield 'lints_templates_with_custom_php_cs_fixer_and_config' => [$this->createMakerTest()
36-
->addExtraDependencies('php-cs-fixer/shim')
3736
->run(function (MakerTestRunner $runner) {
3837
$runner->copy('template-linter/php-cs-fixer.test.php', 'php-cs-fixer.test.php');
3938

@@ -53,13 +52,12 @@ public function getTestDetails(): \Generator
5352

5453
self::assertStringContainsString('Linted by custom php-cs-config', $generatedTemplate);
5554

56-
$expectedOutput = 'System PHP-CS-Fixer (bin/php-cs-fixer) & System PHP-CS-Fixer Configuration (php-cs-fixer.test.php)';
55+
$expectedOutput = 'System PHP-CS-Fixer (bin/php-cs-fixer) & System PHP-CS-Fixer Configuration';
5756
self::assertStringContainsString($expectedOutput, $output);
5857
}),
5958
];
6059

6160
yield 'lints_templates_with_flex_generated_config_file' => [$this->createMakerTest()
62-
->addExtraDependencies('php-cs-fixer/shim')
6361
->run(function (MakerTestRunner $runner) {
6462
$runner->replaceInFile(
6563
'.php-cs-fixer.dist.php',
@@ -79,13 +77,14 @@ public function getTestDetails(): \Generator
7977

8078
self::assertStringContainsString('Linted with stock php-cs-config', $generatedTemplate);
8179

82-
$expectedOutput = 'Bundled PHP-CS-Fixer & System PHP-CS-Fixer Configuration (.php-cs-fixer.dist.php)';
80+
$expectedOutput = 'Bundled PHP-CS-Fixer & System PHP-CS-Fixer Configuration';
8381
self::assertStringContainsString($expectedOutput, $output);
8482
}),
8583
];
8684

8785
yield 'lints_templates_with_bundled_php_cs_fixer' => [$this->createMakerTest()
8886
->run(function (MakerTestRunner $runner) {
87+
$runner->deleteFile('.php-cs-fixer.dist.php');
8988
// Voter class name
9089
$output = $runner->runMaker(['FooBar']);
9190

tests/Util/TemplateLinterTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
namespace Symfony\Bundle\MakerBundle\Tests\Util;
1313

14+
use Composer\InstalledVersions;
1415
use PHPUnit\Framework\TestCase;
16+
use Symfony\Bundle\MakerBundle\FileManager;
1517
use Symfony\Bundle\MakerBundle\Util\TemplateLinter;
1618
use Symfony\Component\Process\Process;
1719

@@ -28,27 +30,30 @@ public function testExceptionBinaryPathDoesntExist(): void
2830
{
2931
$this->expectExceptionMessage('The MAKER_PHP_CS_FIXER_BINARY_PATH provided: /some/bad/path does not exist.');
3032

31-
new TemplateLinter(phpCsFixerBinaryPath: '/some/bad/path');
33+
new TemplateLinter($this->createMock(FileManager::class), phpCsFixerBinaryPath: '/some/bad/path');
3234
}
3335

3436
public function testExceptionThrownIfConfigPathDoesntExist(): void
3537
{
3638
$this->expectExceptionMessage('The MAKER_PHP_CS_FIXER_CONFIG_PATH provided: /bad/config/path does not exist.');
3739

38-
new TemplateLinter(phpCsFixerConfigPath: '/bad/config/path');
40+
new TemplateLinter($this->createMock(FileManager::class), phpCsFixerConfigPath: '/bad/config/path');
3941
}
4042

4143
public function testPhpCsFixerVersion(): void
4244
{
4345
$this->markTestSkippedOnWindows();
4446

45-
$fixerPath = \sprintf('%s/src/Resources/bin/php-cs-fixer-v%s.phar', \dirname(__DIR__, 2), TemplateLinter::BUNDLED_PHP_CS_FIXER_VERSION);
47+
$fixerPath = \sprintf('%s/vendor/php-cs-fixer/shim/php-cs-fixer', \dirname(__DIR__, 2));
48+
49+
// Get the installed version and remove the preceding "v"
50+
$expectedVersion = ltrim(InstalledVersions::getPrettyVersion('php-cs-fixer/shim'), 'v');
4651

4752
$process = Process::fromShellCommandline(\sprintf('%s -V', $fixerPath));
4853

4954
$process->run();
5055

51-
self::assertStringContainsString(TemplateLinter::BUNDLED_PHP_CS_FIXER_VERSION, $process->getOutput());
56+
self::assertStringContainsString($expectedVersion, $process->getOutput());
5257
}
5358

5459
private function markTestSkippedOnWindows(): void

tests/fixtures/make-serializer-normalizer/EntityFixtureNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class EntityFixtureNormalizer implements NormalizerInterface
1010
{
1111
public function __construct(
1212
#[Autowire(service: 'serializer.normalizer.object')]
13-
private NormalizerInterface $normalizer
13+
private NormalizerInterface $normalizer,
1414
) {
1515
}
1616

tests/fixtures/make-serializer-normalizer/FooBarNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class FooBarNormalizer implements NormalizerInterface
99
{
1010
public function __construct(
1111
#[Autowire(service: 'serializer.normalizer.object')]
12-
private NormalizerInterface $normalizer
12+
private NormalizerInterface $normalizer,
1313
) {
1414
}
1515

tests/fixtures/make-validator/expected/FooBar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ final class FooBar extends Constraint
1414
public function __construct(
1515
public string $mode = 'strict',
1616
?array $groups = null,
17-
mixed $payload = null
17+
mixed $payload = null,
1818
) {
1919
parent::__construct([], $groups, $payload);
2020
}

tests/fixtures/make-voter/expected/FooBarVoter.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ protected function supports(string $attribute, mixed $subject): bool
2222
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
2323
{
2424
$user = $token->getUser();
25-
2625
// if the user is anonymous, do not grant access
2726
if (!$user instanceof UserInterface) {
2827
return false;
@@ -34,7 +33,6 @@ protected function voteOnAttribute(string $attribute, mixed $subject, TokenInter
3433
// logic to determine if the user can EDIT
3534
// return true or false
3635
break;
37-
3836
case self::VIEW:
3937
// logic to determine if the user can VIEW
4038
// return true or false

tests/fixtures/make-voter/expected/not_final_FooBarVoter.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ protected function supports(string $attribute, mixed $subject): bool
2222
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
2323
{
2424
$user = $token->getUser();
25-
2625
// if the user is anonymous, do not grant access
2726
if (!$user instanceof UserInterface) {
2827
return false;
@@ -34,7 +33,6 @@ protected function voteOnAttribute(string $attribute, mixed $subject, TokenInter
3433
// logic to determine if the user can EDIT
3534
// return true or false
3635
break;
37-
3836
case self::VIEW:
3937
// logic to determine if the user can VIEW
4038
// return true or false

0 commit comments

Comments
 (0)
0