Completed
Push — master ( a6c8f9...b97be3 )
by Vladimir
03:52 queued 19s
created

File::getBasename()   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 1
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
     * Get the name of the file without an extension.
41
     *
42
     * @param  null $suffix This value will be discarded and is only needed to be able to override the \SplFileInfo
43
     *                      definition.
44
     *
45
     * @since 0.2.0
46
     *
47
     * @return string
48
     */
49 5
    public function getBasename($suffix = null)
50
    {
51 5
        return parent::getBasename('.' . $this->getExtension());
52
    }
53
54
    /**
55
     * Get the name of the with the extension.
56
     *
57
     * @since 0.2.0
58
     *
59
     * @return string
60
     */
61 1
    public function getFilename()
62
    {
63 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...
64
    }
65
66
    /**
67
     * Get the absolute path to this file.
68
     *
69
     * @since 0.2.0
70
     *
71
     * @return string
72
     */
73 129
    public function getAbsolutePath()
74
    {
75 129
        return $this->getPathname();
76
    }
77
78
    /**
79
     * Get the path to the parent folder of this file.
80
     *
81
     * @since 0.2.0
82
     *
83
     * @return string
84
     */
85 1
    public function getParentFolder()
86
    {
87 1
        return $this->getPath();
88
    }
89
90
    /**
91
     * Get the file path to this file, relative to where it was created; likely the current working directory.
92
     *
93
     * @since 0.2.0
94
     *
95
     * @return string
96
     */
97 90
    public function getRelativeFilePath()
98
    {
99 90
        $path = str_replace($this->relativePath, '', $this->getAbsolutePath());
100
101 90
        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...
102
    }
103
104
    /**
105
     * Get the path to the parent folder this file, relative to where it was created; likely the current working directory.
106
     *
107
     * @since 0.2.0
108
     *
109
     * @return string
110
     */
111 1
    public function getRelativeParentFolder()
112
    {
113 1
        return dirname($this->getRelativeFilePath());
114
    }
115
116
    /**
117
     * Get the contents of this file.
118
     *
119
     * @since 0.2.0
120
     *
121
     * @throws \RuntimeException When the file could not be read.
122
     *
123
     * @return string
124
     */
125 126
    public function getContents()
126
    {
127 126
        if (!file_exists($this->getAbsolutePath()))
128 126
        {
129 1
            throw new FileNotFoundException(null, 0, null, $this->getAbsolutePath());
130
        }
131
132 126
        $content = file_get_contents($this->getAbsolutePath());
133
134 126
        if ($content === false)
135 126
        {
136
            $error = error_get_last();
137
            throw new \RuntimeException($error['message']);
138
        }
139
140 126
        return $content;
141
    }
142
}
143