Completed
Push — master ( 98392d...8b7375 )
by Jean-Christophe
04:05
created

HtmlIconGroups::createItem()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 9
nc 4
nop 1
1
<?php
2
3
namespace Ajax\semantic\html\elements;
4
5
use Ajax\semantic\html\base\HtmlSemCollection;
6
use Ajax\service\JArray;
7
8
/**
9
 * Semantic Icons group component
10
 * @see http://semantic-ui.com/elements/icon.html#/definition
11
 * @author jc
12
 * @version 1.001
13
 */
14
class HtmlIconGroups extends HtmlSemCollection {
15
16
	public function __construct($identifier, $icons=array(), $size="") {
17
		parent::__construct($identifier, "i", "icons");
18
		$this->addItems($icons);
19
		$this->setSize($size);
20
	}
21
22
	protected function createItem($value) {
23
		$icon=$value;
24
		if (\is_array($value)) {
25
			$icon=JArray::getValue($value, "icon", 0);
26
			$size=JArray::getValue($value, "size", 1);
27
		}
28
		$iconO=new HtmlIcon("icon-" . $this->identifier, $icon);
29
		if (isset($size)) {
30
			$iconO->setSize($size);
31
		}
32
		return $iconO;
33
	}
34
35
	protected function createCondition($value) {
36
		return ($value instanceof HtmlIcon) === false;
37
	}
38
39
	public function getIcon($index) {
40
		return $this->content[$index];
41
	}
42
43
	public function toCorner($index=1) {
44
		$this->getItem($index)->toCorner();
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 toCorner() does only exist in the following sub-classes of Ajax\common\html\HtmlDoubleElement: Ajax\semantic\html\elements\HtmlIconGroups, Ajax\semantic\html\elements\HtmlLabel. 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...
45
		return $this;
46
	}
47
}