Completed
Push — master ( 15ce78...c333c0 )
by Freek
02:35
created

ConvertCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 22
rs 9.2
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
3
namespace Spatie\Php7to5\Console;
4
5
use Spatie\Php7to5\DirectoryConverter;
6
use Spatie\Php7to5\Exceptions\InvalidArgument;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class ConvertCommand extends Command
14
{
15
    protected function configure()
16
    {
17
        $this->setName('convert')
18
            ->setDescription('Convert PHP 7 code to PHP 5 code')
19
            ->addArgument(
20
                'source',
21
                InputArgument::REQUIRED,
22
                'A PHP 7 file or a directory containing PHP 7 files'
23
            )
24
            ->addArgument(
25
                'destination',
26
                InputArgument::REQUIRED,
27
                'The file or path where the PHP 5 code should be saved'
28
            )
29
            ->addOption(
30
                'alsoCopyNonPhpFiles',
31
                'nonPhp',
32
                InputOption::VALUE_REQUIRED,
33
                'Should non php files be copied over as well',
34
                true
35
            );
36
    }
37
38
    /**
39
     * @param \Symfony\Component\Console\Input\InputInterface   $input
40
     * @param \Symfony\Component\Console\Output\OutputInterface $output
41
     *
42
     * @return int
43
     */
44
    protected function execute(InputInterface $input, OutputInterface $output)
45
    {
46
        $this->guardAgainstInvalidArguments($input);
47
48
        $source = $input->getArgument('source');
49
        $destination = $input->getArgument('destination');
50
51
        $output->writeln("Converting {$source} to {$destination}...");
52
        $output->writeln('');
53
54
        $converter = new DirectoryConverter($source);
55
56
        if (!$input->getOption('alsoCopyNonPhpFiles')) {
57
            $converter->doNotCopyNonPhpFiles();
58
        }
59
60
        $converter->savePhp5FilesTo($destination);
61
62
        $output->writeln('All Done!');
63
        $output->writeln('');
64
65
        return 0;
66
    }
67
68
    protected function guardAgainstInvalidArguments(InputInterface $input)
69
    {
70
        if (!is_dir($input->getArgument('source'))) {
71
            throw InvalidArgument::directoryDoesNotExist($input->getArgument('source'));
72
        }
73
    }
74
}
75