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
|
|
|
namespace DocHeader\Command; |
19
|
|
|
|
20
|
|
|
use DocHeader\Helper\IOResourcePathResolution; |
21
|
|
|
use DocHeader\Validator\RegExp; |
22
|
|
|
use Symfony\Component\Console\Command\Command; |
23
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
24
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
25
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
26
|
|
|
|
27
|
|
|
final class Checker extends Command |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $header; |
33
|
|
|
|
34
|
|
|
public function __construct($name, $header) |
35
|
|
|
{ |
36
|
|
|
parent::__construct(null); |
37
|
|
|
|
38
|
|
|
$this->header = $header; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function configure() |
42
|
|
|
{ |
43
|
|
|
$this |
44
|
|
|
->setName('check') |
45
|
|
|
->setDescription('Check for docComment') |
46
|
|
|
->addArgument( |
47
|
|
|
'directory', |
48
|
|
|
InputArgument::IS_ARRAY | InputArgument::REQUIRED, |
49
|
|
|
'Directory to scan *.php files' |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
54
|
|
|
{ |
55
|
|
|
$directory = $input->getArgument('directory'); |
56
|
|
|
$finder = (new IOResourcePathResolution($directory))->__invoke(); |
57
|
|
|
$validator = new RegExp($this->header); |
58
|
|
|
|
59
|
|
|
/* @var $file \Symfony\Component\Finder\SplFileInfo */ |
60
|
|
|
foreach ($finder as $directory) { |
61
|
|
|
foreach ($directory as $file) { |
62
|
|
|
if (! $this->docIsCompatible($validator, $file->getContents())) { |
63
|
|
|
defined('FAILED') ?: define('FAILED', 1); |
64
|
|
|
$output->writeln('-> ' . $file->getRelativePathname()); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (defined('FAILED')) { |
70
|
|
|
$output->writeln(''); |
71
|
|
|
$output->writeln('<bg=red;fg=white> Something goes wrong! </>'); |
72
|
|
|
|
73
|
|
|
return 1; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$output->writeln('<bg=green;fg=white> Everything is OK! </>'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function docIsCompatible($headerValidator, $fileContent) |
80
|
|
|
{ |
81
|
|
|
return $headerValidator->__invoke($fileContent) || false !== strpos($fileContent, $this->header); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|