Copy   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 112
Duplicated Lines 16.96 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 94.34%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 1
dl 19
loc 112
ccs 50
cts 53
cp 0.9434
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D createDelegate() 19 101 20

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Composer Magento Installer
4
 */
5
6
namespace MagentoHackathon\Composer\Magento\Deploystrategy;
7
8
/**
9
 * Symlink deploy strategy
10
 */
11
class Copy extends DeploystrategyAbstract
12
{
13
    /**
14
     * copy files
15
     *
16
     * @param string $source
17
     * @param string $dest
18
     * @return bool
19
     * @throws \ErrorException
20
     */
21 16
    public function createDelegate($source, $dest)
22
    {
23 16
        list($mapSource, $mapDest) = $this->getCurrentMapping();
24 16
        $mapSource = $this->removeTrailingSlash($mapSource);
25 16
        $mapDest = $this->removeTrailingSlash($mapDest);
26 16
        $cleanDest = $this->removeTrailingSlash($dest);
27
28 16
        $sourcePath = $this->getSourceDir() . '/' . $this->removeTrailingSlash($source);
29 16
        $destPath = $this->getDestDir() . '/' . $this->removeTrailingSlash($dest);
30
31
32
        // Create all directories up to one below the target if they don't exist
33 16
        $destDir = dirname($destPath);
34 16
        if (!file_exists($destDir)) {
35 2
            mkdir($destDir, 0777, true);
36
        }
37
38
        // Handle source to dir copy,
39
        // e.g. Namespace_Module.csv => app/locale/de_DE/
40
        // Namespace/ModuleDir => Namespace/
41
        // Namespace/ModuleDir => Namespace/, but Namespace/ModuleDir may exist
42
        // Namespace/ModuleDir => Namespace/ModuleDir, but ModuleDir may exist
43
44
        // first iteration through, we need to update the mappings to correctly handle mismatch globs
45 16
        if ($mapSource == $this->removeTrailingSlash($source) && $mapDest == $this->removeTrailingSlash($dest)) {
46 13
            if (basename($sourcePath) !== basename($destPath)) {
47 10
                $this->setCurrentMapping(array($mapSource, $mapDest . '/' . basename($source)));
48 10
                $cleanDest = $cleanDest . '/' . basename($source);
49
            }
50
        }
51
52 16
        if (file_exists($destPath) && is_dir($destPath)) {
53 9
            $mapSource = rtrim($mapSource, '*');
54 9
            if (strcmp(substr($cleanDest, strlen($mapDest)+1), substr($source, strlen($mapSource)+1)) === 0) {
55
                // copy each child of $sourcePath into $destPath
56 4 View Code Duplication
                foreach (new \DirectoryIterator($sourcePath) as $item) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57 4
                    $item = (string) $item;
58 4
                    if (!strcmp($item, '.') || !strcmp($item, '..')) {
59 4
                        continue;
60
                    }
61 4
                    $childSource = $this->removeTrailingSlash($source) . '/' . $item;
62 4
                    $this->create($childSource, substr($destPath, strlen($this->getDestDir())+1));
63
                }
64 4
                return true;
65 View Code Duplication
            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66 9
                $destPath = $this->removeTrailingSlash($destPath) . '/' . basename($source);
67 9
                return $this->create($source, substr($destPath, strlen($this->getDestDir())+1));
68
            }
69
        }
70
71
        // From now on $destPath can't be a directory, that case is already handled
72
73
        // If file exists and force is not specified, throw exception unless FORCE is set
74 16 View Code Duplication
        if (file_exists($destPath)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75 2
            if ($this->isForced()) {
76 2
                unlink($destPath);
77
            } else {
78
                throw new \ErrorException("Target $dest already exists (set extra.magento-force to override)");
79
            }
80
        }
81
82
        // File to file
83 16
        if (!is_dir($sourcePath)) {
84 11
            if (is_dir($destPath)) {
85
                $destPath .= '/' . basename($sourcePath);
86
            }
87 11
            $destPath = str_replace('\\', '/', $destPath);
88 11
            $this->addDeployedFile($destPath);
89 11
            return copy($sourcePath, $destPath);
90
        }
91
92
        // Copy dir to dir
93
        // First create destination folder if it doesn't exist
94 6
        if (file_exists($destPath)) {
95
            $destPath .= '/' . basename($sourcePath);
96
        }
97 6
        mkdir($destPath, 0777, true);
98
99 6
        $iterator = new \RecursiveIteratorIterator(
100 6
            new \RecursiveDirectoryIterator($sourcePath),
101 6
            \RecursiveIteratorIterator::SELF_FIRST
102
        );
103
104 6
        foreach ($iterator as $item) {
105 6
            $subDestPath = $destPath . '/' . $iterator->getSubPathName();
106 6
            if ($item->isDir()) {
107 6
                if (! file_exists($subDestPath)) {
108 6
                    mkdir($subDestPath, 0777, true);
109
                }
110
            } else {
111 6
                $subDestPath = str_replace('\\', '/', $subDestPath);
112 6
                copy($item, $subDestPath);
113 6
                $this->addDeployedFile($subDestPath);
114
            }
115 6
            if (!is_readable($subDestPath)) {
116 6
                throw new \ErrorException("Could not create $subDestPath");
117
            }
118
        }
119
120 6
        return true;
121
    }
122
}
123