Completed
Pull Request — master (#11)
by Jefersson
01:42
created

Checker::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
crap 1
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 3
    public function __construct($name, $header)
35
    {
36 3
        parent::__construct(null);
37
38 3
        $this->header = $header;
39 3
    }
40
41 3
    protected function configure()
42
    {
43 3
        $this
44 3
            ->setName('check')
45 3
            ->setDescription('Check for docComment')
46 3
            ->addArgument(
47 3
                'directory',
48 3
                InputArgument::IS_ARRAY | InputArgument::REQUIRED,
49
                'Directory to scan *.php files'
50 3
            );
51 3
    }
52
53 3
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55 3
        $directory = $input->getArgument('directory');
56 3
        $finder    = (new IOResourcePathResolution($directory))->__invoke();
57 3
        $validator = new RegExp($this->header);
58
59
        /* @var $file \Symfony\Component\Finder\SplFileInfo */
60 3
        foreach ($finder as $directory) {
61 3
            foreach ($directory as $file) {
62 2
                if (! $this->docIsCompatible($validator, $file->getContents())) {
63 1
                    defined('FAILED') ?: define('FAILED', 1);
64 1
                    $output->writeln('-> ' . $file->getRelativePathname());
65 1
                }
66 3
            }
67 3
        }
68
69 3
        if (defined('FAILED')) {
70 1
            $output->writeln('');
71 1
            $output->writeln('<bg=red;fg=white>    Something goes wrong!     </>');
72
73 1
            return 1;
74
        }
75
76 2
        $output->writeln('<bg=green;fg=white>    Everything is OK!     </>');
77 2
    }
78
79 2
    private function docIsCompatible($headerValidator, $fileContent)
80
    {
81 2
        return $headerValidator->__invoke($fileContent) || false !== strpos($fileContent, $this->header);
82
    }
83
}
84