Passed
Push — master ( 62c53d...86bd74 )
by Caen
03:23 queued 14s
created

RouteListItem::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 5
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\Models;
6
7
use Hyde\Facades\Site;
8
use Hyde\Pages\InMemoryPage;
9
use Illuminate\Contracts\Support\Arrayable;
10
11
/**
12
 * @internal This class is experimental and is subject to change.
13
 * @experimental This class is experimental and is subject to change.
14
 */
15
class RouteListItem implements Arrayable
16
{
17
    protected Route $route;
18
19
    public function __construct(Route $route)
20
    {
21
        $this->route = $route;
22
    }
23
24
    public function toArray(): array
25
    {
26
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('page_type'...>route->getRouteKey())) returns the type array<string,string> which is incompatible with the return type mandated by Illuminate\Contracts\Support\Arrayable::toArray() of Illuminate\Contracts\Support\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...
27
            'page_type'   => $this->stylePageType($this->route->getPageClass()),
28
            'source_file' => $this->styleSourcePath($this->route->getSourcePath()),
29
            'output_file' => $this->styleOutputPath($this->route->getOutputPath()),
30
            'route_key'   => $this->styleRouteKey($this->route->getRouteKey()),
31
        ];
32
    }
33
34
    protected function stylePageType(string $class): string
35
    {
36
        return str_starts_with($class, 'Hyde') ? class_basename($class) : $class;
37
    }
38
39
    protected function styleSourcePath(string $path): string
40
    {
41
        return $this->isPageDiscoverable() ? $path : 'none';
42
    }
43
44
    protected function styleOutputPath(string $path): string
45
    {
46
        return Site::getOutputDirectory()."/$path";
47
    }
48
49
    protected function styleRouteKey(string $key): string
50
    {
51
        return $key;
52
    }
53
54
    protected function isPageDiscoverable(): bool
55
    {
56
        return $this->route->getSourcePath() && ! $this->route->getPage() instanceof InMemoryPage;
57
    }
58
}
59