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 18 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.
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=falseortrue;// is executed like this:($f=false)ortrue;// 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===5ordie('$x must be 5.');// Instead ofif($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===5orthrownewRuntimeException('$x must be 5.');
These limitations lead to logical operators rarely being of use in current
PHP code.
Loading history...
12
13
/**
14
* Classes using multiple singleton instances.
15
*
16
* @since __DEPLOY_VERSION__
17
*/
18
trait HasInstances
19
{
20
/**
21
* Cached instances
22
*
23
* @var array
24
*/
25
protected static $instances = array();
26
27
/**
28
* Remove an instance from cache.
29
*
30
* @param integer $id Class identifier
31
*
32
* @return void
33
*/
34
1
public static function clearInstance($id)
35
{
36
1
$class = get_called_class();
37
38
1
unset(static::$instances[$class][$id]);
39
1
}
40
41
/**
42
* Create and return a cached instance
43
*
44
* @param integer $id Identifier of the active instance
45
*
46
* @return $this
47
*/
48
3
public static function getInstance($id)
49
{
50
3
$class = get_called_class();
51
52
3
if (empty(static::$instances[$class][$id]))
53
3
{
54
2
static::$instances[$class][$id] = new static($id);
The call to HasInstances::__construct() has too many arguments starting with $id.
This check compares calls to functions or methods with their respective definitions.
If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the
check may pick up the wrong definition and report false positives. One codebase
where this has been known to happen is Wordpress.
In this case you can add the @ignorePhpDoc
annotation to the duplicate definition and it will be ignored.
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.