bookshop_recommended.php ➔ b_bookshop_recomm_show()   F
last analyzed

Complexity

Conditions 13
Paths 418

Size

Total Lines 99

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
nc 418
nop 1
dl 0
loc 99
rs 2.6284
c 0
b 0
f 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
/**
3
 * ****************************************************************************
4
 * bookshop - MODULE FOR XOOPS
5
 * Copyright (c) Hervé Thouzard of Instant Zero (http://www.instant-zero.com)
6
 * ****************************************************************************
7
 */
8
9
/**
10
 * Shows last recommended books
11
 *
12
 * @param $options
13
 *
14
 * @return array|bool
15
 */
16
function b_bookshop_recomm_show($options)
17
{
18
    // '10|0';  // See 10 books, for all categories or a particular category
19
    global $xoopsConfig, $xoopsTpl;
20
    include XOOPS_ROOT_PATH . '/modules/bookshop/include/common.php';
21
    $tblLivres = $tblCategories = $tblTmp = $tbl_tmp_vat = $tbl_vat = $tbl_tmp_lang = $block = $tbl_books_id = array();
0 ignored issues
show
Unused Code introduced by
$tblTmp 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...
Unused Code introduced by
$tblCategories 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...
Unused Code introduced by
$tblLivres 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...
22
    $tblLivres = $h_bookshop_books->getRecentRecommendedBooks(0, $options[0], $options[1]);
0 ignored issues
show
Bug introduced by
The variable $h_bookshop_books does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
23
    if ($h_bookshop_books->getRecommendedCount() > $options[0]) {    // Il y a plus de livres recommand�s dans la BDD que dans le bloc, on affiche donc un lien vers la page des livres recommand�s
24
        $block['showMore'] = true;
25
    }
26
27
    if (count($tblLivres) > 0) {
28
        $url                  = BOOKSHOP_URL . 'assets/css/bookshop.css';
29
        $block['nostock_msg'] = bookshop_getmoduleoption('nostock_msg');
30
        $xoopsTpl->assign('xoops_module_header', "<link rel=\"stylesheet\" type=\"text/css\" href=\"$url\" />");
31
        $tblTmp = array();
32
        foreach ($tblLivres as $item) {
33
            $tblTmp[]       = $item->getVar('book_cid');
34
            $tbl_tmp_vat[]  = $item->getVar('book_vat_id');
35
            $tbl_tmp_lang[] = $item->getVar('book_lang_id');
36
            $tbl_books_id[] = $item->getVar('book_id');
37
        }
38
        $tblTmp       = array_unique($tblTmp);
39
        $tbl_tmp_vat  = array_unique($tbl_tmp_vat);
40
        $tbl_tmp_lang = array_unique($tbl_tmp_lang);
41
42
        sort($tbl_tmp_lang);
43
        sort($tblTmp);
44
        sort($tbl_tmp_vat);
45
        sort($tbl_books_id);
46
47
        // Get languages *******************************************
48
        if (count($tbl_tmp_lang) > 0) {
49
            $tbl_lang = $h_bookshop_lang->getObjects(new Criteria('lang_id', '(' . implode(',', $tbl_tmp_lang) . ')', 'IN'), true);
0 ignored issues
show
Bug introduced by
The variable $h_bookshop_lang does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
50
        }
51
52
        // Get the list of categories ****************************
53
        $tblCategories = $h_bookshop_cat->getObjects(new Criteria('cat_cid', '(' . implode(',', $tblTmp) . ')', 'IN'), true);
0 ignored issues
show
Bug introduced by
The variable $h_bookshop_cat does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
54
55
        // Get VAT ***********************************************
56
        if (count($tbl_tmp_vat) > 0) {
57
            $tbl_vat = $h_bookshop_vat->getObjects(new Criteria('vat_id', '(' . implode(',', $tbl_tmp_vat) . ')', 'IN'), true);
0 ignored issues
show
Bug introduced by
The variable $h_bookshop_vat does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
58
        }
59
60
        // Get the authors, deuxi�me partie **************************
61
        $tbl_books_auteurs = array();
62
        $tbl_auteurs       = $h_bookshop_booksauthors->getObjects(new Criteria('ba_book_id', '(' . implode(',', $tbl_books_id) . ')', 'IN'), true);
0 ignored issues
show
Bug introduced by
The variable $h_bookshop_booksauthors does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
63
        if (count($tbl_auteurs) > 0) {
64
            foreach ($tbl_auteurs as $item) {
65
                $tbl_tmp_auteurs[] = $item->getVar('ba_auth_id');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$tbl_tmp_auteurs was never initialized. Although not strictly required by PHP, it is generally a good practice to add $tbl_tmp_auteurs = 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...
66
                // Grouping data by book
67
                $tbl_books_auteurs[$item->getVar('ba_book_id')][] = $item;
68
            }
69
            $tbl_tmp_auteurs = array_unique($tbl_tmp_auteurs);
0 ignored issues
show
Bug introduced by
The variable $tbl_tmp_auteurs does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
70
            sort($tbl_tmp_auteurs);
71
            // Then recovered the information from these authors / translators
72
            $tbl_infos_auteurs = $h_bookshop_authors->getObjects(new Criteria('auth_id', '(' . implode(',', $tbl_tmp_auteurs) . ')', 'IN'), true);
0 ignored issues
show
Bug introduced by
The variable $h_bookshop_authors does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
73
        }
74
        foreach ($tblLivres as $item) {
75
            $tbl_tmp                            = array();
0 ignored issues
show
Unused Code introduced by
$tbl_tmp 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...
76
            $tbl_tmp                            = $item->toArray();
77
            $tbl_tmp['book_category']           = $tblCategories[$item->getVar('book_cid')];
78
            $tbl_tmp['book_language']           = $tbl_lang[$item->getVar('book_lang_id')];
0 ignored issues
show
Bug introduced by
The variable $tbl_lang does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
79
            $tbl_tmp['book_vat_rate']           = $tbl_vat[$item->getVar('book_vat_id')];
80
            $tbl_tmp['book_price_ttc']          = bookshop_getTTC($item->getVar('book_price'), $tbl_vat[$item->getVar('book_vat_id')]->getVar('vat_rate'));
81
            $tbl_tmp['book_discount_price_ttc'] = bookshop_getTTC($item->getVar('book_discount_price'), $tbl_vat[$item->getVar('book_vat_id')]->getVar('vat_rate'));
82
83
            // Search for  authors / translators
84
            $tbl_join1         = $tbl_join2 = array();
85
            $tbl_tmp2          = $tbl_books_auteurs[$item->getVar('book_id')];    // Returns a list of all authors / translators of a book
86
            $tbl_livre_auteurs = $tbl_livre_traducteurs = array();
87
            foreach ($tbl_tmp2 as $oneauthor) {
88
                $auteur = $tbl_infos_auteurs[$oneauthor->getVar('ba_auth_id')];
0 ignored issues
show
Bug introduced by
The variable $tbl_infos_auteurs does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
89
                if ($oneauthor->getVar('ba_type') == 1) {
90
                    $tbl_livre_auteurs[] = $auteur->toArray();
91
                    $tbl_join1[]         = $auteur->getVar('auth_firstname') . ' ' . $auteur->getVar('auth_name');
92
                } else {
93
                    $tbl_livre_traducteurs[] = $auteur->toArray();
94
                    $tbl_join2[]             = $auteur->getVar('auth_firstname') . ' ' . $auteur->getVar('auth_name');
95
                }
96
            }
97
            if (count($tbl_join1) > 0) {
98
                $tbl_tmp['book_joined_authors'] = implode(', ', $tbl_join1);
99
            }
100
            if (count($tbl_join2) > 0) {
101
                $tbl_tmp['book_joined_translators'] = implode(',', $tbl_join2);
102
            }
103
            $tbl_tmp['book_authors']     = $tbl_livre_auteurs;
104
            $tbl_tmp['book_translators'] = $tbl_livre_traducteurs;
105
            // Et on place le tout dans le template
106
            $block['block_books'][] = $tbl_tmp;
107
        }
108
109
        return $block;
110
    } else {    // The list of books is not found (it is not books sold in the stock books)
111
112
        return false;
113
    }
114
}
115
116
/**
117
 * Block settings
118
 * @param $options
119
 * @return string
120
 */
121
function b_bookshop_recomm_edit($options)
122
{
123
    // '10|0';  // See 10 books, for all categories
124
    global $xoopsConfig;
125
    include XOOPS_ROOT_PATH . '/modules/bookshop/include/common.php';
126
    include_once BOOKSHOP_PATH . 'class/tree.php';
127
    $tblCategories = array();
0 ignored issues
show
Unused Code introduced by
$tblCategories 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...
128
    $tblCategories = $h_bookshop_cat->GetAllCategories();
0 ignored issues
show
Bug introduced by
The variable $h_bookshop_cat does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
129
    $mytree        = new Bookshop_XoopsObjectTree($tblCategories, 'cat_cid', 'cat_pid');
130
    $form          = '';
131
    $checkeds      = array('', '');
132
    if (!isset($options[1])) {
133
        $options[1] = 0;
134
    }
135
    $checkeds[$options[1]] = 'checked';
136
    $form .= "<table border='0'>";
137
    $form .= '<tr><td>' . _MB_BOOKSHOP_BOOKS_CNT . "</td><td><input type='text' name='options[]' id='options' value='" . $options[0] . "' /></td></tr>";
138
    //$form .= '<tr><td>'._MB_BOOKSHOP_SORT_ORDER."</td><td><input type='radio' name='options[]' id='options[]' value='0' ".$checkeds[0]." />"._MB_BOOKSHOP_SORT_1." <input type='radio' name='options[]' id='options[]' value='1' ".$checkeds[1]." />"._MB_BOOKSHOP_SORT_2.'</td></tr>';
139
    $select = $mytree->makeSelBox('options[]', 'cat_title', '-', $options[1], _MB_BOOKSHOP_ALL_CATEGORIES);
140
    $form .= '<tr><td>' . _MB_BOOKSHOP_CATEGORY . '</td><td>' . $select . '</td></tr>';
141
    $form .= '</table>';
142
143
    return $form;
144
}
145
146
/**
147
 * Ad hoc Block
148
 * @param $options
149
 */
150 View Code Duplication
function b_bookshop_recomm_duplicatable($options)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
151
{
152
    $options = explode('|', $options);
153
    $block   = &b_bookshop_recomm_show($options);
154
155
    $tpl = new XoopsTpl();
156
    $tpl->assign('block', $block);
157
    $tpl->display('db:bookshop_block_recommended.tpl');
158
}
159