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

HtmlFormFields::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 4
nc 1
nop 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
10
class HtmlFormFields extends HtmlSemCollection {
11
12
	use FieldsTrait;
13
	protected $_equalWidth;
14
15
	public function __construct($identifier, $fields=array(),$equalWidth=true) {
16
		parent::__construct($identifier, "div");
17
		$this->_equalWidth=$equalWidth;
18
		$this->addItems($fields);
19
	}
20
21
	public function compile(JsUtils $js=NULL,View $view=NULL){
22
		if($this->_equalWidth){
23
			$count=$this->count();
24
			$this->setClass( Wide::getConstants()["W".$count]." fields");
25
		}else
26
			$this->setClass("fields");
27
		return parent::compile($js,$view);
28
	}
29
30
	public function setWidth($index,$width){
31
		$this->_equalWidth=false;
32
		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\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...
33
	}
34
}