Completed
Push — master ( 3cf661...c0f044 )
by Jean-Christophe
03:28
created

HtmlFormFields   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
c 2
b 0
f 0
lcom 2
cbo 5
dl 0
loc 71
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setLabel() 0 8 2
A compile() 0 8 2
A setWidth() 0 4 1
A setInline() 0 4 1
A setGrouped() 0 4 1
A getName() 0 3 1
A setName() 0 4 1
A radios() 0 11 3
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
11
class HtmlFormFields extends HtmlSemCollection {
12
13
	use FieldsTrait;
14
	protected $_equalWidth;
15
	protected $_name;
16
17
	public function __construct($identifier, $fields=array(),$equalWidth=true) {
18
		parent::__construct($identifier, "div");
19
		$this->_equalWidth=$equalWidth;
20
		$this->addItems($fields);
21
	}
22
23
	/**
24
	 * @param string|HtmlSemDoubleElement $label
25
	 * @return \Ajax\semantic\html\base\HtmlSemDoubleElement
26
	 */
27
	public function setLabel($label){
28
		$labelO=$label;
29
		if(\is_string($label)){
30
			$labelO=new HtmlSemDoubleElement("","label","",$label);
31
		}
32
		$this->insertItem($labelO,0);
33
		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...
34
	}
35
36
	public function compile(JsUtils $js=NULL,View $view=NULL){
37
		if($this->_equalWidth){
38
			$count=$this->count();
39
			$this->addToProperty("class", Wide::getConstants()["W".$count]." fields");
40
		}else
41
			$this->addToProperty("class","fields");
42
		return parent::compile($js,$view);
43
	}
44
45
	public function setWidth($index,$width){
46
		$this->_equalWidth=false;
47
		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...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...
48
	}
49
50
	public function setInline(){
51
		$this->_equalWidth=false;
52
		$this->addToProperty("class", "inline");
53
	}
54
55
	public function setGrouped(){
56
		$this->_equalWidth=false;
57
		$this->addToProperty("class", "grouped");
58
	}
59
60
	public function getName() {
61
		return $this->_name;
62
	}
63
64
	public function setName($_name) {
65
		$this->_name=$_name;
66
		return $this;
67
	}
68
69
	public static function radios($name,$items=array(),$label=NULL){
70
		$fields=array();
71
		$i=0;
72
		foreach ($items as $item){
73
			$fields[]=new HtmlFormRadio($name."-".$i++,$name,$item,$item);
74
		}
75
		$radios=new HtmlFormFields("fields-".$name,$fields);
76
		if(isset($label))
77
			$radios->setLabel($label)->setProperty("for", $name);
78
		return $radios;
79
	}
80
81
}