Passed
Push — master ( dcbd4a...4bb7b0 )
by Michael
02:37
created

functions.php ➔ marquee_getmoduleoption()   D

Complexity

Conditions 10
Paths 6

Size

Total Lines 31
Code Lines 21

Duplication

Lines 31
Ratio 100 %

Importance

Changes 0
Metric Value
cc 10
eloc 21
nc 6
nop 2
dl 31
loc 31
rs 4.8196
c 0
b 0
f 0

How to fix   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
 * marquee - MODULE FOR XOOPS
5
 * Copyright (c) Hervé Thouzard (http://www.herve-thouzard.com)
6
 *
7
 * You may not change or alter any portion of this comment or credits
8
 * of supporting developers from this source code or any supporting source code
9
 * which is considered copyrighted (c) material of the original comment or credit authors.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 *
14
 * @copyright         Hervé Thouzard (http://www.herve-thouzard.com)
15
 * @license           http://www.fsf.org/copyleft/gpl.html GNU public license
16
 * @package           marquee
17
 * @author            Hervé Thouzard (http://www.herve-thouzard.com)
18
 *
19
 * Version :
20
 * ****************************************************************************
21
 */
22
23
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
24
25
/**
26
 * Returns a module's option
27
 *
28
 * Return's a module's option (for the news module)
29
 *
30
 * @package          Marquee
31
 * @author           Hervé Thouzard (http://www.herve-thouzard.com)
32
 * @copyright    (c) Hervé Thouzard
33
 *
34
 * @param string $option module option's name
35
 *
36
 * @param string $repmodule
37
 *
38
 * @return bool
39
 */
40
function marquee_getmoduleoption($option, $repmodule = 'marquee')
41
{
42
    global $xoopsModuleConfig, $xoopsModule;
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...
43
    static $tbloptions = [];
44
    if (is_array($tbloptions) && array_key_exists($option, $tbloptions)) {
45
        return $tbloptions[$option];
46
    }
47
48
    $retval = false;
49
    if (null !== $xoopsModuleConfig
50
        && (is_object($xoopsModule) && $xoopsModule->getVar('dirname') == $repmodule
51
            && $xoopsModule->getVar('isactive'))) {
52
        if (isset($xoopsModuleConfig[$option])) {
53
            $retval = $xoopsModuleConfig[$option];
54
        }
55
    } else {
56
        /** @var \XoopsModuleHandler $moduleHandler */
57
        $moduleHandler = xoops_getHandler('module');
0 ignored issues
show
Bug introduced by
The function xoops_getHandler was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
        $moduleHandler = /** @scrutinizer ignore-call */ xoops_getHandler('module');
Loading history...
58
        $module        = $moduleHandler->getByDirname($repmodule);
59
        /** @var \XoopsConfigHandler $configHandler */
60
        $configHandler = xoops_getHandler('config');
61
        if ($module) {
62
            $moduleConfig = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
63
            if (isset($moduleConfig[$option])) {
64
                $retval = $moduleConfig[$option];
65
            }
66
        }
67
    }
68
    $tbloptions[$option] = $retval;
69
70
    return $retval;
71
}
72
73
/**
74
 * Verify if the current "user" is a bot or not
75
 *
76
 * If you have a problem with this function, insert the folowing code just before the line if (isset($_SESSION['news_cache_bot'])) { :
77
 * return false;
78
 *
79
 * @package          Marquee
80
 * @author           Hervé Thouzard (http://www.herve-thouzard.com)
81
 * @copyright    (c) Hervé Thouzard
82
 */
83
function marquee_isbot()
84
{
85
    if (isset($_SESSION['marquee_cache_bot'])) {
86
        return $_SESSION['marquee_cache_bot'];
87
    } else {
88
        // Add here every bot you know separated by a pipe | (not matter with the upper or lower cases)
89
        // If you want to see the result for yourself, add your navigator's user agent at the end (mozilla for example)
90
        $botlist      = 'AbachoBOT|Arachnoidea|ASPSeek|Atomz|cosmos|crawl25-public.alexa.com|CrawlerBoy Pinpoint.com|Crawler|DeepIndex|EchO!|exabot|Excalibur Internet Spider|FAST-WebCrawler|Fluffy the spider|GAIS Robot/1.0B2|GaisLab data gatherer|Google|Googlebot-Image|googlebot|Gulliver|ia_archiver|Infoseek|Links2Go|Lycos_Spider_(modspider)|Lycos_Spider_(T-Rex)|MantraAgent|Mata Hari|Mercator|MicrosoftPrototypeCrawler|[email protected]|MSNBOT|NEC Research Agent|NetMechanic|Nokia-WAPToolkit|nttdirectory_robot|Openfind|Oracle Ultra Search|PicoSearch|Pompos|Scooter|Slider_Search_v1-de|Slurp|Slurp.so|SlySearch|Spider|Spinne|SurferF3|Surfnomore Spider|suzuran|teomaagent1|TurnitinBot|Ultraseek|VoilaBot|vspider|W3C_Validator|Web Link Validator|WebTrends|WebZIP|whatUseek_winona|WISEbot|Xenu Link Sleuth|ZyBorg';
91
        $botlist      = strtoupper($botlist);
92
        $currentagent = strtoupper(xoops_getenv('HTTP_USER_AGENT'));
0 ignored issues
show
Bug introduced by
The function xoops_getenv was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
        $currentagent = strtoupper(/** @scrutinizer ignore-call */ xoops_getenv('HTTP_USER_AGENT'));
Loading history...
93
        $retval       = false;
94
        $botarray     = explode('|', $botlist);
95
        foreach ($botarray as $onebot) {
96
            if (false !== strpos($currentagent, $onebot)) {
97
                $retval = true;
98
                break;
99
            }
100
        }
101
    }
102
    $_SESSION['marquee_cache_bot'] = $retval;
103
104
    return $retval;
105
}
106
107
/**
108
 * Escape a string so that it can be included in a javascript string
109
 *
110
 * @param $string
111
 *
112
 * @return mixed
113
 */
114
function marquee_javascript_escape($string)
115
{
116
    return str_replace("'", "\\'", $string);
117
}
118