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

WhereTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 23
c 2
b 0
f 2
dl 0
loc 47
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A checkCount() 0 4 2
A replaceParam() 0 6 3
A checkData() 0 20 4
A where() 0 6 2
1
<?php
2
3
namespace HnrAzevedo\Router;
4
5
trait WhereTrait
6
{
7
    use Helper;
8
9
    public static function where(array $wheres): Router
10
    {
11
        $route = self::getInstance()->inSabe();
0 ignored issues
show
Bug introduced by
The method inSabe() does not exist on HnrAzevedo\Router\Router. Did you maybe mean inSave()? ( Ignorable by Annotation )

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

11
        $route = self::getInstance()->/** @scrutinizer ignore-call */ inSabe();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
12
        $route['where'] = (is_array($route['where'])) ? array_merge($route['where'],$wheres) : $wheres;
13
        self::getInstance()->updateRoute($route,array_key_last(self::getInstance()->routes));
0 ignored issues
show
Bug introduced by
The property routes is declared protected in HnrAzevedo\Router\Router and cannot be accessed from this context.
Loading history...
14
        return self::getInstance();
15
    }
16
17
    protected function checkData(string $routePath, string $uriPath): void
18
    {
19
        $routePath = explode('/',urldecode($routePath));
20
        unset($routePath[0]);
21
        $uriPath = explode('/',urldecode($uriPath));
22
        unset($uriPath[0]);
23
24
        $this->checkCount($routePath, $uriPath);
25
        
26
        $corretRoute = true;
27
        foreach($routePath as $r => $routeFrag){
28
            $routeFrag = $this->replaceParam($routeFrag, $uriPath[$r]);
29
30
            if($routeFrag !== $uriPath[$r]){
31
                $corretRoute = false;
32
            }
33
        }
34
35
        if(!$corretRoute){
36
            throw new \Exception('continue');
37
        }
38
    }
39
40
    private function replaceParam(string $ref, string $value): string
41
    {
42
        if(((substr($ref,0,1) === '{') && (substr($ref,strlen($ref)-1) === '}'))) {
43
            return $value;
44
        } 
45
        return $ref;
46
    }
47
48
    private function checkCount(array $routePath, array $uriPath): void
49
    {
50
        if(count($routePath) !== count($uriPath)){
51
            throw new \Exception('Continue');
52
        }
53
    }
54
55
}
56