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
|
|
|
declare(strict_types=1); |
20
|
|
|
|
21
|
|
|
namespace DocHeader\Command; |
22
|
|
|
|
23
|
|
|
use DocHeader\Filter\FilterAggregator; |
24
|
|
|
use DocHeader\Helper\DocheaderFileResolution; |
25
|
|
|
use DocHeader\Helper\IOResourcePathResolution; |
26
|
|
|
use DocHeader\Validator\RegExp; |
27
|
|
|
use Symfony\Component\Console\Command\Command; |
28
|
|
|
use Symfony\Component\Console\Exception\InvalidArgumentException; |
29
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
30
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
31
|
|
|
use Symfony\Component\Console\Input\InputOption; |
32
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
33
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
34
|
|
|
use function assert; |
35
|
|
|
use function file_get_contents; |
36
|
3 |
|
use function is_array; |
37
|
|
|
use function is_string; |
38
|
3 |
|
use function strpos; |
39
|
3 |
|
|
40
|
3 |
|
final class Checker extends Command |
41
|
3 |
|
{ |
42
|
3 |
|
/** |
43
|
3 |
|
* @throws InvalidArgumentException |
44
|
|
|
*/ |
45
|
3 |
|
protected function configure() : void |
46
|
3 |
|
{ |
47
|
3 |
|
$this |
48
|
3 |
|
->setName('check') |
49
|
3 |
|
->setDescription('Check for docComment') |
50
|
|
|
->addArgument( |
51
|
1 |
|
'directory', |
52
|
3 |
|
InputArgument::IS_ARRAY | InputArgument::REQUIRED, |
53
|
3 |
|
'Directory to scan *.php files' |
54
|
3 |
|
) |
55
|
3 |
|
->addOption( |
56
|
3 |
|
'exclude-dir', |
57
|
|
|
null, |
58
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
59
|
3 |
|
'Exclude the specified directory from being scanned; declare multiple directories ' |
60
|
3 |
|
. 'with multiple invocations of this option.' |
61
|
3 |
|
) |
62
|
3 |
|
->addOption( |
63
|
3 |
|
'exclude', |
64
|
3 |
|
null, |
65
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
66
|
3 |
|
'Exclude the specified file from being scanned; declare multiple files with multiple ' |
67
|
3 |
|
. 'invocations of this option.' |
68
|
|
|
) |
69
|
3 |
|
->addOption( |
70
|
|
|
'docheader', |
71
|
3 |
|
null, |
72
|
3 |
|
InputOption::VALUE_REQUIRED, |
73
|
3 |
|
'Specify a docheader template file', |
74
|
3 |
|
'.docheader' |
75
|
3 |
|
); |
76
|
3 |
|
} |
77
|
3 |
|
|
78
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) : int |
79
|
3 |
|
{ |
80
|
|
|
$docheaderFile = $this->getDocheaderFileContent($input); |
81
|
3 |
|
$directory = (array) $input->getArgument('directory'); |
82
|
3 |
|
$excludedDirectories = $input->getOption('exclude-dir') ?: []; |
83
|
2 |
|
$excludedFiles = $input->getOption('exclude') ?: []; |
84
|
1 |
|
|
85
|
1 |
|
assert(is_array($excludedDirectories)); |
86
|
1 |
|
assert(is_array($excludedFiles)); |
87
|
3 |
|
|
88
|
3 |
|
$finder = (new IOResourcePathResolution($directory, $excludedDirectories, $excludedFiles)) |
89
|
|
|
->__invoke(); |
90
|
3 |
|
$validator = new RegExp($docheaderFile); |
91
|
1 |
|
|
92
|
1 |
|
$success = true; |
93
|
|
|
/** @var SplFileInfo[][] $finder */ |
94
|
1 |
|
foreach ($finder as $dir) { |
95
|
|
|
foreach ($dir as $file) { |
96
|
|
|
if ($this->docIsCompatible($validator, $file->getContents(), $docheaderFile)) { |
97
|
2 |
|
continue; |
98
|
2 |
|
} |
99
|
|
|
|
100
|
2 |
|
$success = false; |
101
|
|
|
$output->writeln('-> ' . $file->getRelativePathname()); |
102
|
2 |
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
3 |
|
if (! $success) { |
106
|
|
|
$output->writeln(''); |
107
|
3 |
|
$output->writeln('<bg=red;fg=white> Something goes wrong! </>'); |
108
|
3 |
|
|
109
|
3 |
|
return 1; |
110
|
|
|
} |
111
|
3 |
|
|
112
|
|
|
$output->writeln('<bg=green;fg=white> Everything is OK! </>'); |
113
|
|
|
|
114
|
|
|
return 0; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function docIsCompatible(RegExp $headerValidator, string $fileContent, string $docheaderFile) : bool |
118
|
|
|
{ |
119
|
|
|
return $headerValidator->__invoke($fileContent) || strpos($fileContent, $docheaderFile) !== false; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @throws Exception\DocHeaderFileConfiguration |
124
|
|
|
*/ |
125
|
|
|
private function getDocheaderFileContent(InputInterface $input) : string |
126
|
|
|
{ |
127
|
|
|
$docheaderFile = $input->getOption('docheader'); |
128
|
|
|
|
129
|
|
|
assert(is_string($docheaderFile)); |
130
|
|
|
|
131
|
|
|
$docheader = (new DocheaderFileResolution())->resolve($docheaderFile); |
132
|
|
|
$filter = new FilterAggregator(file_get_contents($docheader)); |
133
|
|
|
|
134
|
|
|
return $filter->apply(); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|