Completed
Pull Request — master (#19)
by Jefersson
02:28
created

Checker::getDocheaderFileContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 2
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
    public function __construct($name)
37
    {
38 3
        parent::__construct(null);
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
            ->addOption(
52 3
                'exclude-dir',
53 3
                null,
54 3
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
55
                'Exclude the specified directory from being scanned; declare multiple directories '
56
                . 'with multiple invocations of this option.'
57 3
            )
58 3
            ->addOption(
59 3
                'exclude',
60 3
                null,
61 3
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
62
                'Exclude the specified file from being scanned; declare multiple files with multiple '
63
                . 'invocations of this option.'
64 3
            )
65 3
            ->addOption(
66 3
                'docheader',
67 3
                null,
68 3
                InputOption::VALUE_REQUIRED,
69 3
                'Specify a docheader template file',
70
                '.docheader'
71 3
            );
72 3
    }
73
74 3
    protected function execute(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Complexity introduced by
This operation has 400 execution paths which exceeds the configured maximum of 200.

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.

Loading history...
75
    {
76 3
        $docheaderFile       = $this->getDocheaderFileContent($input);
77 3
        $directory           = $input->getArgument('directory');
78 3
        $excludedDirectories = $input->getOption('exclude-dir') ?: [];
79 3
        $excludedFiles       = $input->getOption('exclude') ?: [];
80 3
        $finder              = (new IOResourcePathResolution($directory, $excludedDirectories, $excludedFiles))->__invoke();
81 3
        $validator           = new RegExp($docheaderFile);
82
83
        /* @var $file \Symfony\Component\Finder\SplFileInfo */
84 3
        foreach ($finder as $dir) {
85 3
            foreach ($dir as $file) {
86 2
                if (! $this->docIsCompatible($validator, $file->getContents(), $docheaderFile)) {
87 1
                    defined('FAILED') ?: define('FAILED', 1);
88 1
                    $output->writeln('-> ' . $file->getRelativePathname());
89 1
                }
90 3
            }
91 3
        }
92
93 3
        if (defined('FAILED')) {
94 1
            $output->writeln('');
95 1
            $output->writeln('<bg=red;fg=white>    Something goes wrong!     </>');
96
97 1
            return 1;
98
        }
99
100 2
        $output->writeln('<bg=green;fg=white>    Everything is OK!     </>');
101 2
    }
102
103 2
    private function docIsCompatible($headerValidator, $fileContent, $docheaderFile)
104
    {
105 2
        return $headerValidator->__invoke($fileContent) || false !== strpos($fileContent, $docheaderFile);
106
    }
107
108 3
    private function getDocheaderFileContent(InputInterface $input)
109
    {
110 3
        $docheaderFile = $input->getOption('docheader');
111
112 3
        if ('.docheader' === $docheaderFile) {
113 3
            $docheaderFile = getcwd() . '/' . $docheaderFile;
114 3
        }
115
116 3
        $docheader = (new DocheaderFileResolution())->__invoke($docheaderFile);
117
118 3
        $filter = new Filter(file_get_contents($docheader));
119
120 3
        return $filter->apply();
121
    }
122
}
123