Test Failed
Push — master ( 3fe081...08cc3f )
by Kacper
03:11
created

RegenerateCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 72
rs 10
wmc 10
lcom 1
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 14 1
B execute() 0 28 4
B regenerate() 0 16 5
1
<?php
2
3
4
namespace Kadet\Highlighter\bin\Commands\Test;
5
6
7
use Kadet\Highlighter\KeyLighter;
8
use Kadet\Highlighter\Language\Language;
9
use Kadet\Highlighter\Tests\Helpers\TestFormatter;
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 RegenerateCommand extends Command
17
{
18
    /**
19
     * @var KeyLighter
20
     */
21
    private $_keylighter;
22
    private $_formatter;
23
24
    private $_input;
25
    private $_output;
26
27
    protected function configure()
28
    {
29
        $this->setName('test:regenerate')
30
            ->addArgument('files', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'tests to regenerate', ['*'])
31
            ->addOption('new', null, InputOption::VALUE_NONE, 'generate only new files')
32
            ->setDescription('Regenerates test files')
33
        ;
34
35
        $this->_keylighter = KeyLighter::get();
36
        $this->_formatter  = new TestFormatter();
37
38
        $this->_input  = realpath(__DIR__.'/../../../Tests/Samples');
39
        $this->_output = realpath(__DIR__.'/../../../Tests/Expected/Test');
40
    }
41
42
    protected function execute(InputInterface $input, OutputInterface $output)
43
    {
44
        $iterator = new \RecursiveIteratorIterator(
45
            new \RecursiveDirectoryIterator(
46
                $this->_input,
47
                \RecursiveDirectoryIterator::SKIP_DOTS | \RecursiveDirectoryIterator::UNIX_PATHS
48
            ), \RecursiveIteratorIterator::LEAVES_ONLY
49
        );
50
51
        /** @var \SplFileInfo $file */
52
        foreach ($iterator as $file) {
53
            $pathname = substr($file->getPathname(), strlen($this->_input) + 1);
54
            if(!$this->regenerate($input, $pathname)) {
55
                continue;
56
            }
57
58
            $output->writeln("Generating $pathname...", OutputInterface::VERBOSITY_QUIET);
59
60
            $language = Language::byFilename($pathname);
61
            $result = $this->_keylighter->highlight(file_get_contents($file->getPathname()), $language, $this->_formatter);
62
63
            if(!file_exists($this->_output.'/'.dirname($pathname))) {
64
                mkdir($this->_output.'/'.dirname($pathname), true);
65
            }
66
67
            file_put_contents("{$this->_output}/$pathname.tkn", $result);
68
        }
69
    }
70
71
    private function regenerate(InputInterface $input, $filename) {
72
        $filename = str_replace(DIRECTORY_SEPARATOR, '/', $filename);
73
        $patterns = $input->getArgument('files');
74
75
        foreach($patterns as $pattern) {
76
            if($input->getOption('new') && file_exists("{$this->_output}/$filename.tkn")) {
77
                continue;
78
            }
79
80
            if(fnmatch($pattern, $filename)) {
81
                return true;
82
            }
83
        }
84
85
        return false;
86
    }
87
}