Passed
Push — master ( 289cbe...3ce129 )
by Anton
03:29
created

Messages::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * @package Cadmium\System\Utils
5
 * @author Anton Romanov
6
 * @copyright Copyright (c) 2015-2017, Anton Romanov
7
 * @link http://cadmium-cms.com
8
 */
9
10
namespace Utils {
11
12
	use Template;
13
14
	abstract class Messages {
15
16
		protected static $view = 'Blocks/Utils/Message';
17
18
		protected static $types = ['info', 'warning', 'error', 'success'];
19
20
		protected static $items = [];
21
22
		/**
23
		 * Initialize the messages list
24
		 */
25
26
		public static function init() {
27
28
			static::$items = [];
29
		}
30
31
		/**
32
		 * Set a message
33
		 */
34
35
		public static function set(string $type, string $text, string $title = null) {
36
37
			if (!in_array($type, static::$types, true) || isset(static::$items[$type]) || ('' === $text)) return;
38
39
			static::$items[$type] = ['text' => $text, 'title' => (('' !== $title) ? $title : null)];
40
		}
41
42
		/**
43
		 * Get a message
44
		 *
45
		 * @return array|false : the message data or false if the message was not set
46
		 */
47
48
		public static function get(string $type) {
49
50
			return (static::$items[$type] ?? false);
51
		}
52
53
		/**
54
		 * Get a messages block
55
		 */
56
57
		public static function getBlock() : Template\Block {
58
59
			$messages = Template::createBlock();
60
61
			foreach (static::$items as $type => $item) {
62
63
				$messages->addItem($message = View::get(static::$view));
64
65
				$message->type = $type; $message->text = Template::createBlock($item['text']);
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<Template\Block>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property text does not exist on object<Template\Block>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
66
67
				if (isset($item['title'])) $message->getBlock('title')->set('text', $item['title'])->enable();
68
			}
69
70
			# ------------------------
71
72
			return $messages;
73
		}
74
	}
75
}
76