Completed
Push — master ( 9ca9d9...72ab7d )
by Dmitry
04:11 queued 01:35
created

Http   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 78.38%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 13
c 4
b 1
f 1
lcom 1
cbo 1
dl 0
loc 61
ccs 29
cts 37
cp 0.7838
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C process() 0 29 7
B getChain() 0 21 5
1
<?php
2
3
namespace Basis;
4
5
use League\Container\Container;
6
use LogicException;
7
8
class Http
9
{
10
    private $app;
11
12 1
public     function __construct(Application $app)
13
    {
14 1
        $this->app = $app;
15 1
    }
16
17 1
    public function process($uri)
18
    {
19 1
        list($controller, $method) = $this->getChain($uri);
20 1
        $className = "Controllers\\".ucfirst($controller);
21 1
        $class = $this->app->get(Filesystem::class)->completeClassName($className);
22 1
        if(!class_exists($class)) {
23
            $frameworkClass = $this->app->get(Framework::class)->completeClassName($className);
24
        }
25
26 1
        if(!class_exists($class)) {
27
            if(!class_exists($frameworkClass)) {
0 ignored issues
show
Bug introduced by
The variable $frameworkClass does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
28
                throw new LogicException("No class for $controller $controller, [$class, $frameworkClass]");
29
            }
30
            $class = $frameworkClass;
31
        }
32
33 1
        if(!method_exists($class, $method)) {
34
            return "$controller/$method not found";
35
        }
36
37 1
        $container = $this->app->get(Container::class);
38 1
        $result = $container->call([$container->get($class), $method]);
39
40 1
        if(is_array($result) || is_object($result)) {
41
            return json_encode($result);
42
        } else {
43 1
            return $result;
44
        }
45
    }
46
47 1
    public function getChain($uri)
48
    {
49 1
        $chain = explode('/', $uri);
50 1
        foreach($chain as $k => $v) {
51 1
            if(!$v) {
52 1
                unset($chain[$k]);
53 1
            }
54 1
        }
55
56 1
        $chain = array_values($chain);
57
58 1
        if(!count($chain)) {
59 1
            $chain[] = 'index';
60 1
        }
61
62 1
        if(count($chain) == 1) {
63 1
            $chain[] = 'index';
64 1
        }
65
66 1
        return $chain;
67
    }
68
}
69