-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathUselessConditions.php
177 lines (143 loc) · 3.43 KB
/
UselessConditions.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
declare(strict_types=1);
namespace Conditions;
use function count;
use function strpos;
class UselessConditions
{
public function uselessCondition(): bool
{
if ($foo === 'foo') {
return true;
} else {
return false;
}
}
public function uselessIfCondition(): bool
{
if ($bar === 'bar') {
return true;
}
return false;
}
public function uselessIfConditionWithComment(): bool
{
if ($bar === 'bar') {
// Return true here in case $bar is bar
return true;
}
// Return true here in case $bar is bar
return false;
}
public function uselessNegativeCondition(): bool
{
if ($foo !== 'foo') {
return false;
} else {
return true;
}
}
public function uselessIfNegativeCondition(): bool
{
if ($bar === 'bar') {
return false;
}
return true;
}
public function unecessaryIfMethodForEarlyReturn(): bool
{
if ($bar === 'bar') {
return false;
}
if ($foo === 'foo') {
return false;
}
if ($baz === 'baz') {
return false;
}
return true;
}
public function uselessIfConditionWithParameter(bool $bool): bool
{
if ($bool) {
return false;
}
return true;
}
public function uselessIfConditionWithBoolMethod(): bool
{
if ($this->isTrue()) {
return false;
}
return true;
}
public function uselessIfConditionWithComplexIf(): bool
{
if ($bar === 'bar' && $foo === 'foo' && $baz !== 'quox') {
return true;
} else {
return false;
}
}
public function uselessIfConditionWithEvenMoreComplexIf(): bool
{
if ($bar === 'bar' || $foo === 'foo' || $baz !== 'quox') {
return true;
} else {
return false;
}
}
public function uselessIfConditionWithComplexCondition(): bool
{
if ($bar === 'bar' || $foo === 'foo' || $baz !== 'quox') {
return false;
}
return true;
}
public function uselessIfConditionWithTernary(): bool
{
if ($this->isTrue()) {
return $this->isTrulyTrue() ? true : false;
} else {
return false;
}
}
public function necessaryIfConditionWithMethodCall(): bool
{
if ($this->shouldBeQueued()) {
$this->queue();
return true;
}
return false;
}
public function nullShouldNotBeTreatedAsFalse(): ?bool
{
if (! $this->isAdmin) {
return null;
}
return true;
}
public function uselessTernary(): bool
{
return $foo !== 'bar' ? true : false;
}
public function uselessTernaryWithParameter(bool $condition): bool
{
return $condition ? true : false;
}
public function uselessTernaryWithMethod(): bool
{
return $this->isFalse() ? true : false;
}
/**
* @param string[] $words
*/
public function uselessTernaryCheck(array $words): bool
{
return count($words) >= 1 ? false : true;
}
public function necessaryTernary(): int
{
return strpos('foo', 'This is foo and bar') !== false ? 1 : 0;
}
}