/
6.Operators.php
121 lines (101 loc) · 2.72 KB
/
6.Operators.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
declare(strict_types=1);
namespace LaminasCodingStandardTest\fixed;
class Operators
{
public function testMultipleStatementAlignment(): void
{
// There must be at least one space on either side of an equals sign
// used to assign a value to a variable. In case of a block of related
// assignments, more space MUST be inserted before the equal sign to
// promote readability.
$foo = 'bar';
$fooBar = 'bar';
}
public function testOperatorSpacing(): void
{
// There MUST be one single whitespace around logical operators.
if ($foo || $bar) {
return;
}
if ($foo || $bar && $baz) {
return;
}
if ($foo || $bar && $baz) {
return;
}
if ($foo || $bar && $baz) {
return;
}
$result = 1 + 2;
$result = 1 + 2;
$result = 1 + 2;
$result = 1 - 2;
$result = 1 - 2;
$result = 1 - 2;
$result = 1 * 2;
$result = 1 * 2;
$result = 1 * 2;
$result = 1 / 2;
$result = 1 / 2;
$result = 1 / 2;
$result = 1 % 2;
$result = 1 % 2;
$result = 1 % 2;
$result = '100%';
$result += 4;
$result += 4;
$result -= 4;
$result -= 4;
$result /= 4;
$result /= 4;
$result *= 4;
$result *= 4;
}
public function testObjectOperatorSpacing(): void
{
// There MAY NOT be any white space around the object operator unless
// multilines are used.
$this->testOperatorSpacing();
$this->testOperatorSpacing();
$this->testOperatorSpacing();
$this
->testOperatorSpacing();
$this->
testOperatorSpacing();
}
public function testUseStrictComparisonOperators(): void
{
// Loose comparison operators SHOULD NOT be used, use strict comparison
// operators instead.
// Fix that manually.
$foo == 123;
123 == $foo;
true != 0.0;
false <> true;
}
public function testUseNullCoalesceOperator(): void
{
// The null coalesce operator MUST be used when possible.
$a = $_GET['a'] ?? 'a';
$b = $bb ?? 'bb';
$c = $cc['c'] ?? 'c';
}
public function testAssignmentOperators(int $var): void
{
// Assignment operators SHOULD be used when possible.
$var &= 2;
$var |= 4;
$var .= '';
$var /= 10;
$var -= 100;
$var **= 2;
$var %= 2;
$var *= 1000;
$var += 4;
$var <<= 2;
$var >>= 2;
$var ^= 10;
$var += 10;
}
}