Completed
Push — master ( bb3350...c8eebb )
by Jean-Christophe
03:32
created

HtmlAccordion::renderViewPanel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
	 * @param array $params
44
	 */
45
	public function forwardPanel(JsUtils $js,$title,$initialController,$controller,$action,$params=array()){
46
		return $this->addPanel($title, $js->forward($initialController, $controller, $action,$params));
47
	}
48
49
	/**
50
	 * render the content of an existing view : $controller/$action and set the response to a new panel
51
	 * @param JsUtils $js
52
	 * @param string $title The panel title
53
	 * @param Controller $initialController
54
	 * @param string $viewName
55
	 * @param $params The parameters to pass to the view
56
	 */
57
	public function renderViewPanel(JsUtils $js,$title,$initialController, $viewName, $params=array()) {
58
		return $this->addPanel($title, $js->renderContent($initialController, $viewName,$params));
59
	}
60
	/*
61
	 * (non-PHPdoc)
62
	 * @see BaseHtml::run()
63
	 */
64
	public function run(JsUtils $js) {
65 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...
66
			$this->_bsComponent=$js->semantic()->accordion("#".$this->identifier,$this->params);
67
			$this->addEventsOnRun($js);
68
			return $this->_bsComponent;
69
	}
70
71
	public function setStyled(){
72
		return $this->addToProperty("class", "styled");
73
	}
74
75
	public function activate($index){
76
		$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\base\HtmlSemCollection, Ajax\semantic\html\base\HtmlSemDoubleElement, Ajax\semantic\html\base\HtmlSemNavElement, Ajax\semantic\html\collections\HtmlBreadcrumb, Ajax\semantic\html\collections\HtmlGrid, Ajax\semantic\html\collections\HtmlMessage, Ajax\semantic\html\collections\HtmlTable, Ajax\semantic\html\collections\form\HtmlForm, 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\colle...menus\HtmlAccordionMenu, Ajax\semantic\html\collections\menus\HtmlIconMenu, Ajax\semantic\html\colle...nus\HtmlLabeledIconMenu, Ajax\semantic\html\collections\menus\HtmlMenu, Ajax\semantic\html\colle...enus\HtmlPaginationMenu, Ajax\semantic\html\content\HtmlAbsractItem, Ajax\semantic\html\content\HtmlAccordionItem, Ajax\semantic\html\content\HtmlAccordionMenuItem, Ajax\semantic\html\content\HtmlDropdownItem, Ajax\semantic\html\content\HtmlGridCol, Ajax\semantic\html\content\HtmlGridRow, Ajax\semantic\html\content\HtmlListItem, Ajax\semantic\html\content\HtmlMenuItem, Ajax\semantic\html\content\HtmlStepItem, Ajax\semantic\html\content\card\HtmlCardContent, Ajax\semantic\html\conte...d\HtmlCardHeaderContent, Ajax\semantic\html\content\table\HtmlTD, Ajax\semantic\html\content\table\HtmlTR, Ajax\semantic\html\content\table\HtmlTableContent, Ajax\semantic\html\elements\HtmlButton, Ajax\semantic\html\elements\HtmlButtonGroups, Ajax\semantic\html\elements\HtmlContainer, Ajax\semantic\html\elements\HtmlDivider, Ajax\semantic\html\elements\HtmlHeader, Ajax\semantic\html\elements\HtmlIcon, Ajax\semantic\html\elements\HtmlIconGroups, Ajax\semantic\html\elements\HtmlImage, Ajax\semantic\html\elements\HtmlInput, Ajax\semantic\html\elements\HtmlLabel, Ajax\semantic\html\elements\HtmlLabelGroups, Ajax\semantic\html\elements\HtmlList, Ajax\semantic\html\elements\HtmlLoader, Ajax\semantic\html\elements\HtmlRail, Ajax\semantic\html\elements\HtmlReveal, Ajax\semantic\html\elements\HtmlSegment, Ajax\semantic\html\elements\HtmlSegmentGroups, Ajax\semantic\html\elements\HtmlStep, Ajax\semantic\html\elements\html5\HtmlLink, Ajax\semantic\html\modules\HtmlAccordion, Ajax\semantic\html\modules\HtmlDimmer, Ajax\semantic\html\modules\HtmlDropdown, Ajax\semantic\html\modules\HtmlModal, Ajax\semantic\html\modules\HtmlPopup, Ajax\semantic\html\modules\HtmlProgress, Ajax\semantic\html\modules\HtmlRating, Ajax\semantic\html\modules\HtmlSearch, Ajax\semantic\html\modules\HtmlSticky, Ajax\semantic\html\modules\HtmlTab, Ajax\semantic\html\modul...eckbox\AbstractCheckbox, Ajax\semantic\html\modules\checkbox\HtmlCheckbox, Ajax\semantic\html\modules\checkbox\HtmlRadio, Ajax\semantic\html\views\HtmlCard, Ajax\semantic\html\views\HtmlCardGroups. 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...
77
		return $this;
78
	}
79
80
	public function setExclusive($value){
81
		$this->params["exclusive"]=$value;
82
	}
83
}