Completed
Branch master (00e474)
by Michael
05:07
created

oledrion.php ➔ b_sitemap_oledrion()   B

Complexity

Conditions 8
Paths 10

Size

Total Lines 58
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 44
nc 10
nop 0
dl 0
loc 58
rs 7.1977
c 0
b 0
f 0

How to fix   Long Method   

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
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
/**
13
 * oledrion
14
 *
15
 * @copyright   The XOOPS Project http://sourceforge.net/projects/xoops/
16
 * @license     http://www.fsf.org/copyleft/gpl.html GNU public license
17
 * @author      Hervé Thouzard (http://www.herve-thouzard.com/)
18
 * @version     $Id: oledrion.php 12290 2014-02-07 11:05:17Z beckmi $
19
 */
20
21
function b_sitemap_oledrion()
22
{
23
    require '../oledrion/header.php';
24
    global $sitemap_configs;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
25
    $xoopsDB = XoopsDatabaseFactory::getDatabaseConnection();
26
    $table = $xoopsDB->prefix('oledrion_cat');
27
    $id_name = 'cat_cid';
28
    $pid_name = 'cat_pid';
29
    $title_name = 'cat_title';
30
    $url = 'category.php?cat_cid=';
0 ignored issues
show
Unused Code introduced by
$url 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...
31
    $order = 'cat_title';
32
33
    include_once XOOPS_ROOT_PATH . '/class/xoopstree.php';
34
    $mytree = new XoopsTree($table, $id_name, $pid_name);
35
    $xoopsDB = XoopsDatabaseFactory::getDatabaseConnection();
36
37
    $sitemap = array();
38
    $myts = MyTextSanitizer::getInstance();
39
40
    $i = 0;
41
    $sql = "SELECT `$id_name`,`$title_name` FROM `$table` WHERE `$pid_name`=0";
42
    if ($order != '') {
43
        $sql .= " ORDER BY `$order`";
44
    }
45
    $result = $xoopsDB->query($sql);
46
    while (list($catid, $name) = $xoopsDB->fetchRow($result)) {
47
        $sitemap['parent'][$i]['id'] = $catid;
48
        $sitemap['parent'][$i]['title'] = $myts->htmlSpecialChars($name);
49
        if (oledrion_utils::getModuleOption('urlrewriting') == 1) { // On utilise l'url rewriting
50
            $url = 'category' . '-' . intval($catid) . oledrion_utils::makeSeoUrl($name) . '.html';
51
        } else { // Pas d'utilisation de l'url rewriting
52
            $url = 'category.php?cat_cid=' . intval($catid);
53
        }
54
        $sitemap['parent'][$i]['url'] = $url;
55
56
        if (@$sitemap_configs["show_subcategoris"]) {
57
            $j = 0;
58
            $child_ary = $mytree->getChildTreeArray($catid, $order);
59
            foreach ($child_ary as $child) {
60
                $count = strlen($child['prefix']) + 1;
61
                $sitemap['parent'][$i]['child'][$j]['id'] = $child[$id_name];
62
                $sitemap['parent'][$i]['child'][$j]['title'] = $myts->htmlSpecialChars($child[$title_name]);
63
                $sitemap['parent'][$i]['child'][$j]['image'] = (($count > 3) ? 4 : $count);
64
                if (oledrion_utils::getModuleOption('urlrewriting') == 1) { // On utilise l'url rewriting
65
                    $url = 'category' . '-' . intval($child[$id_name]) . oledrion_utils::makeSeoUrl($child[$title_name]) . '.html';
66
                } else { // Pas d'utilisation de l'url rewriting
67
                    $url = 'category.php?cat_cid=' . intval($child[$id_name]);
68
                }
69
                $sitemap['parent'][$i]['child'][$j]['url'] = $url;
70
71
                $j++;
72
            }
73
        }
74
        $i++;
75
    }
76
77
    return $sitemap;
78
}
79