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.
Using logical operators such as and 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.
It seems like $encrypt can also be of type object<WellCommerce\Bund...oreBundle\Entity\mixed>. However, the property $encrypt is declared as type string. Maybe add an additional type check?
Our type inference engine has found a suspicous assignment of a value to a property.
This check raises an issue when a value that can be of a mixed type is assigned to
a property that is type hinted more strictly.
For example, imagine you have a variable $accountId that can either hold an
Id object or false (if there is no account id yet). Your code now assigns that
value to the id property of an instance of the Account class. This class
holds a proper account, so the id value must no longer be false.
Either this assignment is in error or a type check should be added for that assignment.
classId{public$id;publicfunction__construct($id){$this->id=$id;}}classAccount{/** @var Id $id */public$id;}$account_id=false;if(starsAreRight()){$account_id=newId(42);}$account=newAccount();if($accountinstanceofId){$account->id=$account_id;}
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
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 are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
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 withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.