Completed
Push — master ( a41cf5...7efa4f )
by Vladimir
02:55
created

File::exists()   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
/**
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 Symfony\Component\Filesystem\Exception\FileNotFoundException;
11
12
/**
13
 * A representation of a file on a given filesystem.
14
 *
15
 * This class extends \SplFileInfo and adds new methods along with overriding some methods solely because I feel that
16
 * some of the naming can be misleading.
17
 *
18
 * @since 0.2.0
19
 */
20
final class File extends \SplFileInfo
21
{
22
    private $relativePath;
23
24
    /**
25
     * File Constructor.
26
     *
27
     * @param string $absoluteFilePath  The absolute file path
28
     * @param string|null $relativePath The relative path to its parent folder with respect to the CWD
29
     *
30
     * @since 0.2.0
31
     */
32 156
    public function __construct($absoluteFilePath, $relativePath = null)
33
    {
34 156
        $this->relativePath = ($relativePath === null) ? getcwd() : $relativePath;
35
36 156
        parent::__construct($absoluteFilePath);
37 156
    }
38
39
    /**
40
     * Whether or not this file exists on the filesystem.
41
     *
42
     * @return bool
43
     */
44 126
    public function exists()
45
    {
46 126
        return file_exists($this->getAbsolutePath());
47
    }
48
49
    /**
50
     * Get the name of the file without an extension.
51
     *
52
     * @param  null $suffix This value will be discarded and is only needed to be able to override the \SplFileInfo
53
     *                      definition.
54
     *
55
     * @since 0.2.0
56
     *
57
     * @return string
58
     */
59 5
    public function getBasename($suffix = null)
60
    {
61 5
        return parent::getBasename('.' . $this->getExtension());
62
    }
63
64
    /**
65
     * Get the name of the with the extension.
66
     *
67
     * @since 0.2.0
68
     *
69
     * @return string
70
     */
71 1
    public function getFilename()
72
    {
73 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...
74
    }
75
76
    /**
77
     * Get the absolute path to this file.
78
     *
79
     * @since 0.2.0
80
     *
81
     * @return string
82
     */
83 130
    public function getAbsolutePath()
84
    {
85 130
        return $this->getPathname();
86
    }
87
88
    /**
89
     * Get the path to the parent folder of this file.
90
     *
91
     * @since 0.2.0
92
     *
93
     * @return string
94
     */
95 1
    public function getParentFolder()
96
    {
97 1
        return $this->getPath();
98
    }
99
100
    /**
101
     * Get the file path to this file, relative to where it was created; likely the current working directory.
102
     *
103
     * @since 0.2.0
104
     *
105
     * @return string
106
     */
107 107
    public function getRelativeFilePath()
108
    {
109 107
        $path = str_replace($this->relativePath, '', $this->getAbsolutePath());
110
111 107
        return ltrim($path, "/\\");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal /\\ does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
112
    }
113
114
    /**
115
     * Get the path to the parent folder this file, relative to where it was created; likely the current working directory.
116
     *
117
     * @since 0.2.0
118
     *
119
     * @return string
120
     */
121 1
    public function getRelativeParentFolder()
122
    {
123 1
        return dirname($this->getRelativeFilePath());
124
    }
125
126
    /**
127
     * Get the contents of this file.
128
     *
129
     * @since 0.2.0
130
     *
131
     * @throws \RuntimeException When the file could not be read.
132
     *
133
     * @return string
134
     */
135 126
    public function getContents()
136
    {
137 126
        if (!$this->exists())
138
        {
139
            throw new FileNotFoundException(null, 0, null, $this->getAbsolutePath());
140
        }
141
142 126
        $content = file_get_contents($this->getAbsolutePath());
143
144 126
        if ($content === false)
145
        {
146
            $error = error_get_last();
147
            throw new \RuntimeException($error['message']);
148
        }
149
150 126
        return $content;
151
    }
152
}
153