FileInfo   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 187
Duplicated Lines 12.83 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 0
dl 24
loc 187
ccs 59
cts 59
cp 1
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getBasename() 0 8 2
A isExists() 0 4 1
A createPathname() 0 16 2
A purifyPath() 0 8 1
A purifyBasename() 0 8 1
A purifyExtension() 0 8 1
A changePath() 0 6 1
A changeBasename() 0 6 1
A changeExtension() 0 6 1
A getPathRelativeTo() 12 12 2
A getPathnameRelativeTo() 12 12 2
A toString() 0 4 1

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
namespace FileNamingResolver;
4
5
/**
6
 * An IMMUTABLE object which represents abstract or real file or directory (folder)
7
 *
8
 * @author Victor Bocharsky <[email protected]>
9
 */
10
class FileInfo extends \SplFileInfo
11
{
12
    const SEPARATOR_DIRECTORY = DIRECTORY_SEPARATOR;
13
    const SEPARATOR_EXTENSION = '.';
14
15
    /**
16
     * Returns base name of file without extension (or base name of directory).
17
     *
18
     * @param string $suffix
19
     *
20
     * @return string
21
     */
22 13
    public function getBasename($suffix = null)
23
    {
24 13
        if (null === $suffix) {
25 13
            $suffix = static::SEPARATOR_EXTENSION.$this->getExtension();
26 13
        }
27
28 13
        return parent::getBasename((string)$suffix);
29
    }
30
31
    /**
32
     * Checks whether a file / directory exists.
33
     *
34
     * @return bool
35
     */
36 1
    public function isExists()
37
    {
38 1
        return file_exists($this->toString());
39
    }
40
41
    /**
42
     * Creates full pathname to the file / directory based on its path, basename and extension
43
     *
44
     * @param string $path Directory path to the file (or directory)
45
     * @param string $basename Base name of file without extension (or base name of directory)
46
     * @param string $extension [OPTIONAL] File extension (empty for directory)
47
     *
48
     * @return string
49
     */
50 13
    public static function createPathname($path, $basename, $extension = '')
51
    {
52
        $pathname = ''
53 13
            .static::purifyPath($path)
54 13
            .static::SEPARATOR_DIRECTORY
55 13
            .static::purifyBasename($basename)
56 13
        ;
57 13
        if ($extension) {
58
            $pathname .= ''
59
                .static::SEPARATOR_EXTENSION
60 13
                .static::purifyExtension($extension)
61 13
            ;
62 13
        }
63
64 13
        return $pathname;
65
    }
66
67
    /**
68
     * @param string $path
69
     *
70
     * @return string
71
     */
72 16
    public static function purifyPath($path)
73
    {
74 16
        $path = (string)$path;
75 16
        $path = trim($path);
76 16
        $path = rtrim($path, '\\/');
77
78 16
        return $path;
79
    }
80
81
    /**
82
     * @param string $basename
83
     *
84
     * @return string
85
     */
86 14
    public static function purifyBasename($basename)
87
    {
88 14
        $basename = (string)$basename;
89 14
        $basename = trim($basename);
90 14
        $basename = trim($basename, '\\/');
91
92 14
        return $basename;
93
    }
94
95
    /**
96
     * @param string $extension
97
     *
98
     * @return string
99
     */
100 14
    public static function purifyExtension($extension)
101
    {
102 14
        $extension = (string)$extension;
103 14
        $extension = trim($extension);
104 14
        $extension = ltrim($extension, '.');
105
106 14
        return $extension;
107
    }
108
109
    /**
110
     * Changes directory path to the file / directory.
111
     *
112
     * @param string $path
113
     *
114
     * @return FileInfo
115
     */
116 10
    public function changePath($path)
117
    {
118 10
        $pathname = $this->createPathname($path, $this->getBasename(), $this->getExtension());
119
120 10
        return new static($pathname);
121
    }
122
123
    /**
124
     * Changes base name of file without extension (or base name of directory).
125
     *
126
     * @param string $basename
127
     *
128
     * @return FileInfo
129
     */
130 10
    public function changeBasename($basename)
131
    {
132 10
        $pathname = $this->createPathname($this->getPath(), $basename, $this->getExtension());
133
134 10
        return new static($pathname);
135
    }
136
137
    /**
138
     * Changes file extension.
139
     *
140
     * @param string $extension
141
     *
142
     * @return FileInfo
143
     */
144 1
    public function changeExtension($extension)
145
    {
146 1
        $pathname = $this->createPathname($this->getPath(), $this->getBasename(), $extension);
147
148 1
        return new static($pathname);
149
    }
150
151
    /**
152
     * @param string $basePath
153
     *
154
     * @return string
155
     */
156 1 View Code Duplication
    public function getPathRelativeTo($basePath)
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...
157
    {
158 1
        $basePath = $this->purifyPath($basePath).static::SEPARATOR_DIRECTORY;
159
160 1
        $relativePath = $this->getPath();
161 1
        $prefix = mb_substr($relativePath, 0, mb_strlen($basePath));
162 1
        if ($prefix === $basePath) {
163 1
            $relativePath = mb_substr($relativePath, mb_strlen($prefix));
164 1
        }
165
166 1
        return $relativePath;
167
    }
168
169
    /**
170
     * @param string $basePathname
171
     *
172
     * @return string
173
     */
174 5 View Code Duplication
    public function getPathnameRelativeTo($basePathname)
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...
175
    {
176 5
        $basePathname = $this->purifyPath($basePathname).static::SEPARATOR_DIRECTORY;
177
178 5
        $relativePathname = $this->getPathname();
179 5
        $prefix = mb_substr($relativePathname, 0, mb_strlen($basePathname));
180 5
        if ($prefix === $basePathname) {
181 5
            $relativePathname = mb_substr($relativePathname, mb_strlen($prefix));
182 5
        }
183
184 5
        return $relativePathname;
185
    }
186
187
    /**
188
     * Converts object to a string representation.
189
     *
190
     * @return string
191
     */
192 11
    public function toString()
193
    {
194 11
        return (string)$this;
195
    }
196
}
197