Completed
Push — master ( 3442f8...f4ff57 )
by Vladimir
02:32
created

Folder   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 136
Duplicated Lines 16.91 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 60.98%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 23
loc 136
ccs 25
cts 41
cp 0.6098
rs 10
c 1
b 0
f 0
wmc 13
lcom 1
cbo 3

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 4 1
A buildPath() 0 6 1
A getCwd() 0 6 1
B __construct() 0 28 4
A setTargetDirectory() 0 11 3
A copyFile() 0 10 1
A writeFile() 23 23 2

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
/**
4
 * @copyright 2017 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\System;
9
10
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
11
use Symfony\Component\Finder\SplFileInfo;
12
13
/**
14
 * All folder paths stored inside of this class will **not** have the ending DIRECTORY_SEPARATOR.
15
 *
16
 * @internal
17
 */
18
class Folder
19
{
20
    protected $fs;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $fs. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
21
    protected $absolutePath;
22
    protected $targetDirectories;
23
24 13
    public function __construct($folderPath)
25
    {
26 13
        $this->fs = new Filesystem();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 16 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
27 13
        $this->targetDirectories = array();
28
29
        // Setup the absolute path to the directory
30 13
        if (substr($folderPath, 0, 1) === DIRECTORY_SEPARATOR)
31
        {
32
            $this->absolutePath = $folderPath;
33
        }
34
        else
35
        {
36 13
            $this->absolutePath = $this->fs->absolutePath($folderPath);
37
        }
38
39
        // Ensure our directory paths will don't end with a '/'
40 13
        $this->absolutePath = rtrim($this->absolutePath, DIRECTORY_SEPARATOR);
41
42 13
        if (!$this->fs->isDir($this->absolutePath))
43
        {
44
            throw new \InvalidArgumentException();
45
        }
46
47 13
        if (!$this->fs->exists($this->absolutePath))
48
        {
49
            throw new FileNotFoundException();
50
        }
51 13
    }
52
53
    public function __toString()
54
    {
55
        return rtrim($this->absolutePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
56
    }
57
58
    /**
59
     * Set a base folder that will be prefixed before all file writes and copies.
60
     *
61
     * @param string $folderName
62
     *
63
     * @since 0.1.0
64
     */
65
    public function setTargetDirectory($folderName)
66
    {
67
        if (is_null($folderName) || empty($folderName))
68
        {
69
            $this->targetDirectories = array();
70
        }
71
        else
72
        {
73
            $this->targetDirectories[] = trim($folderName, DIRECTORY_SEPARATOR);
74
        }
75
    }
76
77
    /**
78
     * Copy a file from an absolute file to a path relative to the Folder's location.
79
     *
80
     * @param string $absolutePath The absolute path of the file
81
     * @param string $targetPath   The relative file path to the Folder's location
82
     *
83
     * @since 0.1.0
84
     */
85
    public function copyFile($absolutePath, $targetPath)
86
    {
87
        $targetPath = ltrim($targetPath, DIRECTORY_SEPARATOR);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $targetPath. This often makes code more readable.
Loading history...
88
89
        $this->fs->copy(
90
            $absolutePath,
91
            $this->buildPath($this->getCwd(), $targetPath),
92
            true
93
        );
94
    }
95
96
    /**
97
     * Write a file with the specified content.
98
     *
99
     * @param string $relativePath The file path relative to this Folder's location
100
     * @param string $content      The content that will be written to the file
101
     *
102
     * @since 0.1.0
103
     *
104
     * @return SplFileInfo
105
     */
106 13 View Code Duplication
    public function writeFile($relativePath, $content)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
107
    {
108 13
        $outputFolder = $this->fs->getFolderPath($relativePath);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
109 13
        $targetFileName = $this->fs->getFileName($outputFolder);
110
111 13
        $absoluteFolderPath = $this->buildPath($this->getCwd(), $outputFolder);
112
113 13
        if (!file_exists($absoluteFolderPath))
114
        {
115 10
            mkdir($absoluteFolderPath, 0755, true);
116
        }
117
118 13
        file_put_contents(
119 13
            $this->buildPath($this->getCwd(), $relativePath),
120
            $content
121
        );
122
123 13
        return (new SplFileInfo(
124
            $targetFileName,
125
            $absoluteFolderPath,
126 13
            $this->buildPath($absoluteFolderPath, $targetFileName)
127
        ));
128
    }
129
130
    /**
131
     * @param string $pathFragments
132
     *
133
     * @return string
134
     */
135 13
    private function buildPath($pathFragments)
0 ignored issues
show
Unused Code introduced by
The parameter $pathFragments is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
136
    {
137 13
        $paths = func_get_args();
138
139 13
        return implode(DIRECTORY_SEPARATOR, $paths);
140
    }
141
142
    /**
143
     * Returns the absolute path of where files will be placed.
144
     *
145
     * @return string
146
     */
147 13
    private function getCwd()
148
    {
149 13
        $location = array_merge(array($this->absolutePath), $this->targetDirectories);
150
151 13
        return implode(DIRECTORY_SEPARATOR, $location);
152
    }
153
}
154