Completed
Pull Request — master (#16)
by Michael
01:51
created

news_archives.php ➔ b_news_archives_edit()   F

Complexity

Conditions 16
Paths 2592

Size

Total Lines 81
Code Lines 56

Duplication

Lines 16
Ratio 19.75 %

Importance

Changes 0
Metric Value
cc 16
eloc 56
nc 2592
nop 1
dl 16
loc 81
rs 2.0759
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
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 35 and the first side effect is on line 20.

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
 * 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
 * @copyright      {@link https://xoops.org/ XOOPS Project}
14
 * @license        {@link http://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
15
 * @package
16
 * @since
17
 * @author         XOOPS Development Team
18
 */
19
20
require_once XOOPS_ROOT_PATH . '/modules/news/class/class.newsstory.php';
21
22
/**
23
 * Display archives
24
 *
25
 * @param array $options :
26
 *                       0 = sort order (0=older first, 1=newer first)
27
 *                       1 = Starting date, year
28
 *                       2 = Starting date, month
29
 *                       3 = Ending date, year
30
 *                       4 = Ending date, month
31
 *                       5 = until today ?
32
 *
33
 * @return array|string
34
 */
35
function b_news_archives_show($options)
36
{
37
    global $xoopsDB, $xoopsConfig;
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...
38
    require_once XOOPS_ROOT_PATH . '/modules/news/class/class.newsstory.php';
39
    require_once XOOPS_ROOT_PATH . '/modules/news/class/utility.php';
40
    require_once XOOPS_ROOT_PATH . '/language/' . $xoopsConfig['language'] . '/calendar.php';
41 View Code Duplication
    if (file_exists(XOOPS_ROOT_PATH . '/modules/news/language/' . $xoopsConfig['language'] . '/main.php')) {
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...
42
        require_once XOOPS_ROOT_PATH . '/modules/news/language/' . $xoopsConfig['language'] . '/main.php';
43
    } else {
44
        require_once XOOPS_ROOT_PATH . '/modules/news/language/english/main.php';
45
    }
46
47
    $months_arr    = [
48
        1  => _CAL_JANUARY,
49
        2  => _CAL_FEBRUARY,
50
        3  => _CAL_MARCH,
51
        4  => _CAL_APRIL,
52
        5  => _CAL_MAY,
53
        6  => _CAL_JUNE,
54
        7  => _CAL_JULY,
55
        8  => _CAL_AUGUST,
56
        9  => _CAL_SEPTEMBER,
57
        10 => _CAL_OCTOBER,
58
        11 => _CAL_NOVEMBER,
59
        12 => _CAL_DECEMBER
60
    ];
61
    $block         = [];
62
    $sort_order    = 0 == $options[0] ? 'ASC' : 'DESC';
63
    $starting_date = mktime(0, 0, 0, (int)$options[2], 1, (int)$options[1]);
64
    if (1 != (int)$options[5]) {
65
        $ending_date = mktime(23, 59, 59, (int)$options[4], 28, (int)$options[3]);
66
    } else {
67
        $ending_date = time();
68
    }
69
    $sql    = "SELECT DISTINCT(FROM_UNIXTIME(published,'%Y-%m')) AS published FROM " . $xoopsDB->prefix('news_stories') . ' WHERE published>=' . $starting_date . ' AND published<=' . $ending_date . ' ORDER BY published ' . $sort_order;
70
    $result = $xoopsDB->query($sql);
71
    if (!$result) {
72
        return '';
73
    }
74
    while ($myrow = $xoopsDB->fetchArray($result)) {
75
        $year                = (int)substr($myrow['published'], 0, 4);
76
        $month               = (int)substr($myrow['published'], 5, 2);
77
        $formated_month      = $months_arr[$month];
78
        $block['archives'][] = ['month' => $month, 'year' => $year, 'formated_month' => $formated_month];
79
    }
80
81
    return $block;
82
}
83
84
/**
85
 * @param $options
86
 *
87
 * @return string
88
 */
89
function b_news_archives_edit($options)
90
{
91
    global $xoopsDB;
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...
92
    $syear    = $smonth = $eyear = $emonth = $older = $recent = 0;
0 ignored issues
show
Unused Code introduced by
$emonth 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
$eyear 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
$smonth 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
$syear 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...
93
    $selsyear = $selsmonth = $seleyear = $selemonth = 0;
0 ignored issues
show
Unused Code introduced by
$selemonth 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
$seleyear 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
$selsmonth 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
$selsyear 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...
94
    $form     = '';
95
96
    $selsyear  = $options[1];
97
    $selsmonth = $options[2];
98
    $seleyear  = $options[3];
99
    $selemonth = $options[4];
100
101
    $tmpstory = new NewsStory;
102
    $tmpstory->getOlderRecentNews($older, $recent); // We are searching for the module's older and more recent article's date
103
104
    // Min and max value for the two dates selectors
105
    // We are going to use the older news for the starting date
106
    $syear  = date('Y', $older);
107
    $smonth = date('n', $older);
108
    $eyear  = date('Y', $recent);
109
    $emonth = date('n', $recent);
110
    // Verify parameters
111
    if (0 == $selsyear && 0 == $selsmonth) {
112
        $selsyear  = $syear;
113
        $selsmonth = $smonth;
114
    }
115
    if (0 == $seleyear && 0 == $selemonth) {
116
        $seleyear  = $eyear;
117
        $selemonth = $emonth;
118
    }
119
120
    // Sort order *************************************************************
121
    // (0=older first, 1=newer first)
122
    $form .= '<b>' . _MB_NEWS_ORDER . "</b>&nbsp;<select name='options[]'>";
123
    $form .= "<option value='0'";
124
    if (0 == $options[0]) {
125
        $form .= ' selected';
126
    }
127
    $form .= '>' . _MB_NEWS_OLDER_FIRST . "</option>\n";
128
    $form .= "<option value='1'";
129
    if (1 == $options[0]) {
130
        $form .= ' selected';
131
    }
132
    $form .= '>' . _MB_NEWS_RECENT_FIRST . '</option>';
133
    $form .= "</select>\n";
134
135
    // Starting and ending dates **********************************************
136
    $form .= '<br><br><b>' . _MB_NEWS_STARTING_DATE . '</b><br>';
137
    $form .= _MB_NEWS_CAL_YEAR . "&nbsp;<select name='options[]'>";
138 View Code Duplication
    for ($i = $syear; $i <= $eyear; ++$i) {
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...
139
        $selected = ($i == $selsyear) ? 'selected' : '';
140
        $form     .= "<option value='" . $i . "'" . $selected . '>' . $i . '</option>';
141
    }
142
    $form .= '</select>&nbsp;' . _MB_NEWS_CAL_MONTH . "&nbsp;<select name='options[]'>";
143 View Code Duplication
    for ($i = 1; $i <= 12; ++$i) {
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...
144
        $selected = ($i == $selsmonth) ? 'selected' : '';
145
        $form     .= "<option value='" . $i . "'" . $selected . '>' . $i . '</option>';
146
    }
147
    $form .= '</select>';
148
149
    $form .= '<br><br><b>' . _MB_NEWS_ENDING_DATE . '</b><br>';
150
    $form .= _MB_NEWS_CAL_YEAR . "&nbsp;<select name='options[]'>";
151 View Code Duplication
    for ($i = $syear; $i <= $eyear; ++$i) {
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...
152
        $selected = ($i == $seleyear) ? 'selected' : '';
153
        $form     .= "<option value='" . $i . "'" . $selected . '>' . $i . '</option>';
154
    }
155
    $form .= '</select>&nbsp;' . _MB_NEWS_CAL_MONTH . "&nbsp;<select name='options[]'>";
156 View Code Duplication
    for ($i = 1; $i <= 12; ++$i) {
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...
157
        $selected = ($i == $selemonth) ? 'selected' : '';
158
        $form     .= "<option value='" . $i . "'" . $selected . '>' . $i . '</option>';
159
    }
160
    $form .= '</select>';
161
162
    // Or until today *********************************************************
163
    $form    .= '<br>';
164
    $checked = 1 == $options[5] ? ' checked' : '';
165
    $form    .= "<input type='checkbox' value='1' name='options[]'" . $checked . '>';
166
    $form    .= ' <b>' . _MB_NEWS_UNTIL_TODAY . '</b>';
167
168
    return $form;
169
}
170
171
/**
172
 * @param $options
173
 */
174 View Code Duplication
function b_news_archives_onthefly($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...
175
{
176
    $options = explode('|', $options);
177
    $block   = &b_news_archives_show($options);
178
179
    $tpl = new XoopsTpl();
180
    $tpl->assign('block', $block);
181
    $tpl->display('db:news_block_archives.tpl');
182
}
183