1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Symplify |
5
|
|
|
* Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz). |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Symplify\MultiCodingStandard\Command; |
9
|
|
|
|
10
|
|
|
use Exception; |
11
|
|
|
use Symfony\Component\Console\Command\Command; |
12
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
use Symfony\Component\Console\Style\StyleInterface; |
16
|
|
|
use Symfony\Component\Finder\Finder; |
17
|
|
|
use Symfony\CS\Fixer; |
18
|
|
|
use Symplify\MultiCodingStandard\CodeSniffer\CodeBeautifier; |
19
|
|
|
use Symplify\MultiCodingStandard\CodeSniffer\CodeBeautifierFactory; |
20
|
|
|
use Symplify\MultiCodingStandard\Console\ExitCode; |
21
|
|
|
use Symplify\MultiCodingStandard\Contract\CodeSniffer\CodeBeautifierFactoryInterface; |
22
|
|
|
|
23
|
|
|
final class FixCommand extends Command |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var CodeBeautifierFactoryInterface |
27
|
|
|
*/ |
28
|
|
|
private $codeBeautifierFactory; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var StyleInterface |
32
|
|
|
*/ |
33
|
|
|
private $style; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Fixer |
37
|
|
|
*/ |
38
|
|
|
private $fixer; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var CodeBeautifier |
42
|
|
|
*/ |
43
|
|
|
private $codeBeautifier; |
44
|
|
|
|
45
|
|
|
public function __construct( |
46
|
|
|
CodeBeautifierFactoryInterface $codeBeautifierFactory, |
47
|
|
|
Fixer $fixer, |
48
|
|
|
StyleInterface $style |
49
|
|
|
) { |
50
|
|
|
parent::__construct(); |
51
|
|
|
|
52
|
|
|
$this->codeBeautifierFactory = $codeBeautifierFactory; |
53
|
|
|
$this->fixer = $fixer; |
54
|
|
|
$this->style = $style; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
protected function configure() |
61
|
|
|
{ |
62
|
|
|
$this->setName('fix'); |
63
|
|
|
$this->addArgument('path', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'The path(s)', null); |
64
|
|
|
$this->setDescription('Fix coding standard in one or more directories.'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
71
|
|
|
{ |
72
|
|
|
// note: needs to be lazy created, due to constant-options in PHP_CodeSniffer |
73
|
|
|
$this->codeBeautifier = $this->codeBeautifierFactory->create(); |
74
|
|
|
|
75
|
|
|
try { |
76
|
|
|
foreach ($input->getArgument('path') as $path) { |
77
|
|
|
$this->fixDirectory($path); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return ExitCode::SUCCESS; |
81
|
|
|
} catch (Exception $exception) { |
82
|
|
|
$this->style->error($exception->getMessage()); |
83
|
|
|
|
84
|
|
|
return ExitCode::ERROR; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $path |
90
|
|
|
*/ |
91
|
|
View Code Duplication |
private function fixDirectory($path) |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
// code sniffer |
94
|
|
|
foreach (Finder::create()->in($path)->files() as $filePath => $fileInfo) { |
95
|
|
|
$file = $this->codeBeautifier->processFile($filePath); |
96
|
|
|
var_dump($file->getErrorCount()); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// php-cs-fixer |
100
|
|
|
|
101
|
|
|
$this->style->success( |
102
|
|
|
sprintf('Directory "%s" was checked!', $path) |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.