Passed
Push — master ( ecb50c...13bb1e )
by Caen
03:58 queued 21s
created

ProjectFile::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Support\Filesystem;
6
7
use function basename;
8
use Hyde\Facades\Filesystem;
9
use Hyde\Hyde;
10
use Hyde\Support\Concerns\Serializable;
11
use Hyde\Support\Contracts\SerializableContract;
12
use function pathinfo;
13
14
/**
15
 * Filesystem abstraction for a file stored in the project.
16
 *
17
 * @see \Hyde\Framework\Testing\Feature\Support\ProjectFileTest
18
 */
19
abstract class ProjectFile implements SerializableContract
20
{
21
    use Serializable;
22
23
    /**
24
     * @var string The path relative to the project root.
25
     *
26
     * @example `_pages/index.blade.php`
27
     * @example `_media/logo.png`
28
     */
29
    public readonly string $path;
30
31
    public static function make(string $path): static
32
    {
33
        return new static($path);
34
    }
35
36
    public function __construct(string $path)
37
    {
38
        $this->path = Hyde::pathToRelative($path);
0 ignored issues
show
Bug introduced by
The property path is declared read-only in Hyde\Support\Filesystem\ProjectFile.
Loading history...
39
    }
40
41
    /**
42
     * @return array{name: string, path: string}
43
     */
44
    public function toArray(): array
45
    {
46
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('name' => $...h' => $this->getPath()) returns the type array<string,string> which is incompatible with the return type mandated by Hyde\Support\Contracts\S...ableContract::toArray() of Hyde\Support\Contracts\TValue[].

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
47
            'name' => $this->getName(),
48
            'path' => $this->getPath(),
49
        ];
50
    }
51
52
    public function getName(): string
53
    {
54
        return basename($this->path);
55
    }
56
57
    public function getPath(): string
58
    {
59
        return $this->path;
60
    }
61
62
    public function getAbsolutePath(): string
63
    {
64
        return Hyde::path($this->path);
65
    }
66
67
    public function getContents(): string
68
    {
69
        return Filesystem::getContents($this->path);
70
    }
71
72
    public function getExtension(): string
73
    {
74
        return pathinfo($this->getAbsolutePath(), PATHINFO_EXTENSION);
0 ignored issues
show
Bug Best Practice introduced by
The expression return pathinfo($this->g...tem\PATHINFO_EXTENSION) could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
75
    }
76
}
77