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

File::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2
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\Filesystem;
9
10
use allejo\stakx\Service;
11
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
12
13
/**
14
 * A representation of a file on a given filesystem.
15
 *
16
 * This class extends \SplFileInfo and adds new methods along with overriding some methods solely because I feel that
17
 * some of the naming can be misleading.
18
 *
19
 * @since 0.2.0
20
 */
21
final class File extends \SplFileInfo
22
{
23
    private $relativePath;
24
25
    /**
26
     * File Constructor.
27
     *
28
     * @param string $absoluteFilePath  The absolute file path
29
     * @param string|null $relativePath The relative path to its parent folder with respect to the CWD
30
     *
31
     * @since 0.2.0
32
     */
33 95
    public function __construct($absoluteFilePath, $relativePath = null)
34
    {
35 95
        parent::__construct(self::realpath($absoluteFilePath));
36
37 95
        $this->relativePath = $relativePath;
38
39 95
        if ($this->relativePath === null)
40
        {
41 88
            $this->relativePath = str_replace(Service::getWorkingDirectory() . DIRECTORY_SEPARATOR, '', $this->getAbsolutePath());
42
        }
43 95
    }
44
45
    /**
46
     * Whether or not this file exists on the filesystem.
47
     *
48
     * @return bool
49
     */
50 87
    public function exists()
51
    {
52 87
        return file_exists($this->getAbsolutePath());
53
    }
54
55
    /**
56
     * Get the name of the file without an extension.
57
     *
58
     * @param  null $suffix This value will be discarded and is only needed to be able to override the \SplFileInfo
59
     *                      definition.
60
     *
61
     * @since 0.2.0
62
     *
63
     * @return string
64
     */
65 5
    public function getBasename($suffix = null)
66
    {
67 5
        return parent::getBasename('.' . $this->getExtension());
68
    }
69
70
    /**
71
     * Get the name of the with the extension.
72
     *
73
     * @since 0.2.0
74
     *
75
     * @return string
76
     */
77 1
    public function getFilename()
78
    {
79 1
        return parent::getBasename();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getBasename() instead of getFilename()). Are you sure this is correct? If so, you might want to change this to $this->getBasename().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
80
    }
81
82
    /**
83
     * Get the absolute path to this file.
84
     *
85
     * @since 0.2.0
86
     *
87
     * @return string
88
     */
89 90
    public function getAbsolutePath()
90
    {
91 90
        return $this->getPathname();
92
    }
93
94
    /**
95
     * Get the path to the parent folder of this file.
96
     *
97
     * @since 0.2.0
98
     *
99
     * @return string
100
     */
101 1
    public function getParentFolder()
102
    {
103 1
        return $this->getPath();
104
    }
105
106
    /**
107
     * Get the file path to this file, relative to where it was created; likely the current working directory.
108
     *
109
     * @since 0.2.0
110
     *
111
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
112
     */
113 68
    public function getRelativeFilePath()
114
    {
115 68
        return $this->relativePath;
116
    }
117
118
    /**
119
     * Get the path to the parent folder this file, relative to where it was created; likely the current working directory.
120
     *
121
     * @since 0.2.0
122
     *
123
     * @return string
124
     */
125 2
    public function getRelativeParentFolder()
126
    {
127 2
        return dirname($this->getRelativeFilePath());
128
    }
129
130
    /**
131
     * Get the contents of this file.
132
     *
133
     * @since 0.2.0
134
     *
135
     * @throws \RuntimeException When the file could not be read.
136
     *
137
     * @return string
138
     */
139 87
    public function getContents()
140
    {
141 87
        if (!$this->exists())
142
        {
143
            throw new FileNotFoundException(null, 0, null, $this->getAbsolutePath());
144
        }
145
146 87
        $content = file_get_contents($this->getAbsolutePath());
147
148 87
        if ($content === false)
149
        {
150
            $error = error_get_last();
151
            throw new \RuntimeException($error['message']);
152
        }
153
154 87
        return $content;
155
    }
156
157
    /**
158
     * A vfsStream friendly way of getting the realpath() of something.
159
     *
160
     * @param string $path
161
     *
162
     * @return string
163
     */
164 95
    public static function realpath($path)
165
    {
166 95
        if (substr($path, 0, 6) == 'vfs://')
167
        {
168 82
            return $path;
169
        }
170
171 14
        return realpath($path);
172
    }
173
}
174