Completed
Push — master ( 097ad2...6ae33d )
by Michael
12s
created

blocks.php ➔ about_block_menu_show()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 20
nc 2
nop 0
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 25 and the first side effect is on line 21.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * About
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright      The XOOPS Co.Ltd. http://www.xoops.com.cn
13
 * @copyright      XOOPS Project (https://xoops.org)
14
 * @license        GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
15
 * @package        about
16
 * @since          1.0.0
17
 * @author         Mengjue Shao <[email protected]>
18
 * @author         Susheng Yang <[email protected]>
19
 */
20
21
defined('XOOPS_ROOT_PATH') || exit('Restricted access.');
22
/**
23
 * @return mixed
24
 */
25
function about_block_menu_show()
26
{
27
    $moduleDirName = basename(dirname(__DIR__));
28
    xoops_load('constants', $moduleDirName);
29
30
    $abtHelper     = Xmf\Module\Helper::getHelper($moduleDirName);
31
    $pageHandler  = $abtHelper->getHandler('page');
32
    $menu_criteria = new CriteriaCompo();
33
    $menu_criteria->add(new Criteria('page_status', AboutConstants::PUBLISHED), 'AND');
34
    $menu_criteria->add(new Criteria('page_menu_status', AboutConstants::IN_MENU));
35
    $menu_criteria->setSort('page_order');
36
    $menu_criteria->order = 'ASC';
37
    $fields    = array(
38
        'page_id',
39
        'page_menu_title',
40
        'page_blank',
41
        'page_menu_status',
42
        'page_status'
43
    );
44
    $page_menu = $pageHandler->getAll($menu_criteria, $fields, false);
45
    foreach ($page_menu as $k => $v) {
46
        $page_menu[$k]['links'] = $abtHelper->url("index.php?page_id={$v['page_id']}");
47
    }
48
49
    return $page_menu;
50
}
51
52
/**
53
 * @param $options
54
 * @return array|bool
55
 */
56
function about_block_page_show($options)
57
{
58
    if (empty($options[0])) {
59
        return false;
60
    }
61
    $moduleDirName = basename(dirname(__DIR__));
62
    $abtHelper     = Xmf\Module\Helper::getHelper($moduleDirName);
63
64
    $myts        = MyTextSanitizer::getInstance();
65
    $block       = array();
66
    $pageHandler = $abtHelper->getHandler('page');
67
    $page        = $pageHandler->get($options[0]);
68
    if (!is_object($page)) {
69
        return false;
70
    }
71
    $page_text = strip_tags($page->getVar('page_text', 'n'));
72
    if ($options[1] > 0) {
73
        $url        = $abtHelper->url("index.php?page_id={$options[0]}");
74
        $trimmarker = <<<EOF
75
<a href="{$url}" class="more">{$options[2]}</a>
76
EOF;
77
        $page_text  = xoops_substr($page_text, 0, $options[1]) . $trimmarker;
78
    }
79
80
    $block['page_text']  = $myts->nl2br($page_text);
81
    $block['page_image'] = $options[3] == 1 ? $abtHelper->url($page->getVar('page_image', 's')) : '';
82
83
    return $block;
84
}
85
86
/**
87
 * @param $options
88
 * @return string
89
 */
90
function about_block_page_edit($options)
91
{
92
    $moduleDirName = basename(dirname(__DIR__));
93
    $abtHelper     = Xmf\Module\Helper::getHelper($moduleDirName);
94
    xoops_load('constants', $moduleDirName);
95
96
    $abtHelper->loadLanguage('blocks');
97
    $pageHandler = $abtHelper->getHandler('page');
98
    $criteria    = new CriteriaCompo();
99
    $criteria->add(new Criteria('page_status', AboutConstants::PUBLISHED), 'AND');
100
    $criteria->add(new Criteria('page_type', AboutConstants::PAGE_TYPE_PAGE));
101
    $criteria->setSort('page_order');
102
    $criteria->order = 'ASC';
103
    $fields     = array('page_id', 'page_title', 'page_image');
104
    $pages      = $pageHandler->getAll($criteria, $fields, false);
105
    $page_title = '';
0 ignored issues
show
Unused Code introduced by
$page_title 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
    foreach ($pages as $k => $v) {
107
        $page_title       = '<a href="' . $abtHelper->url("index.php?page_id={$k}") . '" target="_blank">' . $v['page_title'] . '</a>';
108
        $options_page[$k] = empty($v['page_image']) ? $page_title : $page_title . '<img src="' . $abtHelper->url('assets/images/picture.png') . '">';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$options_page was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options_page = 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...
109
    }
110
//    include_once dirname(__DIR__) . '/include/xoopsformloader.php';
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
111
    xoops_load('blockform', $moduleDirName);
112
    $form        = new AboutBlockForm();
113
    $page_select = new XoopsFormRadio(_MB_ABOUT_BLOCKPAGE, 'options[0]', $options[0], '<br>');
114
    $page_select->addOptionArray($options_page);
0 ignored issues
show
Bug introduced by
The variable $options_page 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...
115
    $form->addElement($page_select);
116
    $form->addElement(new XoopsFormText(_MB_ABOUT_TEXT_LENGTH, 'options[1]', 5, 5, $options[1]));
117
    $form->addElement(new XoopsFormText(_MB_ABOUT_VIEW_MORELINKTEXT, 'options[2]', 30, 50, $options[2]));
118
    $form->addElement(new XoopsFormRadioYN(_MB_ABOUT_DOTITLEIMAGE, 'options[3]', $options[3]));
119
120
    return $form->render();
121
}
122