Completed
Push — master ( 767878...e7b929 )
by Dmitry
02:47
created

Http   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 0
loc 67
ccs 30
cts 36
cp 0.8333
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C process() 0 36 8
B getChain() 0 20 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 = "Controller\\".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
        $url = '';
0 ignored issues
show
Unused Code introduced by
$url is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
34 1
        if (!method_exists($class, $method)) {
35 1
            if (!method_exists($class, '__process')) {
36
                return "$controller/$method not found";
37
            }
38 1
            $url = substr($uri, strlen($controller)+2);
39 1
            $method = '__process';
40
        } else {
41 1
            $url = substr($uri, (strlen($controller) + strlen($method) + 3));
42
        }
43
44 1
        $container = $this->app->get(Container::class);
45 1
        $result = $container->call([$container->get($class), $method], ['url' => $url]);
46
47 1
        if (is_array($result) || is_object($result)) {
48
            return json_encode($result);
49
        } else {
50 1
            return $result;
51
        }
52
    }
53
54 1
    public function getChain($uri)
55
    {
56 1
        list($clean) = explode('?', $uri);
57 1
        $chain = [];
58 1
        foreach (explode('/', $clean) as $k => $v) {
59 1
            if ($v) {
60 1
                $chain[] = $v;
61
            }
62
        }
63
64 1
        if (!count($chain)) {
65 1
            $chain[] = 'index';
66
        }
67
68 1
        if (count($chain) == 1) {
69 1
            $chain[] = 'index';
70
        }
71
72 1
        return $chain;
73
    }
74
}
75