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 PHP_CodeSniffer; |
12
|
|
|
use Symfony\Component\Console\Command\Command; |
13
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
14
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
use Symfony\Component\Console\Style\StyleInterface; |
17
|
|
|
use Symfony\Component\Finder\Finder; |
18
|
|
|
use Symplify\MultiCodingStandard\Console\ExitCode; |
19
|
|
|
use Symplify\MultiCodingStandard\Contract\CodeSniffer\CodeSnifferFactoryInterface; |
20
|
|
|
|
21
|
|
|
final class CheckCommand extends Command |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var PHP_CodeSniffer |
25
|
|
|
*/ |
26
|
|
|
private $codeSniffer; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var StyleInterface |
30
|
|
|
*/ |
31
|
|
|
private $style; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var CodeSnifferFactoryInterface |
35
|
|
|
*/ |
36
|
|
|
private $codeSnifferFactory; |
37
|
|
|
|
38
|
|
|
public function __construct( |
39
|
|
|
CodeSnifferFactoryInterface $codeSnifferFactory, |
40
|
|
|
StyleInterface $style |
41
|
|
|
) { |
42
|
|
|
parent::__construct(); |
43
|
|
|
|
44
|
|
|
$this->codeSnifferFactory = $codeSnifferFactory; |
45
|
|
|
$this->style = $style; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
protected function configure() |
52
|
|
|
{ |
53
|
|
|
$this->setName('check'); |
54
|
|
|
$this->addArgument('path', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'The path(s)', null); |
55
|
|
|
$this->setDescription('Check coding standard in one or more directories.'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
62
|
|
|
{ |
63
|
|
|
try { |
64
|
|
|
foreach ($input->getArgument('path') as $path) { |
65
|
|
|
$this->checkDirectory($path); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return ExitCode::SUCCESS; |
69
|
|
|
} catch (Exception $exception) { |
70
|
|
|
$this->style->error($exception->getMessage()); |
71
|
|
|
|
72
|
|
|
return ExitCode::ERROR; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $path |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
private function checkDirectory($path) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
// code sniffer |
82
|
|
|
foreach ((new Finder())->in($path)->files() as $filePath => $fileInfo) { |
83
|
|
|
$file = $this->getCodeSniffer()->processFile($filePath); |
|
|
|
|
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// php-cs-fixer |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
$this->style->success( |
91
|
|
|
sprintf('Directory "%s" was checked!', $path) |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Lazy loaded due to duplicated constants in setup |
97
|
|
|
* in CodeSniffer for both Sniffer and Fixer. |
98
|
|
|
* |
99
|
|
|
* @return PHP_CodeSniffer |
100
|
|
|
*/ |
101
|
|
|
private function getCodeSniffer() |
102
|
|
|
{ |
103
|
|
|
if ($this->codeSniffer) { |
104
|
|
|
return $this->codeSniffer; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->codeSniffer = $this->codeSnifferFactory->create(); |
108
|
|
|
|
109
|
|
|
return $this->codeSniffer; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
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.