Completed
Push — master ( 9f2dac...cab9cb )
by Arnaud
07:03 queued 01:55
created

CopyFilter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 61
ccs 0
cts 34
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
D run() 0 33 9
A checkRequirements() 0 3 1
A getSupportedExtensions() 0 6 1
1
<?php
2
3
namespace JK\Sam\Filter\Copy;
4
5
use Exception;
6
use JK\Sam\Filter\Filter;
7
use SplFileInfo;
8
use Symfony\Component\Filesystem\Filesystem;
9
10
class CopyFilter extends Filter
11
{
12
    /**
13
     * @param SplFileInfo[] $sources
14
     * @param SplFileInfo[] $destinations
15
     * @return SplFileInfo[]
16
     * @throws Exception
17
     */
18
    public function run(array $sources, array $destinations)
19
    {
20
        $fileSystem = new Filesystem();
21
22
        // must have the same sources and destinations number
23
        if (count($sources) !== count($destinations) && count($destinations) !== 1) {
24
            throw new Exception('Sources and destinations count mismatch');
25
        }
26
27
        if (count($destinations) === 1 && !is_dir($destinations[0]) && count($sources) > 1) {
28
            throw new Exception('If only one destination is set for multiple source, it should be a directory');
29
        }
30
        $copiedFiles = [];
31
32
        // copy generated files to its destination
33
        foreach ($sources as $index => $source) {
34
35
            if (array_key_exists($index, $destinations)) {
36
                $destination = $destinations[$index];
37
            } else {
38
                $destination = $destinations[0];
39
            }
40
41
            if (is_dir($destination)) {
42
                $destination = $destination.'/'.$source->getFilename();
43
            }
44
            $this->addNotification('copying "'.$source.'" to "'.$destination.'"');
45
            $fileSystem->copy($source->getRealPath(), $destination);
0 ignored issues
show
Bug introduced by
It seems like $destination can also be of type object<SplFileInfo>; however, Symfony\Component\Filesystem\Filesystem::copy() 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...
46
            $copiedFiles[] = $destination;
47
        }
48
49
        return $copiedFiles;
50
    }
51
52
    /**
53
     * 
54
     */
55
    public function checkRequirements()
56
    {
57
    }
58
59
    /**
60
     * Return the file extensions supported by this filter.
61
     *
62
     * @return string[]
63
     */
64
    public function getSupportedExtensions()
65
    {
66
        return [
67
            '*'
68
        ];
69
    }
70
}
71