Completed
Push — master ( 7b9015...c5b311 )
by Vladimir
26s queued 11s
created

BaseFilesystemItem::getRelativeParentFolder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
namespace allejo\stakx\Filesystem;
4
5
use allejo\stakx\Service;
6
use allejo\stakx\Filesystem\FilesystemLoader as fs;
7
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
8
9
class BaseFilesystemItem extends \SplFileInfo
10
{
11
    /** @var string The path relative to the site's working directory. */
12
    protected $relativePath;
13
14
    /** @var string The original raw path given to the constructor. */
15
    protected $rawPath;
16
17
    /**
18
     * @param string $filePath an absolute file path or a path relative to the current working directory
19
     *
20
     * @since 0.2.0
21
     *
22
     * @throws FileNotFoundException
23
     */
24 180
    public function __construct($filePath)
25
    {
26 180
        $this->rawPath = $filePath;
27 180
        $realPath = fs::realpath($filePath);
0 ignored issues
show
Documentation introduced by
$filePath is of type string, but the function expects a object<string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
28
29 180
        if ($realPath === false)
30
        {
31 4
            throw $this->buildNotFoundException();
32
        }
33
34 177
        parent::__construct($realPath);
35
36 177
        $this->relativePath = str_replace(Service::getWorkingDirectory() . DIRECTORY_SEPARATOR, '', $this->getAbsolutePath());
37
38 177
        $this->isSafeToRead();
39 177
    }
40
41
    /**
42
     * Whether or not this file exists on the filesystem.
43
     *
44
     * @return bool
45
     */
46 158
    public function exists()
47
    {
48 158
        return file_exists($this->getAbsolutePath());
49
    }
50
51
    /**
52
     * Get the absolute path to this file.
53
     *
54
     * @since 0.2.0
55
     *
56
     * @return string
57
     */
58 177
    public function getAbsolutePath()
59
    {
60 177
        return $this->getPathname();
61
    }
62
63
    /**
64
     * Get the path to the parent folder of this file.
65
     *
66
     * @since 0.2.0
67
     *
68
     * @return string
69
     */
70 1
    public function getParentFolder()
71
    {
72 1
        return $this->getPath();
73
    }
74
75
    /**
76
     * Get the file path to this file, relative to where it was created; likely the current working directory.
77
     *
78
     * @since 0.2.0
79
     *
80
     * @return string
81
     */
82 150
    public function getRelativeFilePath()
83
    {
84 150
        return $this->relativePath;
85
    }
86
87
    /**
88
     * Get the path to the parent folder this file, relative to where it was created; likely the current working directory.
89
     *
90
     * @since 0.2.0
91
     *
92
     * @return string
93
     */
94 2
    public function getRelativeParentFolder()
95
    {
96 2
        return dirname($this->getRelativeFilePath());
97
    }
98
99
    /**
100
     * Gets the last modified time.
101
     *
102
     * @return int The last modified time for the file, in a Unix timestamp
103
     */
104 1
    public function getLastModified()
105
    {
106 1
        return $this->getMTime();
107
    }
108
109
    /**
110
     * Get the full name of this file or folder.
111
     *
112
     * @return string
113
     */
114 101
    protected function getFullName()
115
    {
116 101
        return parent::getBasename();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getBasename() instead of getFullName()). 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...
117
    }
118
119
    /**
120
     * Check if a file is safe to read.
121
     *
122
     * @throws FileNotFoundException
123
     */
124 177
    protected function isSafeToRead()
125
    {
126 177
        if (fs::isVFS($this->getAbsolutePath()))
0 ignored issues
show
Documentation introduced by
$this->getAbsolutePath() is of type string, but the function expects a object<string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
127
        {
128 131
            return;
129
        }
130
131 51
        if (strpos($this->getAbsolutePath(), Service::getWorkingDirectory()) !== 0)
132
        {
133
            throw $this->buildNotFoundException();
134
        }
135 51
    }
136
137 5
    protected function buildNotFoundException()
138
    {
139 5
        return new FileNotFoundException(
140 5
            sprintf('The given path "%s" does not exist or is outside the website working directory', $this->rawPath),
141 5
            0,
142 5
            null,
143 5
            $this->rawPath
144
        );
145
    }
146
}
147