Completed
Push — master ( 8431a9...0bccd4 )
by Gabriel
04:28
created

Nip_Form_Renderer_Elements_Select::renderOptions()   B

Complexity

Conditions 10
Paths 20

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 32
ccs 0
cts 22
cp 0
rs 7.6666
cc 10
nc 20
nop 1
crap 110

How to fix   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
class Nip_Form_Renderer_Elements_Select extends Nip_Form_Renderer_Elements_Abstract
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...
3
{
4
    public function generateElement()
5
    {
6
        $return = '<select ';
7
        $return .= $this->renderAttributes();
8
        $return .= ' >' . $this->renderOptions() . '</select>';
9
        return $return;
10
    }
11
12
    public function renderOptions($options = false)
13
    {
14
        $options = $options ? $options : $this->getElement()->getOptions();
0 ignored issues
show
Bug introduced by
The method getOptions() does not exist on Nip_Form_Element_Abstract. Did you maybe mean getOption()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

14
        $options = $options ? $options : $this->getElement()->/** @scrutinizer ignore-call */ getOptions();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
15
        $return = '';
16
        foreach ($options as $value=>$atribs) {
17
            if (is_string($value) && !isset($atribs['label'])) {
18
                $return .= '<optgroup label="' . $value . '">';
19
                $return .= $this->renderOptions($atribs);
20
                $return .= '</optgroup>';
21
            } else {
22
                $return .= '<option';
23
24
                $label = $atribs['label'];
25
                unset($atribs['label']);
26
27
                $atribs['value'] = $value;
28
                $selectedValue = $this->getElement()->getValue();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $selectedValue is correct as $this->getElement()->getValue() targeting Nip\Form\Elements\AbstractElement::getValue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
29
                if ($selectedValue === 0 or $value === 0) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
30
                    if ($value === $selectedValue) {
31
                        $atribs['selected'] = 'selected';
32
                    }
33
                } elseif ($this->getElement()->getValue() == $value) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getElement()->getValue() targeting Nip\Form\Elements\AbstractElement::getValue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
34
                    $atribs['selected'] = 'selected';
35
                }
36
                
37
                foreach ($atribs as $name=>$value) {
0 ignored issues
show
Comprehensibility Bug introduced by
$value is overwriting a variable from outer foreach loop.
Loading history...
38
                    $return .= ' ' . $name . '="' . $value . '"';
39
                }
40
                $return .= '>' . $label . '</option>';
41
            }
42
        }
43
        return $return;
44
    }
45
46
    public function getElementAttribs()
47
    {
48
        $attribs = parent::getElementAttribs();
49
        return $attribs;
50
    }
51
}
52