Completed
Push — generator ( de0b8c...b918fc )
by Sullivan
06:11 queued 04:07
created

GenerateCommand::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 52
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 9.4929
c 0
b 0
f 0
cc 3
eloc 31
nc 3
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SLLH\IsoCodesValidator\Dev\Console\Command;
4
5
use Gnugat\NomoSpaco\File\FileRepository;
6
use Gnugat\NomoSpaco\FqcnRepository;
7
use Gnugat\NomoSpaco\Token\ParserFactory;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
/**
13
 * @author Sullivan Senechal <[email protected]>
14
 */
15
final class GenerateCommand extends Command
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    protected function configure()
21
    {
22
        $this
23
            ->setName('generate')
24
        ;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected function execute(InputInterface $input, OutputInterface $output)
31
    {
32
        $isoCodePath = __DIR__.'/../../../vendor/ronanguilloux/isocodes';
33
        if (!file_exists($isoCodePath.'/.git')) {
34
            $output->writeln(
35
                '<error>No .git directory found on ronanguilloux/isocodes package. Please run rm -r vendor/ronanguilloux/isocodes && composer update ronanguilloux/isocodes --prefer-source command.</error>'
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 205 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
36
            );
37
38
            return 1;
39
        }
40
41
        $fqcnRepository = new FqcnRepository(new FileRepository(), new ParserFactory());
42
43
        $isoCodesClasses = array_map(function ($fqcn) {
44
            return str_replace('IsoCodes\\', '', $fqcn);
45
        }, $fqcnRepository->findIn(__DIR__.'/../../../vendor/ronanguilloux/isocodes/src/IsoCodes'));
46
47
        $constraintClasses = array_filter(array_map(function ($fqcn) {
48
            return str_replace('SLLH\\IsoCodesValidator\\Constraints\\', '', $fqcn);
49
        }, $fqcnRepository->findIn(__DIR__.'/../../../src/Constraints')), function ($className) {
50
            return !empty(trim($className));
51
        });
52
53
        $twig = new \Twig_Environment(new \Twig_Loader_Filesystem(__DIR__.'/../..'));
54
55
        foreach (array_udiff($isoCodesClasses, $constraintClasses, 'strcasecmp') as $className) {
56
            $classVersion = str_replace(
57
                'v',
58
                '',
59
                exec("git -C ${isoCodePath} tag --list --contains $(git -C ${isoCodePath} log --pretty=format:\"%h\" --diff-filter=A src/IsoCodes/${className}.php) | head -n 1")
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 177 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
60
            );
61
62
            $output->writeln("Generate <comment>${className}</comment> constraint (<info>v${classVersion}</info>).");
63
64
            file_put_contents(
65
                __DIR__."/../../../src/Constraints/${className}.php",
66
                $twig->render('templates/Constraint.php.twig', [
67
                    'class_name' => $className,
68
                    'class_version' => $classVersion,
69
                ])
70
            );
71
72
            file_put_contents(
73
                __DIR__."/../../../tests/Constraints/${className}ValidatorTest.php",
74
                $twig->render('templates/ConstraintValidatorTest.php.twig', [
75
                    'class_name' => $className,
76
                ])
77
            );
78
        }
79
80
        return 0;
81
    }
82
}
83