MainCommand::findConfigFile()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
/*
3
 * This file is part of project-quality-inspector.
4
 *
5
 * (c) Alexandre GESLIN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace ProjectQualityInspector\Command;
12
13
use ProjectQualityInspector\Application\Output\UIHelper;
14
use ProjectQualityInspector\Application\Output\JunitHelper;
15
use ProjectQualityInspector\Application\Output\HtmlReportHelper;
16
use ProjectQualityInspector\Exception\RuleViolationException;
17
use ProjectQualityInspector\Loader\RulesLoader;
18
use Symfony\Component\Console\Command\Command;
19
use Symfony\Component\Console\Input\InputArgument;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Input\InputOption;
22
use Symfony\Component\Console\Output\OutputInterface;
23
24
/**
25
 * Main command
26
 */
27
class MainCommand extends Command
28
{
29
    const SUCCESS_EXIT   = 0;
30
    const FAILURE_EXIT   = 1;
31
32
    protected function configure()
33
    {
34
        $this->setName('run')
35
            ->setDescription('The Project quality tool')
36
            ->addArgument('applicationType', InputArgument::REQUIRED, 'The application type (symfony or angularjs, etc...)')
37
            ->addOption('rules', '-r', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'select rules to load')
38
            ->addOption('baseDir', '-b', InputOption::VALUE_REQUIRED, 'Change the base directory of application')
39
            ->addOption('configFile', '-c', InputOption::VALUE_REQUIRED, 'Change the base directory of application')
40
            ->addOption('junitFile', '-j', InputOption::VALUE_REQUIRED, 'Generate a JUnit file')
41
            ->addOption('htmlFile', '-f', InputOption::VALUE_REQUIRED, 'Generate a HTML file');
42
    }
43
44
    /**
45
     * @param InputInterface $input
46
     * @param OutputInterface $output
47
     * @return int
48
     */
49
    protected function execute(InputInterface $input, OutputInterface $output)
50
    {
51
        $exitCode = $this::SUCCESS_EXIT;
52
53
        $applicationType = $input->getArgument('applicationType');
54
        $baseDir = $this->resolveBaseDirOption($input);
55
56
        //TODO: check if absolute baseDir
57
        $configFile = $input->getOption('configFile') ? getcwd() . '/' . $input->getOption('configFile') : $this->findConfigFile();
58
        //TODO: check if absolute baseDir
59
        $junitFile = $input->getOption('junitFile') ? getcwd() . '/' . $input->getOption('junitFile') : null;
60
        $htmlFile = $input->getOption('htmlFile') ? getcwd() . '/' . $input->getOption('htmlFile') : null;
61
62
        $rulesLoader = new RulesLoader();
63
64
        try {
65
            $rules = $rulesLoader->load($configFile, $applicationType, $baseDir, $input->getOption('rules'));
66
        } catch (\InvalidArgumentException $e) {
67
            UIHelper::displayException($e, $output);
68
69
            return $this::FAILURE_EXIT;
70
        }
71
72
        UIHelper::displayStartingBlock($output, $configFile, $this->getApplication()->getVersion());
73
74
        foreach ($rules as $rule) {
75
            try {
76
                $rule->evaluate();
77
                UIHelper::displayRuleSuccess($rule, $output);
78
            } catch (RuleViolationException $e) {
79
                UIHelper::displayRuleViolation($e, $output);
80
            } catch (\InvalidArgumentException $e) {
81
                UIHelper::displayException($e, $output);
82
            }
83
        }
84
85
        if ($junitFile) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $junitFile of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
86
            JunitHelper::generateJunitFile($rules, $junitFile);
87
        }
88
89
        if ($htmlFile) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $htmlFile of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
90
            HtmlReportHelper::generateHtmlFile($rules, $htmlFile);
91
        }
92
93
        return $exitCode;
94
    }
95
96
    /**
97
     * @return string
98
     */
99
    protected function findConfigFile()
100
    {
101
        $configsToSearch = [
102
            getcwd() . '/pqi.yml',
103
            __DIR__ . '/../../pqi.yml'
104
        ];
105
106
        return (file_exists($configsToSearch[0])) ? $configsToSearch[0] : $configsToSearch[1];
107
    }
108
109
    /**
110
     * @param InputInterface $input
111
     * @return string
112
     */
113
    protected function resolveBaseDirOption(InputInterface $input)
114
    { 
115
        //TODO: check if absolute baseDir
116
        $baseDir = $input->getOption('baseDir') ? getcwd() . DIRECTORY_SEPARATOR . $input->getOption('baseDir') : getcwd();
117
118
        return $baseDir;
119
    }
120
}