Completed
Push — master ( 42afef...5f18df )
by Dmitry
06:49 queued 03:50
created

Http::process()   C

Complexity

Conditions 7
Paths 14

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 10.6588

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 29
ccs 11
cts 19
cp 0.5789
rs 6.7272
cc 7
eloc 18
nc 14
nop 1
crap 10.6588
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
    function __construct(Application $app)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
13
    {
14 1
        $this->app = $app;
15 1
    }
16
17 1
    function process($uri)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
18
    {
19 1
        list($controller, $method) = $this->getChain($uri);
20 1
        $className = "Controllers\\$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
    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