Completed
Push — master ( 79b035...e37d9a )
by Jean-Christophe
03:44
created

HtmlFormFields::addFields()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 18
Code Lines 14

Duplication

Lines 10
Ratio 55.56 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 18
rs 8.8571
cc 6
eloc 14
nc 16
nop 2
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\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\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
	}
78
79
	public function setGrouped(){
80
		$this->_equalWidth=false;
81
		$this->addToProperty("class", "grouped");
82
	}
83
84
	public function getName() {
85
		return $this->_name;
86
	}
87
88
	public function setName($_name) {
89
		$this->_name=$_name;
90
		return $this;
91
	}
92
93
	public static function radios($name,$items=array(),$label=NULL){
94
		$fields=array();
95
		$i=0;
96
		foreach ($items as $item){
97
			$fields[]=new HtmlFormRadio($name."-".$i++,$name,$item,$item);
98
		}
99
		$radios=new HtmlFormFields("fields-".$name,$fields);
100
		if(isset($label))
101
			$radios->setLabel($label)->setProperty("for", $name);
102
		return $radios;
103
	}
104
105
	public function setEqualWidth($_equalWidth) {
106
		$this->_equalWidth=$_equalWidth;
107
		return $this;
108
	}
109
110
111
}