Passed
Push — master ( 2aaa83...8f0998 )
by Caen
03:24 queued 13s
created

ProjectFile::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
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
use Stringable;
14
15
/**
16
 * Filesystem abstraction for a file stored in the project.
17
 *
18
 * @see \Hyde\Framework\Testing\Feature\FileTest
19
 */
20
abstract class ProjectFile implements SerializableContract, Stringable
21
{
22
    use Serializable;
23
24
    /**
25
     * @var string The path relative to the project root.
26
     *
27
     * @example `_pages/index.blade.php`
28
     * @example `_media/logo.png`
29
     */
30
    public readonly string $path;
31
32
    public static function make(string $path, ...$args): static
33
    {
34
        return new static($path, ...$args);
0 ignored issues
show
Unused Code introduced by
The call to Hyde\Support\Filesystem\ProjectFile::__construct() has too many arguments starting with $args. ( Ignorable by Annotation )

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

34
        return /** @scrutinizer ignore-call */ new static($path, ...$args);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
35
    }
36
37
    public function __construct(string $path)
38
    {
39
        $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...
40
    }
41
42
    public function __toString(): string
43
    {
44
        return $this->path;
45
    }
46
47
    public function toArray(): array
48
    {
49
        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...
50
            'name' => $this->getName(),
51
            'path' => $this->getPath(),
52
        ];
53
    }
54
55
    public function getName(): string
56
    {
57
        return basename($this->path);
58
    }
59
60
    public function getPath(): string
61
    {
62
        return $this->path;
63
    }
64
65
    public function getAbsolutePath(): string
66
    {
67
        return Hyde::path($this->path);
68
    }
69
70
    public function getContents(): string
71
    {
72
        return Filesystem::getContents($this->path);
73
    }
74
75
    public function getExtension(): string
76
    {
77
        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...
78
    }
79
}
80