Passed
Push — master ( c38b6c...e54c12 )
by Caen
03:47 queued 21s
created

RouteListItem::styleRouteKey()   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
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Support\Internal;
6
7
use Hyde\Hyde;
8
use Hyde\Pages\InMemoryPage;
9
use Hyde\Support\Models\Route;
10
use Illuminate\Contracts\Support\Arrayable;
11
12
use function class_basename;
13
use function str_starts_with;
14
15
/**
16
 * @internal This class is internal and should not be depended on outside the HydePHP framework code.
17
 */
18
class RouteListItem implements Arrayable
19
{
20
    protected Route $route;
21
22
    public function __construct(Route $route)
23
    {
24
        $this->route = $route;
25
    }
26
27
    public function toArray(): array
28
    {
29
        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...
30
            'page_type' => $this->stylePageType($this->route->getPageClass()),
31
            'source_file' => $this->styleSourcePath($this->route->getSourcePath()),
32
            'output_file' => $this->styleOutputPath($this->route->getOutputPath()),
33
            'route_key' => $this->styleRouteKey($this->route->getRouteKey()),
34
        ];
35
    }
36
37
    protected function stylePageType(string $class): string
38
    {
39
        return str_starts_with($class, 'Hyde') ? class_basename($class) : $class;
40
    }
41
42
    protected function styleSourcePath(string $path): string
43
    {
44
        return $this->isPageDiscoverable() ? $path : 'none';
45
    }
46
47
    protected function styleOutputPath(string $path): string
48
    {
49
        return Hyde::getOutputDirectory()."/$path";
50
    }
51
52
    protected function styleRouteKey(string $key): string
53
    {
54
        return $key;
55
    }
56
57
    protected function isPageDiscoverable(): bool
58
    {
59
        return $this->route->getSourcePath() && ! $this->route->getPage() instanceof InMemoryPage;
60
    }
61
}
62