Completed
Push — dot-language-processor ( 82cdb7...13d982 )
by Luis
14:10
created

GenerateStatisticsCommand::savingResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHP version 7.1
4
 *
5
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
6
 */
7
namespace PhUml\Console\Commands;
8
9
use PhUml\Actions\GenerateStatistics;
10
use PhUml\Parser\CodeFinder;
11
use PhUml\Parser\TokenParser;
12
use PhUml\Processors\StatisticsProcessor;
13
use RuntimeException;
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
class GenerateStatisticsCommand extends GeneratorCommand
20
{
21
    /**
22
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
23
     */
24
    protected function configure()
25
    {
26
        $this
27
            ->setName('phuml:statistics')
28
            ->setDescription('Generate statistics about the code of a given directory')
29
            ->setHelp(<<<HELP
30
Example:
31
    php bin/phuml phuml:statistics -r  ./src statistics.txt
32
33
    This example will scan the `./src` directory recursively for php files.
34
    It will generate the statistics and save them to the file `statistics.txt`.
35
HELP
36
            )
37
            ->addOption(
38
                'recursive',
39
                'r',
40
                InputOption::VALUE_NONE,
41
                'Look for classes in the given directory recursively'
42
            )
43
            ->addArgument(
44
                'directory',
45
                InputArgument::REQUIRED,
46
                'The directory to be scanned to generate the statistics'
47
            )
48
            ->addArgument(
49
                'output',
50
                InputArgument::REQUIRED,
51
                'The file name for your statistics file'
52
            )
53
        ;
54
    }
55
56
    /**
57
     * @throws \LogicException
58
     * @throws \RuntimeException
59
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
60
     */
61
    protected function execute(InputInterface $input, OutputInterface $output)
62
    {
63
        $directory = $input->getArgument('directory');
64
        $statisticsFile = $input->getArgument('output');
65
        $recursive = (bool)$input->getOption('recursive');
66
67
        if (!is_dir($directory)) {
68
            throw new RuntimeException("'$directory' is not a valid directory");
69
        }
70
71
        $action = new GenerateStatistics(new TokenParser(), new StatisticsProcessor());
72
        $action->attach($this->display);
73
74
        $finder = new CodeFinder();
75
        $finder->addDirectory($directory, $recursive);
76
77
        $output->writeln('[|] Running... (This may take some time)');
78
79
        $action->generate($finder, $statisticsFile);
80
81
        return 0;
82
    }
83
}
84