Completed
Push — master ( c333c0...93aec9 )
by Freek
04:01
created

ConvertCommand::isDestinationDifferentThanSource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Spatie\Php7to5\Console;
4
5
use Spatie\Php7to5\Converter;
6
use Spatie\Php7to5\DirectoryConverter;
7
use Spatie\Php7to5\Exceptions\InvalidParameter;
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
use Symfony\Component\Console\Input\InputOption;
13
14
class ConvertCommand extends Command
15
{
16
    protected function configure()
17
    {
18
        $this->setName('convert')
19
            ->setDescription('Convert PHP 7 code to PHP 5 code')
20
            ->addArgument(
21
                'source',
22
                InputArgument::REQUIRED,
23
                'A PHP 7 file or a directory containing PHP 7 files'
24
            )
25
            ->addArgument(
26
                'destination',
27
                InputArgument::REQUIRED,
28
                'The file or path where the PHP 5 code should be saved'
29
            )
30
            ->addOption(
31
                'copy-all',
32
                null,
33
                InputOption::VALUE_NONE,
34
                'If set, will copy all files in a directory, not only php'
35
            )
36
            ->addOption(
37
                'overwrite',
38
                null,
39
                InputOption::VALUE_NONE,
40
                'If set, will overwrite existing destination file or directory'
41
            );
42
    }
43
44
    /**
45
     * @param \Symfony\Component\Console\Input\InputInterface   $input
46
     * @param \Symfony\Component\Console\Output\OutputInterface $output
47
     *
48
     * @return int
49
     *
50
     * @throws \Spatie\Php7to5\Exceptions\InvalidParameter
51
     */
52
    protected function execute(InputInterface $input, OutputInterface $output)
53
    {
54
        $output->writeln("<info>Start converting {$input->getArgument('source')}</info>");
55
56
        $source = $input->getArgument('source');
57
58
        if (!file_exists($source)) {
59
            throw InvalidParameter::sourceDoesNotExist($source);
60
        }
61
62
        if (is_file($source)) {
63
            $this->convertFile($input);
64
        }
65
        if (is_dir($source)) {
66
            $this->convertPHPFilesInDirectory($input, $output);
67
        }
68
        $output->writeln('<info>All done!</info>');
69
70
        return 0;
71
    }
72
73
    protected function convertFile(InputInterface $input)
74
    {
75
        $converter = new Converter($input->getArgument('source'));
76
        $destination = $input->getArgument('destination');
77
78
        if (file_exists($destination) && !$input->getOption('overwrite')) {
79
            throw InvalidParameter::destinationExist();
80
        }
81
        $converter->saveAsPhp5($destination);
82
    }
83
84
    protected function convertPHPFilesInDirectory(InputInterface $input, OutputInterface $output)
85
    {
86
        $source = $input->getArgument('source');
87
        $destination = $input->getArgument('destination');
88
        $converter = new DirectoryConverter($source);
89
90
        $this->isDestinationASourceDirectory($source, $destination);
91
        $this->isDestinationDifferentThanSource($source, $destination);
92
93
        if (!$input->getOption('copy-all')) {
94
            $converter->doNotCopyNonPhpFiles();
95
        }
96
97
        if (file_exists($destination) && !$input->getOption('overwrite')) {
98
            throw InvalidParameter::destinationExist();
99
        }
100
101
        $converter->setLogger($output);
102
        $converter->savePhp5FilesTo($destination);
103
    }
104
105
    /**
106
     * @param string $source
107
     * @param string $destination
108
     *
109
     * @throws \Spatie\Php7to5\Exceptions\InvalidParameter
110
     */
111
    protected function isDestinationASourceDirectory($source, $destination)
112
    {
113
        $this->isEqual($source, $destination);
114
    }
115
116
    /**
117
     * @param string $source
118
     * @param string $destination
119
     *
120
     * @throws \Spatie\Php7to5\Exceptions\InvalidParameter
121
     */
122
    protected function isDestinationDifferentThanSource($source, $destination)
123
    {
124
        $path_parts = pathinfo($destination);
125
        $this->isEqual($source, $path_parts['dirname']);
126
    }
127
128
    /**
129
     * @param string $source
130
     * @param string $destination
131
     *
132
     * @throws \Spatie\Php7to5\Exceptions\InvalidParameter
133
     */
134
    protected function isEqual($source, $destination)
135
    {
136
        if (!ends_with($destination, DIRECTORY_SEPARATOR)) {
137
            $destination = $destination.DIRECTORY_SEPARATOR;
138
        }
139
        if (!ends_with($source, DIRECTORY_SEPARATOR)) {
140
            $source = $source.DIRECTORY_SEPARATOR;
141
        }
142
143
        if ($destination === $source) {
144
            throw InvalidParameter::destinationDirectoryIsSource();
145
        }
146
    }
147
}
148