1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace DocHeader\Command; |
20
|
|
|
|
21
|
|
|
use DocHeader\Filter\Filter; |
22
|
|
|
use DocHeader\Helper\DocheaderFileResolution; |
23
|
|
|
use DocHeader\Helper\IOResourcePathResolution; |
24
|
|
|
use DocHeader\Validator\RegExp; |
25
|
|
|
use Symfony\Component\Console\Command\Command; |
26
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
27
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
28
|
|
|
use Symfony\Component\Console\Input\InputOption; |
29
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @author Jefersson Nathan <[email protected]> |
33
|
|
|
*/ |
34
|
|
|
final class Checker extends Command |
35
|
|
|
{ |
36
|
3 |
|
protected function configure() |
37
|
|
|
{ |
38
|
3 |
|
$this |
39
|
3 |
|
->setName('check') |
40
|
3 |
|
->setDescription('Check for docComment') |
41
|
3 |
|
->addArgument( |
42
|
3 |
|
'directory', |
43
|
3 |
|
InputArgument::IS_ARRAY | InputArgument::REQUIRED, |
44
|
|
|
'Directory to scan *.php files' |
45
|
3 |
|
) |
46
|
3 |
|
->addOption( |
47
|
3 |
|
'exclude-dir', |
48
|
3 |
|
null, |
49
|
3 |
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
50
|
|
|
'Exclude the specified directory from being scanned; declare multiple directories ' |
51
|
1 |
|
. 'with multiple invocations of this option.' |
52
|
3 |
|
) |
53
|
3 |
|
->addOption( |
54
|
3 |
|
'exclude', |
55
|
3 |
|
null, |
56
|
3 |
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
57
|
|
|
'Exclude the specified file from being scanned; declare multiple files with multiple ' |
58
|
|
|
. 'invocations of this option.' |
59
|
3 |
|
) |
60
|
3 |
|
->addOption( |
61
|
3 |
|
'docheader', |
62
|
3 |
|
null, |
63
|
3 |
|
InputOption::VALUE_REQUIRED, |
64
|
3 |
|
'Specify a docheader template file', |
65
|
|
|
'.docheader' |
66
|
3 |
|
); |
67
|
3 |
|
} |
68
|
|
|
|
69
|
3 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
|
|
70
|
|
|
{ |
71
|
3 |
|
$docheaderFile = $this->getDocheaderFileContent($input); |
72
|
3 |
|
$directory = $input->getArgument('directory'); |
73
|
3 |
|
$excludedDirectories = $input->getOption('exclude-dir') ?: []; |
74
|
3 |
|
$excludedFiles = $input->getOption('exclude') ?: []; |
75
|
3 |
|
$finder = (new IOResourcePathResolution($directory, $excludedDirectories, $excludedFiles))->__invoke(); |
76
|
3 |
|
$validator = new RegExp($docheaderFile); |
77
|
|
|
|
78
|
3 |
|
$success = true; |
79
|
|
|
/* @var $file \Symfony\Component\Finder\SplFileInfo */ |
80
|
3 |
|
foreach ($finder as $dir) { |
81
|
3 |
|
foreach ($dir as $file) { |
82
|
2 |
|
if (! $this->docIsCompatible($validator, $file->getContents(), $docheaderFile)) { |
83
|
1 |
|
$success = false; |
84
|
1 |
|
$output->writeln('-> ' . $file->getRelativePathname()); |
85
|
1 |
|
} |
86
|
3 |
|
} |
87
|
3 |
|
} |
88
|
|
|
|
89
|
3 |
|
if (! $success) { |
90
|
1 |
|
$output->writeln(''); |
91
|
1 |
|
$output->writeln('<bg=red;fg=white> Something goes wrong! </>'); |
92
|
|
|
|
93
|
1 |
|
return 1; |
94
|
|
|
} |
95
|
|
|
|
96
|
2 |
|
$output->writeln('<bg=green;fg=white> Everything is OK! </>'); |
97
|
2 |
|
} |
98
|
|
|
|
99
|
2 |
|
private function docIsCompatible($headerValidator, $fileContent, $docheaderFile) |
100
|
|
|
{ |
101
|
2 |
|
return $headerValidator->__invoke($fileContent) || false !== strpos($fileContent, $docheaderFile); |
102
|
|
|
} |
103
|
|
|
|
104
|
3 |
|
private function getDocheaderFileContent(InputInterface $input) |
105
|
|
|
{ |
106
|
3 |
|
$docheaderFile = $input->getOption('docheader'); |
107
|
3 |
|
$docheader = (new DocheaderFileResolution())->resolve($docheaderFile); |
108
|
3 |
|
$filter = new Filter(file_get_contents($docheader)); |
109
|
|
|
|
110
|
3 |
|
return $filter->apply(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.
You can also find more information in the “Code” section of your repository.