ProjectFile::toArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Support\Filesystem;
6
7
use function basename;
8
9
use Hyde\Facades\Filesystem;
10
use Hyde\Hyde;
11
use Hyde\Support\Concerns\Serializable;
12
use Hyde\Support\Contracts\SerializableContract;
13
14
/**
15
 * Filesystem abstraction for a file stored in the project.
16
 */
17
abstract class ProjectFile implements SerializableContract
18
{
19
    use Serializable;
20
21
    /**
22
     * @var string The path relative to the project root.
23
     *
24
     * @example `_pages/index.blade.php`
25
     * @example `_media/logo.png`
26
     */
27
    public readonly string $path;
28
29
    public static function make(string $path): static
30
    {
31
        return new static($path);
32
    }
33
34
    public function __construct(string $path)
35
    {
36
        $this->path = Hyde::pathToRelative($path);
0 ignored issues
show
Bug introduced by
The method pathToRelative() does not exist on Hyde\Hyde. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
        /** @scrutinizer ignore-call */ 
37
        $this->path = Hyde::pathToRelative($path);
Loading history...
Bug introduced by
The property path is declared read-only in Hyde\Support\Filesystem\ProjectFile.
Loading history...
37
    }
38
39
    /**
40
     * @return array{name: string, path: string}
41
     */
42
    public function toArray(): array
43
    {
44
        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...
45
            'name' => $this->getName(),
46
            'path' => $this->getPath(),
47
        ];
48
    }
49
50
    public function getName(): string
51
    {
52
        return basename($this->path);
53
    }
54
55
    public function getPath(): string
56
    {
57
        return $this->path;
58
    }
59
60
    public function getAbsolutePath(): string
61
    {
62
        return Hyde::path($this->path);
0 ignored issues
show
Bug introduced by
The method path() does not exist on Hyde\Hyde. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
        return Hyde::/** @scrutinizer ignore-call */ path($this->path);
Loading history...
63
    }
64
65
    public function getContents(): string
66
    {
67
        return Filesystem::getContents($this->path);
68
    }
69
70
    public function getExtension(): string
71
    {
72
        return Filesystem::extension($this->getPath());
73
    }
74
}
75