Passed
Branch v2-dev (0ddf7c)
by Henri
10:02
created

CheckTrait   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 18
c 6
b 0
f 0
dl 0
loc 44
rs 10
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A checkMethod() 0 10 5
A isInPseudGroup() 0 4 2
A hasRouteName() 0 4 2
A existRouteName() 0 4 2
A isInNameGroup() 0 4 2
1
<?php
2
3
namespace HnrAzevedo\Router;
4
5
trait CheckTrait
6
{
7
    protected array $routers = [];
8
    protected array $routesName = [];
9
    protected ?string $group = null;
10
    
11
    protected function hasRouteName(string $name): void
12
    {
13
        if(!isset($this->routesName[$name])){
14
            throw new \RuntimeException("There is no route named with {$name}");
15
        }
16
    }
17
18
    protected function isInNameGroup(): void
19
    {
20
        if(!is_null($this->group)){
21
            throw new \RuntimeException("It is not allowed to assign names to groups");
22
        }
23
    }
24
25
    protected function isInPseudGroup(): void
26
    {
27
        if(!is_null($this->group)){
28
            throw new \RuntimeException("To assign actions before or after the execution of the route, use beforeGroup or afterGroup");
29
        }
30
    }
31
32
    protected function existRouteName(string $name): void
33
    {
34
        if(isset($this->routesName[$name])){
35
            throw new \RuntimeException("There is already a route named with {$name}");
36
        }
37
    }
38
39
    protected function checkMethod(array $route, $method): void
40
    {
41
        $hasMethod = false;
42
        foreach(explode('|',$route['method']) as $routeMethod){
43
            if(@preg_match("/{$routeMethod}/",$method) == true || $method === '*'){
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing @preg_match('/'.$routeMethod.'/', $method) of type integer to the boolean true. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
44
                $hasMethod = true;
45
            }
46
        }
47
        if(!$hasMethod){
48
            throw new \Exception('This route is not released for the accessed method');
49
        }
50
        
51
    }
52
53
}
54