Completed
Push — master ( f6ad35...221395 )
by Andrey
02:31
created

Site::__construct()   D

Complexity

Conditions 21
Paths 150

Size

Total Lines 85
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 20
Bugs 0 Features 0
Metric Value
c 20
b 0
f 0
dl 0
loc 85
rs 4.4922
cc 21
eloc 64
nc 150
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace humanity;
3
4
class Site {
5
6
    private static $js;
0 ignored issues
show
Unused Code introduced by
The property $js is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
7
8
    public function __construct(){
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
9
10
        $config = (new Config)->get();
11
12
        if(!isset($_SERVER['HTTP_ACCEPT'])) $_SERVER['HTTP_ACCEPT'] = '*/*';
13
        $acceptSrc = explode(',',$_SERVER['HTTP_ACCEPT']);
14
        foreach($acceptSrc as $key=>$value){
15
            $value = explode('/',$value);
16
            $value[1] = explode(';',$value[1]);
17
            $name = array_shift($value[1]);
18
            $data = [];
19
            foreach($value[1] as $k=>$v){
20
                $v = explode('=',$v);
21
                $data[$v[0]] = $v[1];
22
            }
23
            $accept[$value[0]][$name] = $data;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$accept was never initialized. Although not strictly required by PHP, it is generally a good practice to add $accept = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
24
        }
25
26
        unset($acceptSrc,$key,$value,$name,$data,$k,$v);
27
28
        $requestUri = parse_url($_SERVER['REQUEST_URI']);
29
        if($requestUri['path'] == '/javascript.js'){
30
            header('Content-Type: application/javascript');
31
32
            $tsstring = 'Sun, 03 Jan 2016 11:22:20 GMT';
33
            $etag = md5($tsstring);
34
            $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false;
35
            $if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? $_SERVER['HTTP_IF_NONE_MATCH'] : false;
36
            if ((($if_none_match && $if_none_match == $etag) || (!$if_none_match)) &&
0 ignored issues
show
Bug Best Practice introduced by
The expression $if_none_match of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by
The expression $if_none_match of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
37
                ($if_modified_since && $if_modified_since == $tsstring))
0 ignored issues
show
Bug Best Practice introduced by
The expression $if_modified_since of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
38
            {
39
                header('HTTP/1.1 304 Not Modified');
40
                exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method __construct() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
41
            }
42
            else
43
            {
44
                header("Last-Modified: $tsstring");
45
                header("ETag: $etag");
46
            }
47
            
48
            if(isset($requestUri['query'])){
49
                $query = explode(':',$requestUri['query']);
50
                (new Content)->js($query);
51
            }
52
            exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method __construct() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
53
        }
54
55
        header('Access-Control-Allow-Origin: *');
56
        header('Access-Control-Max-Age: 31556926'); 
57
        header('Access-Control-Allow-Credentials: true'); 
58
        header('Access-Control-Allow-Methods: POST, GET, DELETE, PUT');
59
60
        if(isset($accept['application']['view'])){
61
            header('Content-Type: text/html');
62
            (new RestApi)->view();
63
        } else if(isset($accept['application']['widget'])){
64
            header('Content-Type: text/html');
65
            (new RestApi)->widget();
66
        } else if(isset($accept['application']['apps'])){
67
            header('Content-Type: application/json');
68
            $query = json_decode(file_get_contents('php://input'),1);
69
            $result = [];
70
            foreach($query as $method=>$params){
71
                $stableParams = [];
72
                $file = $config['core']['apps'].'/'.$method.'.php';
73
                $func = require_once($file);
74
                $reflection = new \ReflectionFunction($func);
75
                foreach($reflection->getParameters() as $key=>$value){
76
                    if(isset($params[$value->name])) $stableParams[$value->name] = $params[$value->name];
77
                    else $stableParams[$value->name] = null;
78
                }
79
                $name = explode('/',$method);
80
                $count = count($name)-1;
81
                $app = new Application;
82
                foreach($name as $key=>$var){
83
                    if($count == $key) $app = call_user_method_array($var,$app,$stableParams);
84
                    else $app = $app->{$var};
85
                }
86
                $result[$method] = $app;
87
            }
88
            die(json_encode($result));
0 ignored issues
show
Coding Style Compatibility introduced by
The method __construct() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
89
        } else {
90
            (new Content)->page();
91
        }
92
    }
93
94
}
95
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
96