ConvertCommand::convertPHPFilesInDirectory()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.2088
c 0
b 0
f 0
cc 5
nc 8
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
                'extension',
32
                'e',
33
                InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
34
                'PHP extensions',
35
                ['php']
36
            )
37
            ->addOption(
38
                'exclude',
39
                null,
40
                InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
41
                'Exclude path'
42
            )
43
            ->addOption(
44
                'copy-all',
45
                null,
46
                InputOption::VALUE_NONE,
47
                'If set, will copy all files in a directory, not only php'
48
            )
49
            ->addOption(
50
                'overwrite',
51
                null,
52
                InputOption::VALUE_NONE,
53
                'If set, will overwrite existing destination file or directory'
54
            );
55
    }
56
57
    /**
58
     * @param \Symfony\Component\Console\Input\InputInterface   $input
59
     * @param \Symfony\Component\Console\Output\OutputInterface $output
60
     *
61
     * @return int
62
     *
63
     * @throws \Spatie\Php7to5\Exceptions\InvalidParameter
64
     */
65
    protected function execute(InputInterface $input, OutputInterface $output)
66
    {
67
        $output->writeln("<info>Start converting {$input->getArgument('source')}</info>");
68
69
        $source = $input->getArgument('source');
70
71
        if (!file_exists($source)) {
72
            throw InvalidParameter::sourceDoesNotExist($source);
73
        }
74
75
        if (is_file($source)) {
76
            $this->convertFile($input);
77
        }
78
        if (is_dir($source)) {
79
            $this->convertPHPFilesInDirectory($input, $output);
80
        }
81
        $output->writeln('<info>All done!</info>');
82
83
        return 0;
84
    }
85
86
    protected function convertFile(InputInterface $input)
87
    {
88
        $converter = new Converter($input->getArgument('source'));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('source') targeting Symfony\Component\Consol...nterface::getArgument() can also be of type array<integer,string> or null; however, Spatie\Php7to5\Converter::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
89
        $destination = $input->getArgument('destination');
90
91
        if (file_exists($destination) && !$input->getOption('overwrite')) {
92
            throw InvalidParameter::destinationExist();
93
        }
94
        $converter->saveAsPhp5($destination);
0 ignored issues
show
Bug introduced by
It seems like $destination defined by $input->getArgument('destination') on line 89 can also be of type array<integer,string> or null; however, Spatie\Php7to5\Converter::saveAsPhp5() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
95
    }
96
97
    protected function convertPHPFilesInDirectory(InputInterface $input, OutputInterface $output)
98
    {
99
        $source = $input->getArgument('source');
100
        $destination = $input->getArgument('destination');
101
        $extensions = $input->getOption('extension');
102
        $excludes = $input->getOption('exclude');
103
        $converter = new DirectoryConverter($source, $extensions, $excludes);
0 ignored issues
show
Bug introduced by
It seems like $source defined by $input->getArgument('source') on line 99 can also be of type array<integer,string> or null; however, Spatie\Php7to5\DirectoryConverter::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
104
105
        if (!$input->getOption('overwrite')) {
106
          $this->isDestinationASourceDirectory($source, $destination);
0 ignored issues
show
Bug introduced by
It seems like $source defined by $input->getArgument('source') on line 99 can also be of type array<integer,string> or null; however, Spatie\Php7to5\Console\C...ationASourceDirectory() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $destination defined by $input->getArgument('destination') on line 100 can also be of type array<integer,string> or null; however, Spatie\Php7to5\Console\C...ationASourceDirectory() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
107
        }
108
109
        $this->isDestinationDifferentThanSource($source, $destination);
0 ignored issues
show
Bug introduced by
It seems like $source defined by $input->getArgument('source') on line 99 can also be of type array<integer,string> or null; however, Spatie\Php7to5\Console\C...onDifferentThanSource() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $destination defined by $input->getArgument('destination') on line 100 can also be of type array<integer,string> or null; however, Spatie\Php7to5\Console\C...onDifferentThanSource() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
110
111
        if (!$input->getOption('copy-all')) {
112
            $converter->doNotCopyNonPhpFiles();
113
        }
114
115
        if (file_exists($destination) && !$input->getOption('overwrite')) {
116
            throw InvalidParameter::destinationExist();
117
        }
118
119
        $converter->setLogger($output);
120
        $converter->savePhp5FilesTo($destination);
0 ignored issues
show
Bug introduced by
It seems like $destination defined by $input->getArgument('destination') on line 100 can also be of type array<integer,string> or null; however, Spatie\Php7to5\Directory...rter::savePhp5FilesTo() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
121
    }
122
123
    /**
124
     * @param string $source
125
     * @param string $destination
126
     *
127
     * @throws \Spatie\Php7to5\Exceptions\InvalidParameter
128
     */
129
    protected function isDestinationASourceDirectory($source, $destination)
130
    {
131
        $this->isEqual($source, $destination);
132
    }
133
134
    /**
135
     * @param string $source
136
     * @param string $destination
137
     *
138
     * @throws \Spatie\Php7to5\Exceptions\InvalidParameter
139
     */
140
    protected function isDestinationDifferentThanSource($source, $destination)
141
    {
142
        $path_parts = pathinfo($destination);
143
        $this->isEqual($source, $path_parts['dirname']);
144
    }
145
146
    /**
147
     * @param string $source
148
     * @param string $destination
149
     *
150
     * @throws \Spatie\Php7to5\Exceptions\InvalidParameter
151
     */
152
    protected function isEqual($source, $destination)
153
    {
154
        if (!ends_with($destination, DIRECTORY_SEPARATOR)) {
155
            $destination = $destination.DIRECTORY_SEPARATOR;
156
        }
157
        if (!ends_with($source, DIRECTORY_SEPARATOR)) {
158
            $source = $source.DIRECTORY_SEPARATOR;
159
        }
160
161
        if ($destination === $source) {
162
            throw InvalidParameter::destinationDirectoryIsSource();
163
        }
164
    }
165
}
166