Completed
Push — master ( d47583...518897 )
by Andrey
02:51
created

Content   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 121
Duplicated Lines 6.61 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 26
Bugs 0 Features 0
Metric Value
wmc 17
c 26
b 0
f 0
lcom 1
cbo 10
dl 8
loc 121
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 8 36 5
A view() 0 4 2
B page() 0 27 1
A js() 0 3 1
C file() 0 29 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace humanity;
3
4
class Content {
5
6
    private static $host;
7
    private static $path;
8
    private static $config; 
9
    private static $js;
10
    private static $css;
11
    private static $app;
12
    private static $view;
13
    private static $widget;
14
    private static $title;
15
    private static $description;
16
    private static $keywords;
17
    private static $uri;
18
19
    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...
20
        # Config
21
        self::$config = (new Config)->get();
22
        # Host
23
        self::$host = parse_url('http://'.$_SERVER['HTTP_HOST'])['host'];
24
        # Url
25
        self::$path = explode('/',parse_url(urldecode($_SERVER['REQUEST_URI']))['path']);
26 View Code Duplication
        foreach(self::$path as $key=>$value){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
            $value = trim($value);
28
            if(empty($value)) unset(self::$path[$key]);
29
        }
30
        self::$path = array_values(self::$path);
31
        # Application
32
        self::$app = new Application;
33
        # View
34
        self::$view = new View;
35
        # Widget
36
        self::$widget = new Widget;
37
        # Js
38
        self::$js = new Js;
39
        # Css
40
        self::$css = new Css;
41
        # Title
42
        self::$title = new Title;
43
        # Desctiption
44
        self::$description = new Description;
45
        # Keywords
46
        self::$keywords = new Keywords;
47
        # Uri
48
        self::$uri = explode('/',$_SERVER['REQUEST_URI']);
49 View Code Duplication
        foreach(self::$uri as $key=>$value){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
            if(empty($value)) unset(self::$uri[$key]);
51
            else self::$uri[$key] = urldecode($value);
52
        }
53
        self::$uri = array_values(self::$uri);
54
    }
55
56
    public function view($name){
57
        $file = $this->file($name);
58
        if(is_file($file)) require $file;
59
    }
60
61
    public function page(){
62
        # get file
63
        $file = $this->file();
64
        # get content
65
        ob_start();
66
        require $file;
67
        $content = ob_get_clean();
68
        # get css
69
        $css = self::$css->get();
70
        # get css
71
        $js = self::$js->get();
72
        # get title
73
        $title = self::$title->get();
74
        # get description
75
        $description = self::$description->get();
76
        # get keywords
77
        $keywords = self::$keywords->get();
78
        # include for page
79
        $content = preg_replace('/<head>/',"<head>$keywords",$content);
80
        $content = preg_replace('/<head>/',"<head>$description",$content);
81
        $content = preg_replace('/<head>/',"<head>$title",$content);
82
        $content = preg_replace('/<\/head>/',"$css</head>",$content);
83
        $content = preg_replace('/<\/body>/',"$js</body>",$content);
84
        $minify = new MinifyHTML($content);
85
        $content = $minify->compress();
86
        echo $content;
87
    }
88
89
    public function js($query){
90
        echo self::$js->compile($query);
91
    }
92
93
    # $type - page,view
94
    public function file($name=false){
95
        if($name === false){
96
            $path = self::$config['core']['page'];
97
        } else {
98
            $path = self::$config['core']['view'].'/'.$name;
99
        }
100
        # Route
101
        $uri = self::$path;
102
        array_push($uri,'');
103
        foreach($uri as $key=>$value){ if(empty(trim($value))) { unset($uri[$key]); continue; } }
104
        $uri = array_values($uri);
105
        $isFile = null;
0 ignored issues
show
Unused Code introduced by
$isFile 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...
106
        $currentUri = null;
0 ignored issues
show
Unused Code introduced by
$currentUri 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...
107
        $filePhtml = null;
108
        for($i=count($uri)-1; $i>=0; $i--){
109
          $nextUri = implode('/',array_slice($uri,0,$i+1));
110
          $getFile = function($type) use($path,$nextUri){
111
            $file = $path.'/'.$nextUri.'/index.'.$type;
112
            if(is_file($file)) return $file;
113
            return null;
114
          };
115
          $filePhtml = $getFile('phtml');
116
          if($filePhtml) { $currentUri = $nextUri; break; }
0 ignored issues
show
Unused Code introduced by
$currentUri 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...
117
        }
118
        if($filePhtml === null) {
119
          $filePhtml = $path.'/index.phtml';
120
        }
121
        return $filePhtml;
122
    }
123
124
}
125
?>
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...
126