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

RouteListCommand::routeListClass()

Size

Total Lines 38
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 13
c 3
b 1
f 0
nc 1
nop 0
dl 0
loc 38

5 Methods

Rating   Name   Duplication   Size   Complexity  
A RouteListCommand.php$0 ➔ href() 0 3 1
A RouteListCommand.php$0 ➔ routeToListItem() 0 34 1
A RouteListCommand.php$0 ➔ stylePageType() 0 10 3
A RouteListCommand.php$0 ➔ styleSourcePath() 0 5 2
A RouteListCommand.php$0 ➔ styleOutputPath() 0 5 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Console\Commands;
6
7
use Hyde\Console\Concerns\Command;
8
use Hyde\Hyde;
9
use Hyde\Support\Models\Route;
10
use Hyde\Support\Models\RouteList;
11
use Hyde\Support\Models\RouteListItem;
12
use function file_exists;
13
use function sprintf;
14
15
/**
16
 * Hyde command to display the list of site routes.
17
 *
18
 * @see \Hyde\Framework\Testing\Feature\Commands\RouteListCommandTest
19
 */
20
class RouteListCommand extends Command
21
{
22
    /** @var string */
23
    protected $signature = 'route:list';
24
25
    /** @var string */
26
    protected $description = 'Display all registered routes.';
27
28
    public function handle(): int
29
    {
30
        $routes = $this->routeListClass();
31
32
        $this->table($routes->headers(), $routes->toArray());
33
34
        return Command::SUCCESS;
35
    }
36
37
    protected function routeListClass(): RouteList
38
    {
39
        return new class extends RouteList
40
        {
41
            protected static function routeToListItem(Route $route): RouteListItem
42
            {
43
                return new class($route) extends RouteListItem
44
                {
45
                    protected function stylePageType(string $class): string
46
                    {
47
                        $type = parent::stylePageType($class);
48
49
                        /** @experimental */
50
                        if ($type === 'InMemoryPage' && $this->route->getPage()->hasMacro('typeLabel')) {
0 ignored issues
show
Bug introduced by
The method hasMacro() does not exist on Hyde\Pages\Concerns\HydePage. It seems like you code against a sub-type of Hyde\Pages\Concerns\HydePage such as Hyde\Pages\InMemoryPage. ( Ignorable by Annotation )

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

50
                        if ($type === 'InMemoryPage' && $this->route->getPage()->/** @scrutinizer ignore-call */ hasMacro('typeLabel')) {
Loading history...
51
                            $type .= sprintf(' <fg=gray>(%s)</>', $this->route->getPage()->typeLabel());
0 ignored issues
show
Bug introduced by
The method typeLabel() does not exist on Hyde\Pages\Concerns\HydePage. It seems like you code against a sub-type of Hyde\Pages\Concerns\HydePage such as Hyde\Pages\InMemoryPage. ( Ignorable by Annotation )

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

51
                            $type .= sprintf(' <fg=gray>(%s)</>', $this->route->getPage()->/** @scrutinizer ignore-call */ typeLabel());
Loading history...
52
                        }
53
54
                        return $type;
55
                    }
56
57
                    protected function styleSourcePath(string $path): string
58
                    {
59
                        return parent::styleSourcePath($path) !== 'none'
60
                            ? $this->href(Command::createClickableFilepath(Hyde::path($path)), $path)
61
                            : '<fg=gray>none</>';
62
                    }
63
64
                    protected function styleOutputPath(string $path): string
65
                    {
66
                        return file_exists(Hyde::sitePath($path))
67
                            ? $this->href(Command::createClickableFilepath(Hyde::sitePath($path)), parent::styleOutputPath($path))
68
                            : parent::styleOutputPath($path);
69
                    }
70
71
                    /** @todo Move to base Command class */
72
                    protected function href(string $link, string $label): string
73
                    {
74
                        return "<href=$link>$label</>";
75
                    }
76
                };
77
            }
78
        };
79
    }
80
}
81