Passed
Push — master ( 934657...c3ba90 )
by 世昌
02:07
created

GroupRoutes::merge()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
namespace nebula\application\route;
3
4
use nebula\application\NameFinder;
0 ignored issues
show
Bug introduced by
The type nebula\application\NameFinder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
5
use nebula\component\route\RouteMatcher;
6
use nebula\component\route\RouteCollection;
7
use nebula\component\cacheable\CacheableTrait;
8
use nebula\component\cacheable\CacheableInterface;
9
10
/**
11
 * 应用
12
 *
13
 */
14
class GroupRoutes implements CacheableInterface
15
{
16
    use CacheableTrait;
17
    
18
    /**
19
     * 路由集合
20
     *
21
     * @var RouteCollection[]
22
     */
23
    protected $routes = [];
24
25
    /**
26
     * 默认分组
27
     *
28
     * @var string
29
     */
30
    protected $default = 'global:';
31
32
    /**
33
     * 组名
34
     *
35
     * @var string
36
     */
37
    protected $name;
38
39
    /**
40
     * 分组名
41
     *
42
     * @param string $name
43
     */
44
    public function __construct(string $name)
45
    {
46
        $this->name = $name;
47
    }
48
49
    /**
50
     * 注册路由
51
     *
52
     * @param string|null $module 模块名
53
     * @param string $name 路由名
54
     * @param RouteMatcher $router 路由匹配
55
     * @return void
56
     */
57
    public function add(?string $module = null, string $name, RouteMatcher $router)
58
    {
59
        $module =  $module ?: $this->default;
60
        if (\array_key_exists($module, $this->routes)) {
61
            $this->routes[$module]->add($name, $router);
62
        } else {
63
            $this->routes[$module] = new RouteCollection([$name => $router]);
64
        }
65
    }
66
67
    /**
68
     * 合并路由组
69
     *
70
     * @param GroupRoutes $routes
71
     * @return void
72
     */
73
    public function merge(GroupRoutes $routes)
74
    {
75
        $name = $routes->name;
76
        if (\array_key_exists($name, $this->routes)) {
77
            $this->routes[$name]->merge($routes->routes);
0 ignored issues
show
Bug introduced by
$routes->routes of type nebula\component\route\RouteCollection[] is incompatible with the type nebula\component\route\RouteCollection expected by parameter $route of nebula\component\route\RouteCollection::merge(). ( Ignorable by Annotation )

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

77
            $this->routes[$name]->merge(/** @scrutinizer ignore-type */ $routes->routes);
Loading history...
78
        } else {
79
            $this->routes[$name] = $routes;
80
        }
81
    }
82
83
    /**
84
     * 搜索路由
85
     *
86
     * @param string|null $module
87
     * @param string $name
88
     * @param string|null $default
89
     * @return RouteMatcher|null
90
     */
91
    public function find(?string $module = null, string $name, ?string $default = null):?RouteMatcher
0 ignored issues
show
Unused Code introduced by
The parameter $default is not used and could be removed. ( Ignorable by Annotation )

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

91
    public function find(?string $module = null, string $name, /** @scrutinizer ignore-unused */ ?string $default = null):?RouteMatcher

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
92
    {
93
        $module =  $module ?: $this->default;
94
        if (\array_key_exists($module, $this->routes)) {
95
            return $this->routes[$module]->get($name);
96
        }
97
        return null;
98
    }
99
100
    public function getCacheProperty():array
101
    {
102
        return ['routes'];
103
    }
104
105
    public function getCacheKey():string
106
    {
107
        return 'routes:'.$this->name;
108
    }
109
}
110