Completed
Push — master ( 8bcc1d...04c30e )
by Jean-Christophe
03:22
created

FlashBag::key()   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 0
1
<?php
2
3
namespace Ubiquity\utils\flash;
4
5
use Ubiquity\utils\SessionUtils;
6
7
/**
8
 * Bag for Session Flash messages
9
 * @author jc
10
 *
11
 */
12
class FlashBag implements \Iterator{
13
	const FLASH_BAG_KEY="_flash_bag";
14
	private $array;
15
	private $position;
16
	public function __construct(){
17
		SessionUtils::start();
18
		$this->array=SessionUtils::get(self::FLASH_BAG_KEY, []);
19
	}
20
21
	public function addMessage($type,$content,$icon=NULL){
22
		$this->array[]=new FlashMessage($type, $content,$icon);
23
	}
24
25
	public function getMessages($type){
26
		$result=[];
27
		foreach ($this->array as $msg){
0 ignored issues
show
Bug introduced by
The expression $this->array of type array<integer,object<Ubi...ash\FlashMessage>>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
28
			if($msg->getType()==$type)
29
				$result[]=$msg;
30
		}
31
		return $result;
32
	}
33
34
	public function getAll(){
35
		return $this->array;
36
	}
37
38
	public function clear(){
39
		SessionUtils::delete(self::FLASH_BAG_KEY);
40
	}
41
	public function rewind() {
42
		$this->position = 0;
43
	}
44
45
	public function current() {
46
		return $this->array[$this->position];
47
	}
48
49
	public function key() {
50
		return $this->position;
51
	}
52
53
	public function next() {
54
		++$this->position;
55
	}
56
57
	public function valid() {
58
		return isset($this->array[$this->position]);
59
	}
60
61
	public function save(){
62
		$this->array=SessionUtils::set(self::FLASH_BAG_KEY, $this->array);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $this->array is correct as \Ubiquity\utils\SessionU..._BAG_KEY, $this->array) (which targets Ubiquity\utils\SessionUtils::set()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
63
	}
64
65
}
66
67