-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathUselessConditions.php
139 lines (109 loc) · 2.7 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
<?php
declare(strict_types=1);
namespace Conditions;
use function count;
use function strpos;
class UselessConditions
{
public function uselessCondition(): bool
{
return $foo === 'foo';
}
public function uselessIfCondition(): bool
{
return $bar === 'bar';
}
public function uselessIfConditionWithComment(): bool
{
return $bar === 'bar';
}
public function uselessNegativeCondition(): bool
{
return $foo === 'foo';
}
public function uselessIfNegativeCondition(): bool
{
return $bar !== 'bar';
}
public function unecessaryIfMethodForEarlyReturn(): bool
{
if ($bar === 'bar') {
return false;
}
if ($foo === 'foo') {
return false;
}
return $baz !== 'baz';
}
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
{
return $bar === 'bar' && $foo === 'foo' && $baz !== 'quox';
}
public function uselessIfConditionWithEvenMoreComplexIf(): bool
{
return $bar === 'bar' || $foo === 'foo' || $baz !== 'quox';
}
public function uselessIfConditionWithComplexCondition(): bool
{
return $bar !== 'bar' && $foo !== 'foo' && $baz === 'quox';
}
public function uselessIfConditionWithTernary(): bool
{
if ($this->isTrue()) {
return $this->isTrulyTrue() ? true : false;
}
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';
}
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;
}
public function necessaryTernary(): int
{
return strpos('foo', 'This is foo and bar') !== false ? 1 : 0;
}
}