Passed
Push — master ( 7b0e14...a8dbbf )
by Fabien
01:54
created

AssessComplexityCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Churn\Command;
6
7
use Churn\Assessor\CyclomaticComplexityAssessor;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
/**
14
 * @internal
15
 */
16
class AssessComplexityCommand extends Command
17
{
18
    /**
19
     * @var CyclomaticComplexityAssessor
20
     */
21
    private $assessor;
22
23
    /**
24
     * Class constructor.
25
     *
26
     * @param CyclomaticComplexityAssessor $assessor The class calculating the complexity.
27
     */
28
    public function __construct(CyclomaticComplexityAssessor $assessor)
29
    {
30
        parent::__construct();
31
32
        $this->assessor = $assessor;
33
    }
34
35
    /**
36
     * Returns a new instance of the command.
37
     */
38
    public static function newInstance(): self
39
    {
40
        return new self(new CyclomaticComplexityAssessor());
41
    }
42
43
    /**
44
     * Configure the command
45
     */
46
    protected function configure(): void
47
    {
48
        $this->setName('assess-complexity')
49
            ->addArgument('file', InputArgument::REQUIRED, 'Path to file to analyze.')
50
            ->setDescription('Calculate the Cyclomatic Complexity');
51
    }
52
53
    /**
54
     * Execute the command
55
     *
56
     * @param InputInterface $input Input.
57
     * @param OutputInterface $output Output.
58
     */
59
    protected function execute(InputInterface $input, OutputInterface $output): int
60
    {
61
        $file = (string) $input->getArgument('file');
62
        $contents = \is_file($file)
63
            ? \file_get_contents($file)
64
            : false;
65
66
        if (false === $contents) {
67
            $output->writeln('0');
68
69
            return 0;
70
        }
71
72
        $output->writeln((string) $this->assessor->assess($contents));
73
74
        return 0;
75
    }
76
}
77