Completed
Push — master ( 538c9f...f6ad35 )
by Andrey
03:04 queued 53s
created

Widget   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 42
Duplicated Lines 9.52 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 11
Bugs 0 Features 0
Metric Value
wmc 9
c 11
b 0
f 0
lcom 0
cbo 5
dl 4
loc 42
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 19 3
A __get() 0 4 1
B __call() 0 5 5

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 Widget {
5
6
    public static $js;
7
    public static $css;
8
    private static $config;
9
    private static $app;
10
    private static $view;
11
    private static $property;
12
    private static $path;
13
14
    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...
15
        # Config
16
        self::$config = (new Config)->get();
17
        # Url
18
        self::$path = explode('/',parse_url(urldecode($_SERVER['REQUEST_URI']))['path']);
19 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...
20
            $value = trim($value);
21
            if(empty($value)) unset(self::$path[$key]);
22
        }
23
        self::$path = array_values(self::$path);
24
        # Application
25
        self::$app = new Application;
26
        # View
27
        self::$view = new View;
28
        # Js
29
        self::$js = new Js;
30
        # Css
31
        self::$css = new Css;
32
    }
33
34
    public function __get($name){
35
        self::$property= $name;
36
        return new self;
37
    }
38
39
    public function __call($_name,$value){
40
        if(isset($value[0]) && !empty($value[0]) && is_array($value[0])) extract($value[0]);
41
        $file = self::$config['core']['widget'].'/'.self::$property.'/'.$_name.'.phtml';
42
        if(is_file($file)) require($file);
43
    }
44
45
}
46
?>
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...
47