Completed
Push — master ( 80d5dd...e32bbf )
by Vladimir
03:02
created

FilesystemPath::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace allejo\stakx\Filesystem;
4
5
use allejo\stakx\System\Filesystem;
6
7
/**
8
 * A cross-platform filesystem path wrapper.
9
 *
10
 * This class is a wrapper for handling file paths in stakx in a cross-platform way. Give it a Windows path and append
11
 * a Unix style path and it'll just work.
12
 */
13
final class FilesystemPath
14
{
15
    /** @var Filesystem */
16
    private $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...
17
    /** @var string */
18
    private $absolutePath;
19
    /** @var string */
20
    private $originalPath;
21
    /** @var bool */
22
    private $isWindows;
23
24
    /**
25
     * @param string $filePath
26
     * @param string $dirSep
27
     */
28 42
    public function __construct($filePath, $dirSep = DIRECTORY_SEPARATOR)
29
    {
30 42
        $this->fs = new Filesystem();
31 42
        $this->originalPath = $filePath;
32 42
        $this->isWindows = ($dirSep === '\\');
33
34 42
        if ($this->isWindows)
35 42
        {
36 2
            $filePath = $this->unixifyPath($filePath);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $filePath. This often makes code more readable.
Loading history...
37 2
        }
38
39 42
        $this->absolutePath = ($this->fs->isAbsolutePath($filePath)) ? $filePath : $this->fs->absolutePath($filePath);
40 42
    }
41
42 4
    public function __toString()
43
    {
44 4
        return $this->getAbsolutePath();
45
    }
46
47
    /**
48
     * Append a path to a directory path.
49
     *
50
     * @param string $append The path to append
51
     */
52 5
    public function appendToPath($append)
53
    {
54 5
        if ($this->isFile(false))
55 5
        {
56 1
            throw new \InvalidArgumentException("Appending to a file's path is not possible");
57
        }
58
59 4
        $this->absolutePath = $this->buildPath($this->absolutePath, $this->unixifyPath($append));
60 4
    }
61
62
    /**
63
     * Generate a path based off this file path.
64
     *
65
     * This method will not modify the existing file path of this instance, use FilesystemPath::appendToPath() for that.
66
     *
67
     * @param string $append
68
     *
69
     * @return string
70
     */
71 36
    public function generatePath($append)
72
    {
73 36
        return $this->buildPath($this->absolutePath, $this->unixifyPath($append));
74
    }
75
76
    /**
77
     * Get the absolute path of the file path.
78
     *
79
     * @return string
80
     */
81 4
    public function getAbsolutePath()
82
    {
83 4
        if ($this->isWindows)
84 4
        {
85 2
            return str_replace('/', '\\', $this->absolutePath);
86
        }
87
88 2
        return $this->absolutePath;
89
    }
90
91
    /**
92
     * Check whether the given path is a directory.
93
     *
94
     * @param bool $checkExistence When set to true, it will check the filesystem for the existence of the directory.
95
     *                             When set to false, this function will guess based on the path ending in a directory
96
     *                             separator.
97
     *
98
     * @return bool
99
     */
100 42
    public function isDir($checkExistence = true)
101
    {
102 42
        $absPath = $this->absolutePath;
103
104
        if ($checkExistence)
105 42
        {
106 36
            return file_exists($absPath) && is_dir($absPath);
107
        }
108
109 6
        return (substr($absPath, -1, 1) == '/');
110
    }
111
112
    /**
113
     * Check whether the given path is a file.
114
     *
115
     * @param bool $checkExistence When set to true, it will check the filesystem for the existence of the file. When
116
     *                             set to false, this function will guess based on the path ending in a directory
117
     *                             separator.
118
     *
119
     * @return bool
120
     */
121 5
    public function isFile($checkExistence = true)
122
    {
123 5
        $absPath = $this->absolutePath;
124
125
        if ($checkExistence)
126 5
        {
127
            return file_exists($absPath) && is_file($absPath);
128
        }
129
130 5
        return (!$this->isDir($checkExistence));
131
    }
132
133
    /**
134
     * Build a path from multiple strings.
135
     *
136
     * This function will _always_ use the '/' as the directory separator, because internal that's all stakx will use.
137
     * The FilesystemPath::getAbsolutePath() function will worry about Windows paths when necessary.
138
     *
139
     * @return string
140
     */
141 40 View Code Duplication
    private function buildPath()
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...
142
    {
143 40
        $paths = [];
144
145 40
        foreach (func_get_args() as $arg)
146
        {
147 40
            if ($arg !== '')
148 40
            {
149 40
                $paths[] = $arg;
150 40
            }
151 40
        }
152
153 40
        return preg_replace('#(?<!:)/+#', '/', join('/', $paths));
154
    }
155
156
    /**
157
     * Convert a Windows path into a blasphemous Unix path.
158
     *
159
     * @param string $filePath
160
     *
161
     * @return string
162
     */
163 40
    private function unixifyPath($filePath)
164
    {
165 40
        return str_replace('\\', '/', $filePath);
166
    }
167
}
168