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

QuotesForm::__construct()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 56
Code Lines 35

Duplication

Lines 18
Ratio 32.14 %

Importance

Changes 0
Metric Value
cc 4
eloc 35
nc 6
nop 1
dl 18
loc 56
rs 9.0544
c 0
b 0
f 0

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 namespace Xoopsmodules\randomquote\form;
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 38 and the first side effect is on line 27.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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
 * Module: randomquote
14
 *
15
 * @category        Module
16
 * @package         randomquote
17
 * @author          XOOPS Development Team <[email protected]> - <https://xoops.org>
18
 * @copyright       {@link https://xoops.org/ XOOPS Project}
19
 * @license         GPL 2.0 or later
20
 * @link            https://xoops.org/
21
 * @since           1.0.0
22
 */
23
24
use Xmf\Request;
25
use Xoopsmodules\randomquote;
26
27
require_once __DIR__ . '/../../include/common.php';
28
29
$moduleDirName = basename(dirname(dirname(__DIR__)));
30
$helper        = randomquote\Helper::getInstance();
31
$permHelper    = new \Xmf\Module\Helper\Permission();
32
33
xoops_load('XoopsFormLoader');
34
35
/**
36
 * Class QuotesForm
37
 */
38
class QuotesForm extends \XoopsThemeForm
39
{
40
    public $targetObject;
41
42
    /**
43
     * Constructor
44
     *
45
     * @param $target
46
     */
47
    public function __construct($target)
48
    {
49
        global $helper;
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...
50
        $this->targetObject = $target;
51
52
        $title = $this->targetObject->isNew() ? sprintf(AM_RANDOMQUOTE_QUOTES_ADD) : sprintf(AM_RANDOMQUOTE_QUOTES_EDIT);
53
        parent::__construct($title, 'form', xoops_getenv('PHP_SELF'), 'post', true);
54
        $this->setExtra('enctype="multipart/form-data"');
55
56
        //include ID field, it's needed so the module knows if it is a new form or an edited form
57
58
        $hidden = new \XoopsFormHidden('id', $this->targetObject->getVar('id'));
59
        $this->addElement($hidden);
60
        unset($hidden);
61
62
        // Id
63
        $this->addElement(new \XoopsFormLabel(AM_RANDOMQUOTE_QUOTES_ID, $this->targetObject->getVar('id'), 'id'));
64
        // Quote
65 View Code Duplication
        if (class_exists('XoopsFormEditor')) {
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...
66
            $editorOptions           = [];
67
            $editorOptions['name']   = 'quote';
68
            $editorOptions['value']  = $this->targetObject->getVar('quote', 'e');
69
            $editorOptions['rows']   = 5;
70
            $editorOptions['cols']   = 40;
71
            $editorOptions['width']  = '100%';
72
            $editorOptions['height'] = '400px';
73
            //$editorOptions['editor'] = xoops_getModuleOption('randomquote_editor', 'randomquote');
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...
74
            //$this->addElement( new \XoopsFormEditor(AM_RANDOMQUOTE_QUOTES_QUOTE, 'quote', $editorOptions), false  );
0 ignored issues
show
Unused Code Comprehensibility introduced by
61% 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...
75
            if ($helper->isUserAdmin()) {
76
                $descEditor = new \XoopsFormEditor(AM_RANDOMQUOTE_QUOTES_QUOTE, $helper->getConfig('randomquoteEditorAdmin'), $editorOptions, $nohtml = false, $onfailure = 'textarea');
77
            } else {
78
                $descEditor = new \XoopsFormEditor(AM_RANDOMQUOTE_QUOTES_QUOTE, $helper->getConfig('randomquoteEditorUser'), $editorOptions, $nohtml = false, $onfailure = 'textarea');
79
            }
80
        } else {
81
            $descEditor = new \XoopsFormDhtmlTextArea(AM_RANDOMQUOTE_QUOTES_QUOTE, 'description', $this->targetObject->getVar('description', 'e'), '100%', '100%');
82
        }
83
        $this->addElement($descEditor);
84
        // Author
85
        $this->addElement(new \XoopsFormText(AM_RANDOMQUOTE_QUOTES_AUTHOR, 'author', 50, 255, $this->targetObject->getVar('author')), false);
86
        // Online
87
        $this->addElement(new \XoopsFormText(AM_RANDOMQUOTE_QUOTES_ONLINE, 'online', 50, 255, $this->targetObject->getVar('online')), false);
88
        // Created
89
        $this->addElement(new \XoopsFormTextDateSelect(AM_RANDOMQUOTE_QUOTES_CREATED, 'created', '', strtotime($this->targetObject->getVar('created'))));
90
        // Cid
91
        //$categoryHandler = xoops_getModuleHandler('category', 'randomquote');        
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
92
        $db = \XoopsDatabaseFactory::getDatabaseConnection();
93
        /** @var \XoopsPersistableObjectHandler $categoryHandler */
94
        $categoryHandler = new randomquote\CategoryHandler($db);
95
96
        $category_id_select = new \XoopsFormSelect(AM_RANDOMQUOTE_QUOTES_CID, 'cid', $this->targetObject->getVar('cid'));
97
        $category_id_select->addOptionArray($categoryHandler->getList());
98
        $this->addElement($category_id_select, false);
99
100
        $this->addElement(new \XoopsFormHidden('op', 'save'));
101
        $this->addElement(new \XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
102
    }
103
}
104