Completed
Push — master ( 9e0835...2461e8 )
by Jean-Christophe
03:27
created

HtmlFormFields   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 121
Duplicated Lines 33.06 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 26
c 5
b 0
f 0
lcom 2
cbo 7
dl 40
loc 121
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B addFields() 10 18 6
A setLabel() 0 8 2
A addItem() 0 5 1
A compile() 0 8 2
A setWidth() 0 4 1
A setInline() 0 5 1
A setGrouped() 0 4 1
A getName() 0 3 1
A setName() 0 4 1
A radios() 15 15 4
A checkeds() 15 15 4
A setEqualWidth() 0 4 1

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
2
3
namespace Ajax\semantic\html\collections\form;
4
5
use Ajax\semantic\html\base\HtmlSemCollection;
6
use Ajax\semantic\html\base\constants\Wide;
7
use Ajax\JsUtils;
8
use Phalcon\Mvc\View;
9
use Ajax\semantic\html\base\HtmlSemDoubleElement;
10
use Ajax\semantic\html\collections\form\traits\FieldsTrait;
11
12
class HtmlFormFields extends HtmlSemCollection {
13
14
	use FieldsTrait;
15
	protected $_equalWidth;
16
	protected $_name;
17
18
	public function __construct($identifier, $fields=array(),$equalWidth=true) {
19
		parent::__construct($identifier, "div");
20
		$this->_equalWidth=$equalWidth;
21
		$this->addItems($fields);
22
	}
23
24
	public function addFields($fields=NULL,$label=NULL){
25 View Code Duplication
		if(!$fields instanceof HtmlFormFields){
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...
26
			if(\is_array($fields)===false){
27
				$fields = \func_get_args();
28
				$end=\end($fields);
29
				if(\is_string($end)){
30
					$label=$end;
31
					\array_pop($fields);
32
				}else $label=NULL;
33
			}
34
		}
35
		if(isset($label))
36
			$this->setLabel($label);
37
		foreach ($fields as $field){
0 ignored issues
show
Bug introduced by
The expression $fields of type array|object<Ajax\semant...ns\form\HtmlFormFields> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
38
			$this->addItem($field);
39
		}
40
		return $this;
41
	}
42
43
	/**
44
	 * @param string|HtmlSemDoubleElement $label
45
	 * @return \Ajax\semantic\html\base\HtmlSemDoubleElement
46
	 */
47
	public function setLabel($label){
48
		$labelO=$label;
49
		if(\is_string($label)){
50
			$labelO=new HtmlSemDoubleElement("","label","",$label);
51
		}
52
		$this->insertItem($labelO,0);
53
		return $labelO;
0 ignored issues
show
Bug Compatibility introduced by
The expression return $labelO; of type string|Ajax\semantic\htm...se\HtmlSemDoubleElement is incompatible with the return type documented by Ajax\semantic\html\colle...tmlFormFields::setLabel of type Ajax\semantic\html\base\HtmlSemDoubleElement as it can also be of type string which is not included in this return type.
Loading history...
54
	}
55
	public function addItem($item){
56
		$item=parent::addItem($item);
57
		$item->setContainer($this);
58
		return $item;
59
	}
60
	public function compile(JsUtils $js=NULL,View $view=NULL){
61
		if($this->_equalWidth){
62
			$count=$this->count();
63
			$this->addToProperty("class", Wide::getConstants()["W".$count]." fields");
64
		}else
65
			$this->addToProperty("class","fields");
66
		return parent::compile($js,$view);
67
	}
68
69
	public function setWidth($index,$width){
70
		$this->_equalWidth=false;
71
		return $this->getItem($index)->setWidth($width);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Ajax\common\html\HtmlDoubleElement as the method setWidth() does only exist in the following sub-classes of Ajax\common\html\HtmlDoubleElement: Ajax\bootstrap\html\content\HtmlGridCol, Ajax\semantic\html\collections\HtmlGrid, Ajax\semantic\html\colle...ctHtmlFormRadioCheckbox, Ajax\semantic\html\colle...s\form\HtmlFormCheckbox, Ajax\semantic\html\colle...s\form\HtmlFormDropdown, Ajax\semantic\html\collections\form\HtmlFormField, Ajax\semantic\html\collections\form\HtmlFormFields, Ajax\semantic\html\collections\form\HtmlFormInput, Ajax\semantic\html\collections\form\HtmlFormRadio, Ajax\semantic\html\colle...s\form\HtmlFormTextarea, Ajax\semantic\html\colle...menus\HtmlAccordionMenu, Ajax\semantic\html\collections\menus\HtmlIconMenu, Ajax\semantic\html\colle...nus\HtmlLabeledIconMenu, Ajax\semantic\html\collections\menus\HtmlMenu, Ajax\semantic\html\content\HtmlGridCol, Ajax\semantic\html\content\HtmlGridRow. 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...
72
	}
73
74
	public function setInline(){
75
		$this->_equalWidth=false;
76
		$this->addToProperty("class", "inline");
77
		return $this;
78
	}
79
80
	public function setGrouped(){
81
		$this->_equalWidth=false;
82
		$this->addToProperty("class", "grouped");
83
	}
84
85
	public function getName() {
86
		return $this->_name;
87
	}
88
89
	public function setName($_name) {
90
		$this->_name=$_name;
91
		return $this;
92
	}
93
94 View Code Duplication
	public static function radios($name,$items=array(),$label=NULL,$value=null,$type=NULL){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
95
		$fields=array();
96
		$i=0;
97
		foreach ($items as $val=>$caption){
98
			$itemO=new HtmlFormRadio($name."-".$i++,$name,$caption,$val,$type);
99
			if($val===$value){
100
				$itemO->getInput()->getField()->setProperty("checked", "");
101
			}
102
			$fields[]=$itemO;
103
		}
104
		$radios=new HtmlFormFields("fields-".$name,$fields);
105
		if(isset($label))
106
			$radios->setLabel($label)->setProperty("for", $name);
107
		return $radios;
108
	}
109
110 View Code Duplication
	public static function checkeds($name,$items=array(),$label=NULL,$values=array(),$type=NULL){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
111
		$fields=array();
112
		$i=0;
113
		foreach ($items as $val=>$caption){
114
			$itemO=new HtmlFormCheckbox($name."-".$i++,$name,$caption,$val,$type);
0 ignored issues
show
Unused Code introduced by
The call to HtmlFormCheckbox::__construct() has too many arguments starting with $type.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
115
			if(\array_search($val, $values)!==false){
116
				$itemO->getInput()->getField()->setProperty("checked", "");
117
			}
118
			$fields[]=$itemO;
119
		}
120
		$radios=new HtmlFormFields("fields-".$name,$fields);
121
		if(isset($label))
122
			$radios->setLabel($label)->setProperty("for", $name);
123
			return $radios;
124
	}
125
126
	public function setEqualWidth($_equalWidth) {
127
		$this->_equalWidth=$_equalWidth;
128
		return $this;
129
	}
130
131
132
}