Completed
Push — master ( 099278...5c47c1 )
by Roberto
03:04
created

HasParams::setParam()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 20 and the first side effect is on line 11.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Joomla! common library.
4
 *
5
 * @copyright  Copyright (C) 2017 Roberto Segura López, Inc. All rights reserved.
6
 * @license    GNU/GPL 2, http://www.gnu.org/licenses/gpl-2.0.htm
7
 */
8
9
namespace Phproberto\Joomla\Traits;
10
11 1
defined('JPATH_PLATFORM') or die;
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
12
13
use Joomla\Registry\Registry;
14
15
/**
16
 * Trait for classes that have parameters.
17
 *
18
 * @since  __DEPLOY_VERSION__
19
 */
20
trait HasParams
21
{
22
	/**
23
	 * Module parameters.
24
	 *
25
	 * @var  Registry
26
	 */
27
	protected $params;
28
29
	/**
30
	 * Get a param value.
31
	 *
32
	 * @param   string  $name     Parameter name
33
	 * @param   mixed   $default  Optional default value, returned if the internal value is null.
34
	 *
35
	 * @return  mixed
36
	 */
37 3
	public function getParam($name, $default = null)
38
	{
39 3
		return $this->getParams()->get($name, $default);
40
	}
41
42
	/**
43
	 * Get the module parameters.
44
	 *
45
	 * @param   boolean  $reload  Force reloading data from DB.
46
	 *
47
	 * @return  Registry
48
	 */
49 4
	public function getParams($reload = false)
50
	{
51 4
		if ($reload || null === $this->params)
52 4
		{
53 4
			$this->params = $this->loadParams();
54 4
		}
55
56 4
		return clone $this->params;
57
	}
58
59
	/**
60
	 * Load parameters from database.
61
	 *
62
	 * @return  Registry
63
	 */
64
	abstract protected function loadParams();
65
66
	/**
67
	 * Set the value of a parameter.
68
	 *
69
	 * @param   string  $name   Parameter name
70
	 * @param   mixed   $value  Value to assign to selected parameter
71
	 *
72
	 * @return  self
73
	 */
74 1
	public function setParam($name, $value)
75
	{
76 1
		if (null === $this->params)
77 1
		{
78 1
			$this->params = $this->loadParams();
79 1
		}
80
81 1
		$this->params->set($name, $value);
82
83 1
		return $this;
84
	}
85
86
	/**
87
	 * Set the module parameters.
88
	 *
89
	 * @param   Registry  $params  Parameters to apply
90
	 *
91
	 * @return  self
92
	 */
93 1
	public function setParams(Registry $params)
94
	{
95 1
		$this->params = $params;
96
97 1
		return $this;
98
	}
99
}
100