-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateCommand.php
More file actions
80 lines (64 loc) · 2.26 KB
/
GenerateCommand.php
File metadata and controls
80 lines (64 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/*
* SPDX-License-Identifier: ISC
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
*/
declare(strict_types=1);
namespace Respect\FluentAnalysis\Commands;
use Respect\FluentAnalysis\BuilderClassScanner;
use Respect\FluentAnalysis\ConfigGenerator;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use function assert;
use function file_get_contents;
use function file_put_contents;
use function getcwd;
use function is_file;
use function is_string;
use function sprintf;
#[AsCommand(
name: 'generate',
description: 'Generate fluent.neon config for PHPStan',
)]
final class GenerateCommand extends Command
{
public function __construct(
private readonly BuilderClassScanner $scanner = new BuilderClassScanner(),
private readonly ConfigGenerator $generator = new ConfigGenerator(),
) {
parent::__construct();
}
protected function configure(): void
{
$this->addOption(
'output',
'o',
InputOption::VALUE_REQUIRED,
'Output file path',
'fluent.neon',
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$outputFile = $input->getOption('output');
assert(is_string($outputFile));
$builderClasses = $this->scanner->scan(getcwd() . '/composer.json');
if ($builderClasses === []) {
$output->writeln('<comment>No classes with #[FluentNamespace] attribute found.</comment>');
return Command::SUCCESS;
}
$content = $this->generator->generate($builderClasses);
$existing = is_file($outputFile) ? (file_get_contents($outputFile) ?: '') : '';
if ($content === $existing) {
$output->writeln(sprintf('<info>No changes needed.</info>'));
return Command::SUCCESS;
}
file_put_contents($outputFile, $content);
$output->writeln(sprintf('<info>Generated %s</info>', $outputFile));
return Command::SUCCESS;
}
}