Completed
Push — master ( b3b654...428c4b )
by Freek
01:49
created

DirectoryConverter::copyDirectory()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 26
rs 6.7272
cc 7
eloc 14
nc 10
nop 2
1
<?php
2
3
namespace Spatie\Php7to5;
4
5
use FilesystemIterator;
6
use Spatie\Php7to5\Exceptions\InvalidParameter;
7
8
class DirectoryConverter
9
{
10
    /** @var string */
11
    protected $copyNonPhpFiles = true;
12
13
    public function __construct(string $sourceDirectory)
14
    {
15
        if (!file_exists($sourceDirectory)) {
16
            throw InvalidParameter::directoryDoesNotExist($sourceDirectory);
17
        }
18
19
        $this->sourceDirectory = $sourceDirectory;
0 ignored issues
show
Bug introduced by
The property sourceDirectory does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
20
    }
21
22
    public function alsoCopyNonPhpFiles() : self
23
    {
24
        $this->copyNonPhpFiles = true;
0 ignored issues
show
Documentation Bug introduced by
The property $copyNonPhpFiles was declared of type string, but true is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
25
26
        return $this;
27
    }
28
29
    public function doNotCopyNonPhpFiles() : self
30
    {
31
        $this->copyNonPhpFiles = false;
0 ignored issues
show
Documentation Bug introduced by
The property $copyNonPhpFiles was declared of type string, but false is of type false. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
32
33
        return $this;
34
    }
35
36
    public function savePhp5FilesTo(string $destinationDirectory)
37
    {
38
        if ($destinationDirectory === '') {
39
            throw InvalidParameter::directoryIsRequired();
40
        }
41
42
        $this->copyDirectory($this->sourceDirectory, $destinationDirectory);
43
    }
44
45
    protected function copyDirectory(string $sourceDirectory, string $destinationDirectory)
46
    {
47
        if (!is_dir($destinationDirectory)) {
48
            mkdir($destinationDirectory);
49
        }
50
51
        $items = new FilesystemIterator($sourceDirectory, FilesystemIterator::SKIP_DOTS);
52
53
        foreach ($items as $item) {
54
            $target = $destinationDirectory.'/'.$item->getBasename();
55
56
            if ($item->isDir()) {
57
                $sourceDirectory = $item->getPathname();
58
59
                $this->copyDirectory($sourceDirectory, $target);
60
            } else {
61
                if ($this->isPhpFile($target) || $this->copyNonPhpFiles) {
62
                    copy($item->getPathname(), $target);
63
64
                    if (strtolower(pathinfo($target, PATHINFO_EXTENSION)) === 'php') {
65
                        $this->convertToPhp5($target);
66
                    }
67
                }
68
            }
69
        }
70
    }
71
72
    protected function convertToPhp5(string $filePath)
73
    {
74
        $converter = new Converter($filePath);
75
76
        $converter->saveAsPhp5($filePath);
77
    }
78
79
    protected function isPhpFile(string $filePath) : bool
80
    {
81
        return strtolower(pathinfo($filePath, PATHINFO_EXTENSION)) === 'php';
82
    }
83
}
84