Completed
Push — master ( 3c4d84...92e7ec )
by Vladimir
02:30
created

FilesystemPath::generatePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace allejo\stakx\Filesystem;
4
5
use allejo\stakx\Filesystem\FilesystemLoader as fs;
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 string */
16
    private $absolutePath;
17
    /** @var string */
18
    private $originalPath;
19
    /** @var bool */
20
    private $isWindows;
21
22
    /**
23
     * @param string $filePath
24
     * @param string $dirSep
25
     */
26 12
    public function __construct($filePath, $dirSep = DIRECTORY_SEPARATOR)
27
    {
28 12
        $this->originalPath = $filePath;
29 12
        $this->isWindows = ($dirSep === '\\');
30
31 12
        if ($this->isWindows)
32
        {
33 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...
34
        }
35
36 12
        $this->absolutePath = (fs::isAbsolutePath($filePath)) ? $filePath : fs::absolutePath($filePath);
0 ignored issues
show
Bug introduced by
The method isAbsolutePath() does not exist on allejo\stakx\Filesystem\FilesystemLoader. Did you maybe mean absolutePath()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
37 12
    }
38
39 10
    public function __toString()
40
    {
41 10
        return $this->getAbsolutePath();
42
    }
43
44
    /**
45
     * Append a path to a directory path.
46
     *
47
     * @param string $append The path to append
48
     */
49 5
    public function appendToPath($append)
50
    {
51 5
        if ($this->isFile(false))
52
        {
53 1
            throw new \InvalidArgumentException("Appending to a file's path is not possible");
54
        }
55
56 4
        $this->absolutePath = $this->buildPath($this->absolutePath, $this->unixifyPath($append));
57 4
    }
58
59
    /**
60
     * Generate a path based off this file path.
61
     *
62
     * This method will not modify the existing file path of this instance, use FilesystemPath::appendToPath() for that.
63
     *
64
     * @param string $append
65
     *
66
     * @return string
67
     */
68
    public function generatePath($append)
69
    {
70
        return $this->buildPath($this->absolutePath, $this->unixifyPath($append));
71
    }
72
73
    /**
74
     * Get the absolute path of the file path.
75
     *
76
     * @return string
77
     */
78 10
    public function getAbsolutePath()
79
    {
80 10
        if ($this->isWindows)
81
        {
82 2
            return str_replace('/', '\\', $this->absolutePath);
83
        }
84
85 8
        return $this->absolutePath;
86
    }
87
88
    /**
89
     * Check whether the given path is a directory.
90
     *
91
     * @param bool $checkExistence When set to true, it will check the filesystem for the existence of the directory.
92
     *                             When set to false, this function will guess based on the path ending in a directory
93
     *                             separator.
94
     *
95
     * @return bool
96
     */
97 6
    public function isDir($checkExistence = true)
98
    {
99 6
        $absPath = $this->absolutePath;
100
101 6
        if ($checkExistence)
102
        {
103
            return file_exists($absPath) && is_dir($absPath);
104
        }
105
106 6
        return (substr($absPath, -1, 1) == '/');
107
    }
108
109
    /**
110
     * Check whether the given path is a file.
111
     *
112
     * @param bool $checkExistence When set to true, it will check the filesystem for the existence of the file. When
113
     *                             set to false, this function will guess based on the path ending in a directory
114
     *                             separator.
115
     *
116
     * @return bool
117
     */
118 5
    public function isFile($checkExistence = true)
119
    {
120 5
        $absPath = $this->absolutePath;
121
122 5
        if ($checkExistence)
123
        {
124
            return file_exists($absPath) && is_file($absPath);
125
        }
126
127 5
        return (!$this->isDir($checkExistence));
128
    }
129
130
    /**
131
     * Build a path from multiple strings.
132
     *
133
     * This function will _always_ use the '/' as the directory separator, because internal that's all stakx will use.
134
     * The FilesystemPath::getAbsolutePath() function will worry about Windows paths when necessary.
135
     *
136
     * @return string
137
     */
138 4 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...
139
    {
140 4
        $paths = [];
141
142 4
        foreach (func_get_args() as $arg)
143
        {
144 4
            if ($arg !== '')
145
            {
146 4
                $paths[] = $arg;
147
            }
148
        }
149
150 4
        return preg_replace('#(?<!:)/+#', '/', join('/', $paths));
151
    }
152
153
    /**
154
     * Convert a Windows path into a blasphemous Unix path.
155
     *
156
     * @param string $filePath
157
     *
158
     * @return string
159
     */
160 4
    private function unixifyPath($filePath)
161
    {
162 4
        return str_replace('\\', '/', $filePath);
163
    }
164
}
165