Completed
Branch master (55a138)
by Michael
03:21
created

TableForm   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 124
Duplicated Lines 9.68 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 12
loc 124
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setAddBaliseTable() 0 4 1
A getAddBaliseTable() 0 4 1
A insertBreak() 0 12 3
C render() 12 55 13

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace XoopsModules\Extcal;
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 14 and the first side effect is on line 7.

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
5
// defined('XOOPS_ROOT_PATH') || die('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...
6
7
xoops_load('XoopsForm');
8
9
/**
10
 * Form that will output as a theme-enabled HTML table.
11
 *
12
 * Also adds JavaScript to validate required fields
13
 */
14
class TableForm extends \XoopsForm
15
{
16
    /**
17
     * ad the balise html "table" to render.
18
     *
19
     * @var bool|string
20
     */
21
    public $_addBaliseTable = '';
22
23
    /**
24
     * Gets the "value" attribute of a form element.
25
     *
26
     * @param $addBaliseTable
27
     *
28
     * @internal param string $name the "name" attribute of a form element
29
     * @internal param bool $encode To sanitizer the text?
30
     *
31
     * @return string the "value" attribute assigned to a form element, null if not set
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
32
     */
33
    public function setAddBaliseTable($addBaliseTable)
34
    {
35
        $this->_addBaliseTable = $addBaliseTable;
36
    }
37
38
    /**
39
     * gets the "value" attribute of all form elements.
40
     *
41
     * @internal param bool $encode To sanitizer the text?
42
     *
43
     * @return array array of name/value pairs assigned to form elements
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
44
     */
45
    public function getAddBaliseTable()
46
    {
47
        return $this->_addBaliseTable;
48
    }
49
50
    /**
51
     * Insert an empty row in the table to serve as a seperator.
52
     *
53
     * @param string $extra HTML to be displayed in the empty row.
54
     * @param string $class CSS class name for <td> tag
55
     */
56
    public function insertBreak($extra = '', $class = '')
57
    {
58
        $class = ('' != $class) ? " class='" . preg_replace('/[^A-Za-z0-9\s\s_-]/i', '', $class) . "'" : '';
59
        // Fix for $extra tag not showing
60
        if ($extra) {
61
            $extra = '<tr><td colspan="2" ' . $class . '>' . $extra . '</td></tr>';
62
            $this->addElement($extra);
63
        } else {
64
            $extra = '<tr><td colspan="2" ' . $class . '>&nbsp;</td></tr>';
65
            $this->addElement($extra);
66
        }
67
    }
68
69
    /**
70
     * create HTML to output the form as a theme-enabled table with validation.
71
     *
72
     * YOU SHOULD AVOID TO USE THE FOLLOWING Nocolspan METHOD, IT WILL BE REMOVED
73
     *
74
     * To use the noColspan simply use the following example:
75
     *
76
     * $colspan = new \XoopsFormDhtmlTextArea( '', 'key', $value, '100%', '100%' );
77
     * $colspan->setNocolspan();
78
     * $form->addElement( $colspan );
79
     *
80
     * @return string
81
     */
82
    public function render()
83
    {
84
        $addBaliseTable = $this->_addBaliseTable;
85
        $title          = $this->getTitle();
86
        $ret            = '';
87
88
        if ($addBaliseTable) {
89
            $ret .= '<table width="100%" class="outer" cellspacing="1"> ';
90
        }
91
92
        if ('' != $title) {
93
            $ret .= '<tr><th colspan="2">' . $title . '</th></tr>';
94
        }
95
96
        $hidden = '';
97
        $class  = 'even';
98
        foreach ($this->getElements() as $ele) {
99
            if (!is_object($ele)) {
100
                $ret .= $ele;
101
            } elseif (!$ele->isHidden()) {
102
                if (!$ele->getNocolspan()) {
103
                    $ret .= '<tr valign="top" align="left"><td class="head">';
104 View Code Duplication
                    if ('' != ($caption = $ele->getCaption())) {
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...
105
                        $ret .= '<div class="xoops-form-element-caption' . ($ele->isRequired() ? '-required' : '') . '">';
106
                        $ret .= '<span class="caption-text">' . $caption . '</span>';
107
                        $ret .= '<span class="caption-marker">*</span>';
108
                        $ret .= '</div>';
109
                    }
110
                    if ('' != ($desc = $ele->getDescription())) {
111
                        $ret .= '<div class="xoops-form-element-help">' . $desc . '</div>';
112
                    }
113
                    $ret .= '</td><td class="' . $class . '">' . $ele->render() . '</td></tr>' . NWLINE;
114
                } else {
115
                    $ret .= '<tr valign="top" align="left"><td class="head" colspan="2">';
116 View Code Duplication
                    if ('' != ($caption = $ele->getCaption())) {
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...
117
                        $ret .= '<div class="xoops-form-element-caption' . ($ele->isRequired() ? '-required' : '') . '">';
118
                        $ret .= '<span class="caption-text">' . $caption . '</span>';
119
                        $ret .= '<span class="caption-marker">*</span>';
120
                        $ret .= '</div>';
121
                    }
122
                    $ret .= '</td></tr><tr valign="top" align="left"><td class="' . $class . '" colspan="2">' . $ele->render() . '</td></tr>';
123
                }
124
            } else {
125
                $hidden .= $ele->render();
126
            }
127
        }
128
129
        if ($addBaliseTable) {
130
            $ret .= '</table>';
131
        }
132
133
        $ret .= NWLINE . ' ' . $hidden . NWLINE;
134
135
        return $ret;
136
    }
137
} // fin de la classe
138