Completed
Push — master ( b93a9c...e7c2e1 )
by Alexey
05:14
created

Materials   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 112
Duplicated Lines 59.82 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 67
loc 112
rs 10
c 1
b 0
f 0
wmc 22
lcom 0
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
B viewsList() 0 19 5
A templatesList() 16 16 3
A viewsCategoryList() 0 18 4
A templatesCategoryList() 16 16 3
C sitemap() 35 35 7

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
3
/**
4
 * Materials module
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
class Materials extends Module
12
{
13
    public function viewsList()
14
    {
15
        $return = [
16
            'inherit' => 'Как у родителя',
17
            'default' => 'Стандартная страница',
18
            'materialWithCategorys' => 'Страница со списком категорий',
19
        ];
20
        $conf = App::$primary->view->template->config;
21
22
        if (!empty($conf['files']['modules']['Materials'])) {
23
24
            foreach ($conf['files']['modules']['Materials'] as $file) {
25
                if (!empty($file['type']) && $file['type'] == 'Material') {
26
                    $return[$file['file']] = $file['name'];
27
                }
28
            }
29
        }
30
        return $return;
31
    }
32
33 View Code Duplication
    public function templatesList()
34
    {
35
        $return = [
36
            'inherit' => 'Как у родителя',
37
            'current' => 'Текущая тема'
38
        ];
39
40
        $conf = App::$primary->view->template->config;
41
42
        if (!empty($conf['files']['aditionTemplateFiels'])) {
43
            foreach ($conf['files']['aditionTemplateFiels'] as $file) {
44
                $return[$file['file']] = '- ' . $file['name'];
45
            }
46
        }
47
        return $return;
48
    }
49
50
    public function viewsCategoryList()
51
    {
52
        $return = [
53
            'inherit' => 'Как у родителя',
54
            'category' => 'Стандартная категория',
55
        ];
56
        $conf = App::$primary->view->template->config;
57
58
        if (!empty($conf['files']['modules']['Materials'])) {
59
60
            foreach ($conf['files']['modules']['Materials'] as $file) {
61
                if ($file['type'] == 'Category') {
62
                    $return[$file['file']] = $file['name'];
63
                }
64
            }
65
        }
66
        return $return;
67
    }
68
69 View Code Duplication
    public function templatesCategoryList()
70
    {
71
        $return = [
72
            'inherit' => 'Как у родителя',
73
            'current' => 'Текущая тема'
74
        ];
75
76
        $conf = App::$primary->view->template->config;
77
78
        if (!empty($conf['files']['aditionTemplateFiels'])) {
79
            foreach ($conf['files']['aditionTemplateFiels'] as $file) {
80
                $return[$file['file']] = '- ' . $file['name'];
81
            }
82
        }
83
        return $return;
84
    }
85
86 View Code Duplication
    function sitemap()
1 ignored issue
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...
Coding Style introduced by
sitemap 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...
87
    {
88
        $map = [];
89
        $zeroMaterials = \Materials\Material::getList(['where' => ['category_id', 0]]);
90
        foreach ($zeroMaterials as $mat) {
91
            $map[] = [
92
                'name' => $mat->name,
93
                'url' => [
94
                    'loc' => (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . INJI_DOMAIN_NAME . ($mat->getHref())
95
                ],
96
            ];
97
        }
98
        
99
        $categorys = \Materials\Category::getList(['where' => ['parent_id', 0]]);
100
        $scan = function($category, $scan) {
101
            $map = [];
102
            
103
            foreach ($category->items as $mat) {
104
                $map[] = [
105
                    'name' => $mat->name,
106
                    'url' => [
107
                        'loc' => (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . INJI_DOMAIN_NAME . ($mat->getHref())
108
                    ],
109
                ];
110
            }
111
            foreach ($category->childs as $child) {
112
                $map = array_merge($map, $scan($child, $scan));
113
            }
114
            return $map;
115
        };
116
        foreach ($categorys as $category) {
117
            $map = array_merge($map, $scan($category, $scan));
118
        }
119
        return $map;
120
    }
121
122
}
123