b_marquee_catads()   B
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 55
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 42
c 0
b 0
f 0
nc 4
nop 3
dl 0
loc 55
rs 8.3146

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 declare(strict_types=1);
2
3
/**
4
 * ****************************************************************************
5
 * marquee - MODULE FOR XOOPS
6
 * Copyright (c) Hervé Thouzard (https://www.herve-thouzard.com)
7
 *
8
 * You may not change or alter any portion of this comment or credits
9
 * of supporting developers from this source code or any supporting source code
10
 * which is considered copyrighted (c) material of the original comment or credit authors.
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 *
15
 * @copyright         Hervé Thouzard (https://www.herve-thouzard.com)
16
 * @license           GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
17
 * @author            Hervé Thouzard (https://www.herve-thouzard.com)
18
 *
19
 * Version :
20
 * ****************************************************************************
21
 *
22
 * @param $limit
23
 * @param $dateFormat
24
 * @param $itemsSize
25
 *
26
 * @return array
27
 */
28
29
use XoopsModules\Catads;
0 ignored issues
show
Bug introduced by
The type XoopsModules\Catads was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
31
// Script to list recent ads from the catads module (tested with catads v 1.4)
32
/**
33
 * @param $limit
34
 * @param $dateFormat
35
 * @param $itemsSize
36
 * @return array
37
 */
38
function b_marquee_catads($limit, $dateFormat, $itemsSize)
39
{
40
    global $xoopsModule, $xoopsModuleConfig, $xoopsDB;
41
    //    require_once XOOPS_ROOT_PATH . '/modules/catads/class/cat.php';
42
    $block = [];
43
    if (empty($xoopsModule) || 'catads' !== $xoopsModule->getVar('dirname')) {
44
        /** @var \XoopsModuleHandler $moduleHandler */
45
        $moduleHandler = xoops_getHandler('module');
46
        $module        = $moduleHandler->getByDirname('catads');
47
        /** @var \XoopsConfigHandler $configHandler */
48
        $configHandler = xoops_getHandler('config');
49
        $config        = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
0 ignored issues
show
Unused Code introduced by
The assignment to $config is dead and can be removed.
Loading history...
50
    } else {
51
        $module = $xoopsModule;
0 ignored issues
show
Unused Code introduced by
The assignment to $module is dead and can be removed.
Loading history...
52
        $config = $xoopsModuleConfig;
53
    }
54
    //echo '<br>ok';
55
    $ads_hnd  = Catads\Helper::getInstance()->getHandler('Ads');
0 ignored issues
show
Bug introduced by
The type XoopsModules\Catads\Helper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
56
    $criteria = new \CriteriaCompo(new \Criteria('waiting', '0'));
57
    $criteria->add(new \Criteria('published', time(), '<'));
58
    $criteria->add(new \Criteria('expired', time(), '>'));
59
    $criteria->setSort('published');
60
    $criteria->setOrder('DESC');
61
    $criteria->setLimit($limit);
62
    $nbads     = $ads_hnd->getCount($criteria);
63
    $itemArray = [];
64
    $catBuffer = [];
65
    if ($nbads > 0) {
66
        $ads  = $ads_hnd->getObjects($criteria);
67
        $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
68
        foreach ($ads as $oneads) {
69
            if ($itemsSize > 0) {
70
                $title = xoops_substr($oneads->getVar('ads_title'), 0, $itemsSize);
71
            } else {
72
                $title = $oneads->getVar('ads_title');
73
            }
74
            if (!isset($catBuffer[$oneads->getVar('cat_id')])) {
75
                $tmpcat                               = new Catads\AdsCategory($oneads->getVar('cat_id'));
0 ignored issues
show
Bug introduced by
The type XoopsModules\Catads\AdsCategory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
76
                $catBuffer[$oneads->getVar('cat_id')] = $tmpcat->title();
77
                $catTitle                             = $tmpcat->title();
0 ignored issues
show
Unused Code introduced by
The assignment to $catTitle is dead and can be removed.
Loading history...
78
            } else {
79
                $catTitle = $catBuffer[$oneads->getVar('cat_id')];
80
            }
81
            $block[] = [
82
                'date'     => formatTimestamp($oneads->getVar('published'), $dateFormat),
83
                'category' => '',
84
                'author'   => \XoopsUser::getUnameFromId($oneads->getVar('uid')),
85
                'title'    => $title,
86
                'link'     => "<a href='" . XOOPS_URL . '/modules/catads/adsitem.php?ads_id=' . $oneads->getVar('ads_id') . "'>" . $title . '</a>',
87
            ];
88
            unset($itemArray);
89
        }
90
    }
91
92
    return $block;
93
}
94