Completed
Push — master ( 8786b6...8e35b4 )
by Michael
8s
created

RandomquoteQuotes::getForm()   C

Complexity

Conditions 10
Paths 128

Size

Total Lines 70
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
cc 10
eloc 46
c 3
b 1
f 1
nc 128
nop 1
dl 0
loc 70
rs 5.6296

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
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
 * Module: RandomQuote
13
 *
14
 * @category        Module
15
 * @package         randomquote
16
 * @author          XOOPS Module Development Team
17
 * @author          Mamba
18
 * @copyright       {@link http://xoops.org The XOOPS Project}
19
 * @license         {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
20
 * @link            http://xoops.org XOOPS
21
 * @since           2.00
22
 */
23
24
// defined('XOOPS_ROOT_PATH') || exit('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...
25
26
/**
27
 * Class RandomquoteQuotes
28
 */
29
class RandomquoteQuotes extends XoopsObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
30
{
31
    //Constructor
32
    /**
33
     *
34
     */
35
    public function __construct()
36
    {
37
        parent::__construct();
38
        $this->initVar('id', XOBJ_DTYPE_INT, null, false, 11);
39
        $this->initVar('quote', XOBJ_DTYPE_TXTAREA, null, true);
40
        $this->initVar('author', XOBJ_DTYPE_TXTAREA, null, false);
41
        $this->initVar('quote_status', XOBJ_DTYPE_INT, null, false, 10);
42
        $this->initVar('create_date', XOBJ_DTYPE_INT, time(), false, 11);
43
        $this->initVar('dohtml', XOBJ_DTYPE_INT, 1, false);
44
        $this->initVar('dosmiley', XOBJ_DTYPE_INT, 1, false);
45
        $this->initVar('doxcode', XOBJ_DTYPE_INT, 1, false);
46
        $this->initVar('doimage', XOBJ_DTYPE_INT, 1, false);
47
        $this->initVar('dobr', XOBJ_DTYPE_INT, 1, false);
48
    }
49
50
    /**
51
     *
52
     * Magic function to display obj as string
53
     */
54
    public function __toString()
55
    {
56
        return $this->getVar('quote') . ' -  ' . $this->getVar('author');
57
    }
58
    /**
59
     * Displays the Edit (Create) quote form
60
     *
61
     * @param mixed $action
62
     *
63
     * @return object {@see XoopsThemeForm)
64
     */
65
    public function getForm($action = false)
0 ignored issues
show
Coding Style introduced by
getForm 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...
66
    {
67
        if (false === $action) {
68
            $action = XoopsRequest::getString('REQUEST_URI','','SERVER');
69
        }
70
71
        $title = $this->isNew() ? sprintf(_AM_RANDOMQUOTE_QUOTES_ADD) : sprintf(_AM_RANDOMQUOTE_QUOTES_EDIT);
72
73
        xoops_load('constants', 'randomquote');
74
        include_once $GLOBALS['xoops']->path("/class/xoopsformloader.php");
75
76
        $form = new XoopsThemeForm($title, 'form', $action, 'post', true);
77
        $form->setExtra('enctype="multipart/form-data"');
78
79
        $author     = $this->isNew() ? '' : $this->getVar('author');
80
        $id         = ($this->getVar('id')) ? $this->getVar('id') : RandomquoteConstants::DEFAULT_ID;
81
        $textAuthor = new XoopsFormText(_AM_RANDOMQUOTE_QUOTES_AUTHOR, 'author', 50, 255, $author);
82
        $form->addElement($textAuthor);
83
84
        $editorConfigs = array('name' => 'quote',
85
                              'value' => $this->getVar('quote', 'e'),
86
                               'rows' => 10,
87
                               'cols' => 50,
88
                              'width' => '100%',
89
                             'height' => '400px',
90
                             'editor' => $GLOBALS['xoopsModuleConfig']['randomquote_editor']
91
        );
92
        $form->addElement(new XoopsFormEditor(_AM_RANDOMQUOTE_QUOTES_QUOTE, 'quote', $editorConfigs), true);
93
94
        /*
95
         * pseudo code
96
         * see if tag module is present & active
97
         * load the formtag class
98
         * display the tag form element to collect the tag item
99
         */
100
        $moduleHandler = xoops_gethandler('module');
0 ignored issues
show
Unused Code introduced by
$moduleHandler 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...
101
        $tagModule     = XoopsModule::getByDirname('tag');
102
        if (($tagModule instanceof XoopsModule) && ($tagModule->isactive())) {
0 ignored issues
show
Bug introduced by
The class XoopsModule does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
103
            $tagClassExists = XoopsLoad::load('formtag', 'tag');  // get the TagFormTag class
104
            if ($tagClassExists) {
105
                if ($this->isNew()) {
106
                    $tag_items = array();
107
                } else {
108
                    $moduleMid  = $GLOBALS['xoopsModule']->mid();
109
                    $tagHandler = xoops_getmodulehandler('tag', 'tag');
110
                    $tag_items  = $tagHandler->getByItem($id, $moduleMid, 0);
111
                }
112
                $tag_string = implode('|', $tag_items);
113
                $form->addElement(new TagFormTag('item_tag', 60, 255, $tag_string, 0));
114
            }
115
        } else {
116
            $form->addElement(new XoopsFormHidden('item_tag', ''));
117
        }
118
119
        $quote_status = ($this->isNew()) ? RandomquoteConstants::STATUS_ONLINE : $this->getVar('quote_status');
120
        $check_quote_status = new XoopsFormRadio(_AM_RANDOMQUOTE_QUOTES_STATUS, 'quote_status', $quote_status);
121
        $check_quote_status->addOption(RandomquoteConstants::STATUS_OFFLINE, _AM_RANDOMQUOTE_QUOTES_OFFLINE_TXT);
122
        $check_quote_status->addOption(RandomquoteConstants::STATUS_ONLINE, _AM_RANDOMQUOTE_QUOTES_ONLINE_TXT);
123
        $check_quote_status->addOption(RandomquoteConstants::STATUS_WAITING, _AM_RANDOMQUOTE_QUOTES_WAITING_TXT);
124
        $form->addElement($check_quote_status);
125
126
        $form->addElement(new XoopsFormHidden('op', 'save_quote'));
127
        $form->addElement(new XoopsFormHidden('id', $id));
128
129
        //Submit buttons
130
        $button_tray = new XoopsFormButtonTray('submit', _SUBMIT);
131
        $form->addElement($button_tray);
132
133
        return $form;
134
    }
135
}
136
137
/**
138
 * Class RandomquoteQuotesHandler
139
 */
140
class RandomquoteQuotesHandler extends XoopsPersistableObjectHandler
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
141
{
142
    /**
143
     * @param null|object $db
144
     */
145
    function __construct(&$db)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
146
    {
147
        parent::__construct($db, 'randomquote_quotes', 'RandomquoteQuotes', 'id', 'quote');
148
    }
149
}
150