PHP7CCAnalyseCommand::configure()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 20
nc 1
nop 0
dl 0
loc 25
ccs 0
cts 21
cp 0
crap 2
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
4
namespace cwreden\php7ccAnalyser\Console;
5
6
7
use cwreden\php7ccAnalyser\Analyser;
8
use cwreden\php7ccAnalyser\FilePersistenceAdapter;
9
use cwreden\php7ccAnalyser\ScanResultFile;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
class PHP7CCAnalyseCommand extends Command
17
{
18
    const COMMAND_NAME = 'php7ccAnalyse';
19
    const PATH_ARGUMENT_NAME = 'path';
20
    const LIST_OPTION_NAME = 'list';
21
    const PREVIEW_OPTION_NAME = 'preview';
22
    const IGNORE_FIRST_RESULT_OPTION_NAME = 'ignore-first';
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    protected function configure()
28
    {
29
        $this->setName(static::COMMAND_NAME)
30
            ->addArgument(
31
                static::PATH_ARGUMENT_NAME,
32
                InputArgument::REQUIRED,
33
                'The json file from php7cc scan.'
34
            )
35
            ->addOption(
36
                static::LIST_OPTION_NAME,
37
                'l',
38
                InputOption::VALUE_NONE,
39
                'Show all matched compatibility issues.'
40
            )
41
            ->addOption(
42
                static::PREVIEW_OPTION_NAME,
43
                '',
44
                InputOption::VALUE_NONE,
45
                'Preview mode does not save the scan for upcoming analyses'
46
            )
47
            ->addOption(
48
                static::IGNORE_FIRST_RESULT_OPTION_NAME,
49
                '',
50
                InputOption::VALUE_NONE,
51
                'The analyse will not fail without a previous scan.'
52
            );
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    protected function execute(InputInterface $input, OutputInterface $output)
59
    {
60
        $path = $input->getArgument(static::PATH_ARGUMENT_NAME);
61
        $showList = $input->getOption(static::LIST_OPTION_NAME);
62
        $preview = $input->getOption(static::PREVIEW_OPTION_NAME);
63
        $ignoreFirstResult = $input->getOption(static::IGNORE_FIRST_RESULT_OPTION_NAME);
64
65
        $cachePath = '.' . DIRECTORY_SEPARATOR . 'lastScan';
66
        $analyser = new Analyser(
67
            $output,
68
            new FilePersistenceAdapter($cachePath)
69
        );
70
        $analyseResult = $analyser->analyse(new ScanResultFile($path), !$preview, $showList);
71
72
        $output->writeln(PHP_EOL);
73
        if ($analyseResult === Analyser::RESULT_STATUS_FAILURES) {
74
            $output->writeln('There are new php 7 incompatible statements!');
75
        } elseif ($analyseResult === Analyser::RESULT_STATUS_RISKY) {
76
            $output->writeln('There are new php 7 risky statements!');
77
        } else {
78
            $output->writeln('There are no new php 7 compatibility errors or warnings.');
79
        }
80
81
        if (!$ignoreFirstResult && $analyseResult === Analyser::RESULT_STATUS_FAILURES) {
82
            return 2;
83
        } elseif (!$ignoreFirstResult && $analyseResult === Analyser::RESULT_STATUS_RISKY) {
84
            return 1;
85
        }
86
        return 0;
87
    }
88
89
}