Completed
Push — master ( 35bfed...a5f27e )
by Adam
05:07
created

FlashNotifier::setMessage()   B

Complexity

Conditions 8
Paths 36

Size

Total Lines 56
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 8.0172

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 56
ccs 29
cts 31
cp 0.9355
rs 7.3333
cc 8
eloc 29
nc 36
nop 6
crap 8.0172

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * TFlashMessages.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec http://www.ipublikuj.eu
8
 * @package        iPublikuj:FlashMessages!
9
 * @subpackage     common
10
 * @since          1.0.0
11
 *
12
 * @date           01.02.15
13
 */
14
15
namespace IPub\FlashMessages;
16
17
use Nette;
18
use Nette\Localization;
19
20
use Kdyby;
21
use Kdyby\Translation;
22
23
use IPub;
24
use IPub\FlashMessages\Adapters;
25
use IPub\FlashMessages\Entities;
26
27
/**
28
 * Flash message notifier
29
 *
30
 * @package        iPublikuj:FlashMessages!
31
 * @subpackage     common
32
 *
33
 * @author         Adam Kadlec <[email protected]>
34
 */
35
class FlashNotifier extends Nette\Object
36 1
{
37
	/**
38
	 * @var SessionStorage
39
	 */
40
	protected $sessionStorage;
41
42
	/**
43
	 * @var Localization\ITranslator
44
	 */
45
	protected $translator;
46
47
	/**
48
	 * @param SessionStorage $sessionStorage
49
	 * @param Localization\ITranslator $translator
50
	 */
51
	public function __construct(SessionStorage $sessionStorage, Localization\ITranslator $translator = NULL)
52
	{
53 1
		$this->sessionStorage = $sessionStorage;
54 1
		$this->translator = $translator;
55 1
	}
56
57
	/**
58
	 * Flash a success message
59
	 *
60 1
	 * @param string $message
61
	 * @param string|null $title
62
	 *
63
	 * @return Entities\IMessage
64
	 */
65 View Code Duplication
	public function success($message, $title = NULL)
0 ignored issues
show
Unused Code introduced by
The parameter $message is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $title is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
66
	{
67
		$args = func_get_args();
68
		array_splice($args, 1, 0, ['success']);
69
70
		return call_user_func_array([$this, 'setMessage'], $args);
71
	}
72
73
	/**
74
	 * Flash an information message
75 1
	 *
76
	 * @param string $message
77
	 * @param string|null $title
78
	 *
79
	 * @return Entities\IMessage
80
	 */
81 View Code Duplication
	public function info($message, $title = NULL)
0 ignored issues
show
Unused Code introduced by
The parameter $message is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $title is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
82
	{
83 1
		$args = func_get_args();
84 1
		array_splice($args, 1, 0, ['info']);
85
86 1
		return call_user_func_array([$this, 'setMessage'], $args);
87
	}
88
89
	/**
90
	 * Flash a warning message
91
	 *
92
	 * @param string $message
93
	 * @param string|null $title
94
	 *
95
	 * @return Entities\IMessage
96
	 */
97 View Code Duplication
	public function warning($message, $title = NULL)
0 ignored issues
show
Unused Code introduced by
The parameter $message is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $title is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
98
	{
99
		$args = func_get_args();
100
		array_splice($args, 1, 0, ['warning']);
101
102
		return call_user_func_array([$this, 'setMessage'], $args);
103
	}
104
105
	/**
106
	 * Flash an error message
107
	 *
108
	 * @param string $message
109
	 * @param string|null $title
110
	 *
111
	 * @return Entities\IMessage
112
	 */
113 View Code Duplication
	public function error($message, $title = NULL)
0 ignored issues
show
Unused Code introduced by
The parameter $message is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $title is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
114
	{
115
		$args = func_get_args();
116
		array_splice($args, 1, 0, ['danger']);
117
118
		return call_user_func_array([$this, 'setMessage'], $args);
119
	}
120
121
	/**
122
	 * Add an "important" flash to the session
123
	 *
124
	 * @return $this
125
	 */
126
	public function important()
127
	{
128
		$this->sessionStorage->set(SessionStorage::KEY_IMPORTANT, TRUE);
129
130
		return $this;
131
	}
132
133
	/**
134
	 * Flash an overlay modal
135
	 *
136
	 * @param string $message
137
	 * @param string $title
138
	 *
139
	 * @return Entities\IMessage
140
	 */
141
	public function overlay($message, $title = NULL)
0 ignored issues
show
Unused Code introduced by
The parameter $message is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $title is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
142
	{
143 1
		$args = func_get_args();
144
145 1
		$level = $args[1];
146
147 1
		if (is_string($level) === FALSE || $level === NULL
148 1
			|| !in_array($level, [Entities\IMessage::LEVEL_ERROR, Entities\IMessage::LEVEL_INFO, Entities\IMessage::LEVEL_SUCCESS, Entities\IMessage::LEVEL_WARNING])
149 1
		) {
150 1
			array_splice($args, 1, 0, ['info']);
151 1
		}
152
153 1
		array_splice($args, 3, 0, [TRUE]);
154
155 1
		return call_user_func_array([$this, 'setMessage'], $args);
156
	}
157
158
	/**
159
	 * @param string $message
160
	 * @param string $level
161
	 * @param string $title
162
	 * @param boolean $overlay
163
	 * @param int|null $count
164
	 * @param array $parameters
165
	 *
166
	 * @return Entities\IMessage
167
	 */
168
	public function message($message, $level = 'info', $title = NULL, $overlay = FALSE, $count = NULL, array $parameters = [])
169
	{
170 1
		return $this->setMessage($message, $level, $title, $overlay, $count, $parameters);
171
	}
172
173
	/**
174
	 * Flash a general message
175
	 *
176
	 * @param string $message
177
	 * @param string $level
178
	 * @param string $title
179
	 * @param boolean $overlay
180
	 * @param int|null $count
181
	 * @param array $parameters
182
	 *
183
	 * @return Entities\IMessage
184
	 */
185
	public function setMessage($message, $level = 'info', $title = NULL, $overlay = FALSE, $count = NULL, array $parameters = [])
0 ignored issues
show
Unused Code introduced by
The parameter $title is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $overlay is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $count is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $parameters is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
186
	{
187 1
		$args = func_get_args();
188
		// Remove message
189 1
		unset($args[0]);
190
		// Remove level
191 1
		unset($args[1]);
192
193 1
		$title = $this->checkForAttribute($args, 'title', NULL);
194 1
		$overlay = $this->checkForAttribute($args, 'overlay', FALSE);
195 1
		$count = $this->checkForAttribute($args, 'count', NULL);
196 1
		$parameters = $this->checkForAttribute($args, 'parameters', []);
197
198
		// Support for Kdyby/Translation
199 1
		if ($message instanceof Translation\Phrase) {
200
			$phrase = new Adapters\KdybyPhraseAdapter($message);
201
202
			// Default phrase adapter
203 1
		} else if (!$message instanceof Adapters\IPhraseAdapter) {
204 1
			$phrase = new Adapters\DefaultPhraseAdapter($message, $count, $parameters);
205 1
		}
206
207
		// Support for Kdyby/Translation
208 1
		if ($title instanceof Translation\Phrase) {
209
			$titlePhrase = new Adapters\KdybyPhraseAdapter($title);
210
211
		// Default phrase adapter
212 1
		} else if (!$title instanceof Adapters\IPhraseAdapter && $title !== NULL) {
213 1
			$titlePhrase = new Adapters\DefaultPhraseAdapter($title, $count, $parameters);
214
215 1
		} else {
216 1
			$titlePhrase = NULL;
217
		}
218
219
		// Get all stored messages
220 1
		$messages = $this->sessionStorage->get(SessionStorage::KEY_MESSAGES, []);
221
222
		// Create flash message
223 1
		$flash = (new Entities\Message($this->translator, $phrase, $titlePhrase))
0 ignored issues
show
Bug introduced by
The variable $phrase does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
224 1
			->setLevel($level)
225 1
			->setOverlay($overlay);
226
227 1
		if (!$this->translator instanceof Localization\ITranslator) {
228 1
			$flash->setMessage($message);
229 1
			$flash->setTitle($title);
230 1
		}
231
232 1
		if ($this->checkUnique($flash, $messages) === FALSE) {
233 1
			$messages[] = $flash;
234 1
		}
235
236
		// Store messages in session
237 1
		$this->sessionStorage->set(SessionStorage::KEY_MESSAGES, $messages);
238
239 1
		return $flash;
240
	}
241
242
	/**
243
	 * @param Entities\IMessage $flash
244
	 * @param Entities\IMessage[] $messages
245
	 *
246
	 * @return bool
247
	 */
248
	private function checkUnique(Entities\IMessage $flash, array $messages)
249
	{
250 1
		foreach ($messages as $member) {
251 1
			if ((string) $member === (string) $flash) {
252 1
				return TRUE;
253
			}
254 1
		}
255
256 1
		return FALSE;
257
	}
258
259
	/**
260
	 * @param array $attributes
261
	 * @param string $type
262
	 * @param mixed $default
263
	 *
264
	 * @return mixed
265
	 */
266
	private function checkForAttribute(array $attributes, $type, $default)
267
	{
268 1
		foreach($attributes as $attribute) {
269
			switch($type)
270
			{
271 1
				case 'title':
272 1
					if (is_string($attribute) === TRUE || $attribute instanceof Translation\Phrase || $attribute instanceof Adapters\IPhraseAdapter) {
273 1
						return $attribute;
274
					}
275 1
					break;
276
277 1
				case 'overlay':
278 1
					if (is_bool($attribute) === TRUE) {
279 1
						return $attribute;
280
					}
281 1
					break;
282
283 1
				case 'count':
284 1
					if (is_numeric($attribute) === TRUE) {
285 1
						return $attribute;
286
					}
287 1
					break;
288
289 1
				case 'parameters':
290 1
					if (is_array($attribute) === TRUE) {
291 1
						return $attribute;
292
					}
293 1
					break;
294
			}
295 1
		}
296
297
		// Return default
298 1
		return $default;
299
	}
300
}
301