Completed
Push — master ( 66731e...5478d6 )
by Michael
02:20
created

functions.php ➔ marquee_getmoduleoption()   D

Complexity

Conditions 10
Paths 6

Size

Total Lines 28
Code Lines 19

Duplication

Lines 15
Ratio 53.57 %

Importance

Changes 0
Metric Value
cc 10
eloc 19
nc 6
nop 2
dl 15
loc 28
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 : $Id:
20
 * ****************************************************************************
21
 */
22
23
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
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 = array();
44
    if (is_array($tbloptions) && array_key_exists($option, $tbloptions)) {
45
        return $tbloptions[$option];
46
    }
47
48
    $retval = false;
49 View Code Duplication
    if (null !== $xoopsModuleConfig && (is_object($xoopsModule) && $xoopsModule->getVar('dirname') == $repmodule && $xoopsModule->getVar('isactive'))) {
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...
50
        if (isset($xoopsModuleConfig[$option])) {
51
            $retval = $xoopsModuleConfig[$option];
52
        }
53
    } else {
54
        $moduleHandler = xoops_getHandler('module');
55
        $module         = $moduleHandler->getByDirname($repmodule);
56
        $configHandler = xoops_getHandler('config');
57
        if ($module) {
58
            $moduleConfig =& $configHandler->getConfigsByCat(0, $module->getVar('mid'));
59
            if (isset($moduleConfig[$option])) {
60
                $retval = $moduleConfig[$option];
61
            }
62
        }
63
    }
64
    $tbloptions[$option] = $retval;
65
66
    return $retval;
67
}
68
69
/**
70
 * Verify if the current "user" is a bot or not
71
 *
72
 * If you have a problem with this function, insert the folowing code just before the line if (isset($_SESSION['news_cache_bot'])) { :
73
 * return false;
74
 *
75
 * @package          Marquee
76
 * @author           Hervé Thouzard (http://www.herve-thouzard.com)
77
 * @copyright    (c) Hervé Thouzard
78
 */
79
function marquee_isbot()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Coding Style introduced by
marquee_isbot uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
80
{
81
    if (isset($_SESSION['marquee_cache_bot'])) {
82
        return $_SESSION['marquee_cache_bot'];
83
    } else {
84
        // Add here every bot you know separated by a pipe | (not matter with the upper or lower cases)
85
        // If you want to see the result for yourself, add your navigator's user agent at the end (mozilla for example)
86
        $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';
87
        $botlist      = strtoupper($botlist);
88
        $currentagent = strtoupper(xoops_getenv('HTTP_USER_AGENT'));
89
        $retval       = false;
90
        $botarray     = explode('|', $botlist);
91
        foreach ($botarray as $onebot) {
92
            if (false !== strpos($currentagent, $onebot)) {
93
                $retval = true;
94
                break;
95
            }
96
        }
97
    }
98
    $_SESSION['marquee_cache_bot'] = $retval;
99
100
    return $retval;
101
}
102
103
/**
104
 * Escape a string so that it can be included in a javascript string
105
 *
106
 * @param $string
107
 *
108
 * @return mixed
109
 */
110
function marquee_javascript_escape($string)
111
{
112
    return str_replace("'", "\\'", $string);
113
}
114