Completed
Push — master ( 8d9a9b...cd572c )
by Richard
11s
created

XoopsFormElementTray::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
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 25 and the first side effect is on line 20.

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
 * XOOPS form element
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
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
 * @copyright       (c) 2000-2017 XOOPS Project (www.xoops.org)
13
 * @license             GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package             kernel
15
 * @subpackage          form
16
 * @since               2.0.0
17
 * @author              Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
18
 */
19
20
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
21
22
/**
23
 * A group of form elements
24
 */
25
class XoopsFormElementTray extends XoopsFormElement
26
{
27
    /**
28
     * array of form element objects
29
     *
30
     * @var array
31
     * @access private
32
     */
33
    private $_elements = array();
34
35
    /**
36
     * required elements
37
     *
38
     * @var array
39
     */
40
    public $_required = array();
41
42
    /**
43
     * HTML to seperate the elements
44
     *
45
     * @var string
46
     * @access private
47
     */
48
    private $_delimeter;
49
50
    /**
51
     * constructor
52
     *
53
     * @param string $caption   Caption for the group.
54
     * @param string $delimeter HTML to separate the elements
55
     * @param string $name
56
     *
57
     */
58
    public function __construct($caption, $delimeter = '&nbsp;', $name = '')
59
    {
60
        $this->setName($name);
61
        $this->setCaption($caption);
62
        $this->_delimeter = $delimeter;
63
    }
64
65
    /**
66
     * Is this element a container of other elements?
67
     *
68
     * @return bool true
69
     */
70
    public function isContainer()
71
    {
72
        return true;
73
    }
74
75
    /**
76
     * Find out if there are required elements.
77
     *
78
     * @return bool
79
     */
80
    public function isRequired()
81
    {
82
        return !empty($this->_required);
83
    }
84
85
    /**
86
     * Add an element to the group
87
     *
88
     * @param XoopsFormElement $formElement {@link XoopsFormElement} to add
89
     * @param bool             $required
90
     *
91
     */
92
    public function addElement(XoopsFormElement $formElement, $required = false)
93
    {
94
        $this->_elements[] = $formElement;
95 View Code Duplication
        if (!$formElement->isContainer()) {
96
            if ($required) {
97
                $formElement->_required = true;
98
                $this->_required[]      = $formElement;
99
            }
100
        } else {
101
            $required_elements = $formElement->getRequired();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class XoopsFormElement as the method getRequired() does only exist in the following sub-classes of XoopsFormElement: XoopsFormDateTime, XoopsFormElementTray, XoopsFormSelectEditor, XoopsFormSelectUser. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
102
            $count             = count($required_elements);
103
            for ($i = 0; $i < $count; ++$i) {
104
                $this->_required[] = &$required_elements[$i];
105
            }
106
        }
107
    }
108
109
    /**
110
     * get an array of "required" form elements
111
     *
112
     * @return array array of {@link XoopsFormElement}s
113
     */
114
    public function &getRequired()
115
    {
116
        return $this->_required;
117
    }
118
119
    /**
120
     * Get an array of the elements in this group
121
     *
122
     * @param  bool $recurse get elements recursively?
123
     * @return array Array of {@link XoopsFormElement} objects.
124
     */
125 View Code Duplication
    public function &getElements($recurse = false)
126
    {
127
        if (!$recurse) {
128
            return $this->_elements;
129
        } else {
130
            $ret   = array();
131
            $count = count($this->_elements);
132
            for ($i = 0; $i < $count; ++$i) {
133
                if (!$this->_elements[$i]->isContainer()) {
134
                    $ret[] = &$this->_elements[$i];
135
                } else {
136
                    $elements = &$this->_elements[$i]->getElements(true);
137
                    $count2   = count($elements);
138
                    for ($j = 0; $j < $count2; ++$j) {
139
                        $ret[] = &$elements[$j];
140
                    }
141
                    unset($elements);
142
                }
143
            }
144
145
            return $ret;
146
        }
147
    }
148
149
    /**
150
     * Get the delimiter of this group
151
     *
152
     * @param  bool $encode To sanitizer the text?
153
     * @return string The delimiter
154
     */
155
    public function getDelimeter($encode = false)
156
    {
157
        return $encode ? htmlspecialchars(str_replace('&nbsp;', ' ', $this->_delimeter)) : $this->_delimeter;
158
    }
159
160
    /**
161
     * prepare HTML to output this group
162
     *
163
     * @return string HTML output
164
     */
165
    public function render()
166
    {
167
        return XoopsFormRenderer::getInstance()->get()->renderFormElementTray($this);
168
    }
169
}
170