Completed
Push — master ( 94aea7...5914b8 )
by Jean-Christophe
04:29
created

HtmlAccordion::forwardPanel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 5
1
<?php
2
3
namespace Ajax\semantic\html\modules;
4
5
use Ajax\semantic\html\base\HtmlSemCollection;
6
use Ajax\semantic\html\content\HtmlAccordionItem;
7
use Ajax\JsUtils;
8
9
class HtmlAccordion extends HtmlSemCollection{
10
11
	protected $params=array();
12
13
	public function __construct( $identifier, $tagName="div", $baseClass="ui"){
14
		parent::__construct( $identifier, "div", "ui accordion");
15
	}
16
17
18 View Code Duplication
	protected function createItem($value){
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...
19
		$count=$this->count();
20
		$title=$value;
21
		$content=NULL;
22
		if(\is_array($value)){
23
			$title=@$value[0];$content=@$value[1];
24
		}
25
		return new HtmlAccordionItem("item-".$this->identifier."-".$count, $title,$content);
26
	}
27
28
	protected function createCondition($value){
29
		return ($value instanceof HtmlAccordionItem)===false;
30
	}
31
32
	public function addPanel($title,$content){
33
		return $this->addItem([$title,$content]);
0 ignored issues
show
Documentation introduced by
array($title, $content) is of type array<integer,?,{"0":"?","1":"?"}>, but the function expects a object<Ajax\common\html\HtmlDoubleElement>|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
34
	}
35
36
	/**
37
	 * render the content of $controller::$action and set the response to a new panel
38
	 * @param JsUtils $js
39
	 * @param string $title The panel title
40
	 * @param Controller $initialController
41
	 * @param string $controller a Phalcon controller
42
	 * @param string $action a Phalcon action
43
	 */
44
	public function forwardPanel(JsUtils $js,$title,$initialController,$controller,$action){
45
		return $this->addPanel($title, $js->forward($initialController, $controller, $action));
46
	}
47
48
	/*
49
	 * (non-PHPdoc)
50
	 * @see BaseHtml::run()
51
	 */
52
	public function run(JsUtils $js) {
53 View Code Duplication
		if(isset($this->_bsComponent)===false)
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...
54
			$this->_bsComponent=$js->semantic()->accordion("#".$this->identifier,$this->params);
55
			$this->addEventsOnRun($js);
56
			return $this->_bsComponent;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->_bsComponent; (Ajax\semantic\components...otstrap\components\Tabs) is incompatible with the return type of the parent method Ajax\semantic\html\base\HtmlSemCollection::run of type Ajax\common\components\GenericComponent.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
57
	}
58
59
	public function setStyled(){
60
		return $this->addToProperty("class", "styled");
61
	}
62
63
	public function setActive($index){
64
		$this->getItem($index)->setActive(true);
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 setActive() does only exist in the following sub-classes of Ajax\common\html\HtmlDoubleElement: Ajax\bootstrap\html\HtmlBreadcrumbs, Ajax\bootstrap\html\HtmlButton, Ajax\bootstrap\html\HtmlDropdown, Ajax\bootstrap\html\HtmlGlyphButton, Ajax\bootstrap\html\HtmlPagination, Ajax\bootstrap\html\HtmlProgressbar, Ajax\bootstrap\html\HtmlSplitbutton, Ajax\bootstrap\html\HtmlTabs, Ajax\bootstrap\html\content\HtmlTabItem, Ajax\semantic\html\collections\HtmlBreadcrumb, Ajax\semantic\html\content\HtmlAbsractItem, Ajax\semantic\html\content\HtmlAccordionItem, Ajax\semantic\html\content\HtmlAccordionMenuItem, Ajax\semantic\html\content\HtmlListItem, Ajax\semantic\html\content\HtmlMenuItem, Ajax\semantic\html\content\HtmlStepItem, Ajax\semantic\html\elements\HtmlButton, Ajax\semantic\html\modules\HtmlAccordion, Ajax\semantic\html\modules\HtmlProgress. 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...
65
		return $this;
66
	}
67
68
	public function setExclusive($value){
69
		$this->params["exclusive"]=$value;
70
	}
71
}