Completed
Push — master ( 171474...8d265e )
by Michael
01:31
created

category.php ➔ editRandomquoteCategory()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 30
Code Lines 23

Duplication

Lines 30
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 23
nc 6
nop 1
dl 30
loc 30
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
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
13
/**
14
 * Module: randomquote
15
 *
16
 * @category        Module
17
 * @package         randomquote
18
 * @author          XOOPS Development Team <[email protected]> - <https://xoops.org>
19
 * @copyright       {@link https://xoops.org/ XOOPS Project}
20
 * @license         GPL 2.0 or later
21
 * @link            https://xoops.org/
22
 * @since           1.0.0
23
 */
24
25
use Xoopsmodules\randomquote;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, randomquote.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
26
27
//require_once dirname(__DIR__) . '/class/utility.php';
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% 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...
28
/**
29
 * @param $options
30
 *
31
 * @return array
32
 */
33 View Code Duplication
function showRandomquoteCategory($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...
Coding Style introduced by
showRandomquoteCategory uses the super-global variable $GLOBALS 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...
34
{
35
    // require_once dirname(__DIR__) . '/class/category.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...
36
    $moduleDirName = basename(dirname(__DIR__));
0 ignored issues
show
Unused Code introduced by
$moduleDirName 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...
37
    //$myts = \MyTextSanitizer::getInstance();
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...
38
39
    $block         = [];
40
    $blockType     = $options[0];
41
    $categoryCount = $options[1];
42
    //$titleLenght = $options[2];
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
43
44
    /** @var XoopsObjectHandler $categoryHandler */
45
    $categoryHandler = new randomquote\CategoryHandler($GLOBALS['xoopsDB']);
46
    $criteria        = new CriteriaCompo();
47
    array_shift($options);
48
    array_shift($options);
49
    array_shift($options);
50
    if ($blockType) {
51
        $criteria->add(new Criteria('id', 0, '!='));
52
        $criteria->setSort('id');
53
        $criteria->setOrder('ASC');
54
    }
55
56
    $criteria->setLimit($categoryCount);
57
    $categoryArray = $categoryHandler->getAll($criteria);
58
    foreach (array_keys($categoryArray) as $i) {
0 ignored issues
show
Unused Code introduced by
This foreach statement is empty and can be removed.

This check looks for foreach loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
59
    }
60
61
    return $block;
62
}
63
64
/**
65
 * @param $options
66
 *
67
 * @return string
68
 */
69 View Code Duplication
function editRandomquoteCategory($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...
Coding Style introduced by
editRandomquoteCategory uses the super-global variable $GLOBALS 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...
70
{
71
    require_once dirname(__DIR__) . '/class/category.php';
72
    $moduleDirName = basename(dirname(__DIR__));
0 ignored issues
show
Unused Code introduced by
$moduleDirName 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...
73
74
    $form = MB_RANDOMQUOTE_DISPLAY;
75
    $form .= "<input type='hidden' name='options[0]' value='" . $options[0] . "' />";
76
    $form .= "<input name='options[1]' size='5' maxlength='255' value='" . $options[1] . "' type='text' />&nbsp;<br>";
77
    $form .= MB_RANDOMQUOTE_TITLELENGTH . " : <input name='options[2]' size='5' maxlength='255' value='" . $options[2] . "' type='text' /><br><br>";
78
79
    /** @var XoopsObjectHandler $'. category . 'Handler */
80
    $categoryHandler = new randomquote\CategoryHandler($GLOBALS['xoopsDB']); // xoops_getModuleHandler('category', $moduleDirName);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
81
    $criteria        = new CriteriaCompo();
82
    array_shift($options);
83
    array_shift($options);
84
    array_shift($options);
85
    $criteria->add(new Criteria('id', 0, '!='));
86
    $criteria->setSort('id');
87
    $criteria->setOrder('ASC');
88
    $categoryArray = $categoryHandler->getAll($criteria);
89
    $form          .= MB_RANDOMQUOTE_CATTODISPLAY . "<br><select name='options[]' multiple='multiple' size='5'>";
90
    $form          .= "<option value='0' " . (false === in_array(0, $options) ? '' : "selected='selected'") . '>' . MB_RANDOMQUOTE_ALLCAT . '</option>';
91
    foreach (array_keys($categoryArray) as $i) {
92
        $id   = $categoryArray[$i]->getVar('id');
93
        $form .= "<option value='" . $id . "' " . (false === in_array($id, $options) ? '' : "selected='selected'") . '>' . $categoryArray[$i]->getVar('title') . '</option>';
94
    }
95
    $form .= '</select>';
96
97
    return $form;
98
}
99