Completed
Push — master ( 49f42f...848ec5 )
by Jean-Christophe
03:06
created

FieldTrait::addAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
nc 3
cc 3
eloc 9
nop 4
1
<?php
2
3
namespace Ajax\semantic\html\collections\form\traits;
4
5
use Ajax\semantic\html\modules\HtmlDropdown;
6
use Ajax\semantic\html\elements\HtmlButton;
7
use Ajax\semantic\html\base\constants\Direction;
8
use Ajax\semantic\html\base\constants\State;
9
10
trait FieldTrait {
11
12
	public abstract function addToProperty($name, $value, $separator=" ");
13
	public abstract function addLabel($caption, $style="label-default", $leftSeparator="&nbsp;");
14
	public abstract function addContent($content,$before=false);
15
	public function setFocus() {
16
		$this->addToProperty("class", State::FOCUS);
17
	}
18
19
	public function addLoading() {
20
		if ($this->_hasIcon === false) {
0 ignored issues
show
Bug introduced by
The property _hasIcon does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
21
			throw new \Exception("Input must have an icon for showing a loader, use addIcon before");
22
		}
23
		return $this->addToProperty("class", State::LOADING);
24
	}
25
26
	public function labeled($label, $direction=Direction::LEFT, $icon=NULL) {
27
		$labelO=$this->addLabel($label,$direction===Direction::LEFT,$icon);
0 ignored issues
show
Documentation introduced by
$direction === \Ajax\sem...nstants\Direction::LEFT is of type boolean, but the function expects a 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...
28
		$this->addToProperty("class", $direction . " labeled");
29
		return $labelO;
30
	}
31
32
	public function labeledToCorner($icon, $direction=Direction::LEFT) {
33
		return $this->labeled("", $direction . " corner", $icon)->toCorner($direction);
34
	}
35
36
	public function addAction($action, $direction=Direction::RIGHT, $icon=NULL, $labeled=false) {
37
		$actionO=$action;
38
		if (\is_object($action) === false) {
39
			$actionO=new HtmlButton("action-" . $this->identifier, $action);
0 ignored issues
show
Bug introduced by
The property identifier does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
40
			if (isset($icon))
41
				$actionO->addIcon($icon, true, $labeled);
42
		}
43
		$this->addToProperty("class", $direction . " action");
44
		$this->addContent($actionO, \strstr($direction, Direction::LEFT) !== false);
45
		return $actionO;
46
	}
47
48
	public function addDropdown($label="", $items=array(),$direction=Direction::RIGHT){
49
		$labelO=new HtmlDropdown("dd-".$this->identifier,$label,$items);
50
		$labelO->asSelect("select-".$this->identifier,false,true);
51
		return $this->labeled($labelO,$direction);
52
	}
53
54
	public function setTransparent() {
55
		return $this->addToProperty("class", "transparent");
56
	}
57
58
	public function setReadonly(){
59
		$this->getField()->setProperty("readonly", "");
0 ignored issues
show
Bug introduced by
It seems like getField() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
60
	}
61
62
}