Skip to content

Commit e8dfe9b

Browse files
authored
Enforce non-yoda style (#1074)
1 parent cf0c693 commit e8dfe9b

File tree

118 files changed

+590
-585
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+590
-585
lines changed

.php-cs-fixer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@
1111
'no_superfluous_phpdoc_tags' => [
1212
'allow_mixed' => true,
1313
],
14+
'yoda_style' => [
15+
'equal' => false,
16+
'identical' => false,
17+
'less_and_greater' => false,
18+
],
1419
]);

benchmarks/LexerBench.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ public function benchIntrospectionQuery(): void
3131

3232
do {
3333
$token = $lexer->advance();
34-
} while (Token::EOF !== $token->kind);
34+
} while ($token->kind !== Token::EOF);
3535
}
3636
}

benchmarks/Utils/QueryGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct(Schema $schema, float $percentOfLeafFields)
3030
{
3131
$this->schema = $schema;
3232

33-
assert(0 < $percentOfLeafFields && $percentOfLeafFields <= 1);
33+
assert($percentOfLeafFields > 0 && $percentOfLeafFields <= 1);
3434

3535
$totalFields = 0;
3636
foreach ($schema->getTypeMap() as $type) {

benchmarks/Utils/SchemaGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ protected function createType(int $nestingLevel, ?string $typeName = null): Obje
7070
}
7171

7272
++$this->typeIndex;
73-
if (null === $typeName) {
73+
if ($typeName === null) {
7474
$typeName = 'Level_' . $nestingLevel . '_Type' . $this->typeIndex;
7575
}
7676

examples/00-hello-world/graphql.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
]);
5757

5858
$rawInput = file_get_contents('php://input');
59-
if (false === $rawInput) {
59+
if ($rawInput === false) {
6060
throw new RuntimeException('Failed to get php://input');
6161
}
6262

examples/01-blog/Blog/Data/DataSource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public static function findLatestStory(): ?Story
181181
*/
182182
public static function findStories(int $limit, ?int $afterId = null): array
183183
{
184-
$start = null !== $afterId
184+
$start = $afterId !== null
185185
? (int) array_search($afterId, array_keys(self::$stories), true) + 1
186186
: 0;
187187

examples/01-blog/Blog/Type/CommentType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function resolveAuthor(Comment $comment): ?User
6262

6363
public function resolveParent(Comment $comment): ?Comment
6464
{
65-
if (null !== $comment->parentId) {
65+
if ($comment->parentId !== null) {
6666
return DataSource::findComment($comment->parentId);
6767
}
6868

examples/01-blog/Blog/Type/Scalar/EmailType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ public function parseLiteral(Node $valueNode, ?array $variables = null): string
5454
*/
5555
private function isEmail($value): bool
5656
{
57-
return false !== filter_var($value, FILTER_VALIDATE_EMAIL);
57+
return filter_var($value, FILTER_VALIDATE_EMAIL) !== false;
5858
}
5959
}

examples/01-blog/Blog/Type/Scalar/UrlType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ public function parseLiteral(Node $valueNode, ?array $variables = null): string
5555
private function isUrl($value): bool
5656
{
5757
return is_string($value)
58-
&& false !== filter_var($value, FILTER_VALIDATE_URL);
58+
&& filter_var($value, FILTER_VALIDATE_URL) !== false;
5959
}
6060
}

examples/01-blog/graphql.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
// Prepare context that will be available in all field resolvers (as 3rd argument):
3131
$appContext = new AppContext();
3232
$currentlyLoggedInUser = DataSource::findUser(1);
33-
assert(null !== $currentlyLoggedInUser);
33+
assert($currentlyLoggedInUser !== null);
3434
$appContext->viewer = $currentlyLoggedInUser;
3535
$appContext->rootUrl = 'http://localhost:8080';
3636
$appContext->request = $_REQUEST;

examples/02-schema-definition-language/graphql.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
];
3232

3333
$rawInput = file_get_contents('php://input');
34-
if (false === $rawInput) {
34+
if ($rawInput === false) {
3535
throw new RuntimeException('Failed to get php://input');
3636
}
3737

generate-class-reference.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ function renderProp(ReflectionProperty $prop): string
170170

171171
function unwrapDocblock(string $docBlock): string
172172
{
173-
if ('' === $docBlock) {
173+
if ($docBlock === '') {
174174
return '';
175175
}
176176

@@ -191,7 +191,7 @@ function unwrapDocblock(string $docBlock): string
191191
*/
192192
function unpadDocblock($docBlock): string
193193
{
194-
if (false === $docBlock) {
194+
if ($docBlock === false) {
195195
return '';
196196
}
197197

@@ -210,11 +210,11 @@ function unpadDocblock($docBlock): string
210210
function isApi(Reflector $reflector): bool
211211
{
212212
$comment = $reflector->getDocComment();
213-
if (false === $comment) {
213+
if ($comment === false) {
214214
return false;
215215
}
216216

217-
return 1 === preg_match('~[\r\n ]+\* @api~', $comment);
217+
return preg_match('~[\r\n ]+\* @api~', $comment) === 1;
218218
}
219219

220220
file_put_contents($outputFile, '');

src/Error/Error.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function __construct(
9090
$this->nodes = array_filter(iterator_to_array($nodes));
9191
} elseif (is_array($nodes)) {
9292
$this->nodes = array_filter($nodes);
93-
} elseif (null !== $nodes) {
93+
} elseif ($nodes !== null) {
9494
$this->nodes = [$nodes];
9595
} else {
9696
$this->nodes = null;
@@ -110,7 +110,7 @@ public function __construct(
110110

111111
if ($previous instanceof ClientAware) {
112112
$this->isClientSafe = $previous->isClientSafe();
113-
} elseif (null !== $previous) {
113+
} elseif ($previous !== null) {
114114
$this->isClientSafe = false;
115115
} else {
116116
$this->isClientSafe = true;
@@ -158,7 +158,7 @@ public static function createLocatedError($error, $nodes = null, ?array $path =
158158
$message = (string) $error;
159159
}
160160

161-
$nonEmptyMessage = '' === $message
161+
$nonEmptyMessage = $message === ''
162162
? 'An unknown error occurred.'
163163
: $message;
164164

@@ -178,9 +178,9 @@ protected function isLocated(): bool
178178
$path = $this->getPath();
179179
$nodes = $this->getNodes();
180180

181-
return null !== $path
181+
return $path !== null
182182
&& count($path) > 0
183-
&& null !== $nodes
183+
&& $nodes !== null
184184
&& count($nodes) > 0;
185185
}
186186

@@ -239,11 +239,11 @@ public function getLocations(): array
239239
$nodes = $this->getNodes();
240240

241241
$this->locations = [];
242-
if (null !== $source && 0 !== count($positions)) {
242+
if ($source !== null && count($positions) !== 0) {
243243
foreach ($positions as $position) {
244244
$this->locations[] = $source->getLocation($position);
245245
}
246-
} elseif (null !== $nodes && 0 !== count($nodes)) {
246+
} elseif ($nodes !== null && count($nodes) !== 0) {
247247
foreach ($nodes as $node) {
248248
if (isset($node->loc->source)) {
249249
$this->locations[] = $node->loc->source->getLocation($node->loc->start);

src/Error/FormattedError.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ public static function printError(Error $error): string
7575
}
7676
}
7777
}
78-
} elseif (null !== $error->getSource() && 0 !== count($error->getLocations())) {
78+
} elseif ($error->getSource() !== null && count($error->getLocations()) !== 0) {
7979
$source = $error->getSource();
8080
foreach ($error->getLocations() as $location) {
8181
$printedLocations[] = self::highlightSourceAtLocation($source, $location);
8282
}
8383
}
8484

85-
return 0 === count($printedLocations)
85+
return count($printedLocations) === 0
8686
? $error->getMessage()
8787
: implode("\n\n", array_merge([$error->getMessage()], $printedLocations)) . "\n";
8888
}
@@ -121,7 +121,7 @@ private static function highlightSourceAtLocation(Source $source, SourceLocation
121121

122122
private static function getColumnOffset(Source $source, SourceLocation $location): int
123123
{
124-
return 1 === $location->line
124+
return $location->line === 1
125125
? $source->locationOffset->column - 1
126126
: 0;
127127
}
@@ -167,7 +167,7 @@ public static function createFromException(Throwable $exception, int $debugFlag
167167
$formattedError['locations'] = $locations;
168168
}
169169

170-
if (null !== $exception->path && count($exception->path) > 0) {
170+
if ($exception->path !== null && count($exception->path) > 0) {
171171
$formattedError['path'] = $exception->path;
172172
}
173173
}
@@ -179,7 +179,7 @@ public static function createFromException(Throwable $exception, int $debugFlag
179179
}
180180
}
181181

182-
if (DebugFlag::NONE !== $debugFlag) {
182+
if ($debugFlag !== DebugFlag::NONE) {
183183
$formattedError = self::addDebugEntries($formattedError, $exception, $debugFlag);
184184
}
185185

@@ -196,7 +196,7 @@ public static function createFromException(Throwable $exception, int $debugFlag
196196
*/
197197
public static function addDebugEntries(array $formattedError, Throwable $e, int $debugFlag): array
198198
{
199-
if (DebugFlag::NONE === $debugFlag) {
199+
if ($debugFlag === DebugFlag::NONE) {
200200
return $formattedError;
201201
}
202202

@@ -205,14 +205,14 @@ public static function addDebugEntries(array $formattedError, Throwable $e, int
205205
throw $e;
206206
}
207207

208-
if (null !== $e->getPrevious()) {
208+
if ($e->getPrevious() !== null) {
209209
throw $e->getPrevious();
210210
}
211211
}
212212

213213
$isUnsafe = ! $e instanceof ClientAware || ! $e->isClientSafe();
214214

215-
if (($debugFlag & DebugFlag::RETHROW_UNSAFE_EXCEPTIONS) !== 0 && $isUnsafe && null !== $e->getPrevious()) {
215+
if (($debugFlag & DebugFlag::RETHROW_UNSAFE_EXCEPTIONS) !== 0 && $isUnsafe && $e->getPrevious() !== null) {
216216
throw $e->getPrevious();
217217
}
218218

@@ -226,7 +226,7 @@ public static function addDebugEntries(array $formattedError, Throwable $e, int
226226
$formattedError['extensions']['line'] = $e->getLine();
227227
}
228228

229-
$isTrivial = $e instanceof Error && null === $e->getPrevious();
229+
$isTrivial = $e instanceof Error && $e->getPrevious() === null;
230230

231231
if (! $isTrivial) {
232232
$formattedError['extensions']['trace'] = static::toSafeTrace($e->getPrevious() ?? $e);
@@ -247,7 +247,7 @@ public static function prepareFormatter(?callable $formatter, int $debug): calla
247247
{
248248
$formatter ??= [self::class, 'createFromException'];
249249

250-
if (DebugFlag::NONE !== $debug) {
250+
if ($debug !== DebugFlag::NONE) {
251251
$formatter = static fn (Throwable $e): array => self::addDebugEntries($formatter($e), $e, $debug);
252252
}
253253

@@ -273,7 +273,7 @@ public static function toSafeTrace(Throwable $error): array
273273
if (
274274
isset($trace[0]['function']) && isset($trace[0]['class'])
275275
// Remove invariant entries as they don't provide much value:
276-
&& ('GraphQL\Utils\Utils::invariant' === $trace[0]['class'] . '::' . $trace[0]['function'])
276+
&& ($trace[0]['class'] . '::' . $trace[0]['function'] === 'GraphQL\Utils\Utils::invariant')
277277
) {
278278
array_shift($trace);
279279
} elseif (! isset($trace[0]['file'])) {
@@ -326,7 +326,7 @@ public static function printVar($var): string
326326
return 'array(' . count($var) . ')';
327327
}
328328

329-
if ('' === $var) {
329+
if ($var === '') {
330330
return '(empty string)';
331331
}
332332

@@ -342,7 +342,7 @@ public static function printVar($var): string
342342
return (string) $var;
343343
}
344344

345-
if (null === $var) {
345+
if ($var === null) {
346346
return 'null';
347347
}
348348

src/Error/Warning.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ public static function setWarningHandler(?callable $warningHandler = null): void
6666
*/
6767
public static function suppress($suppress = true): void
6868
{
69-
if (true === $suppress) {
69+
if ($suppress === true) {
7070
self::$enableWarnings = 0;
71-
} elseif (false === $suppress) {
71+
} elseif ($suppress === false) {
7272
self::$enableWarnings = self::ALL;
7373
// @phpstan-ignore-next-line necessary until we can use proper unions
7474
} elseif (is_int($suppress)) {
@@ -92,9 +92,9 @@ public static function suppress($suppress = true): void
9292
*/
9393
public static function enable($enable = true): void
9494
{
95-
if (true === $enable) {
95+
if ($enable === true) {
9696
self::$enableWarnings = self::ALL;
97-
} elseif (false === $enable) {
97+
} elseif ($enable === false) {
9898
self::$enableWarnings = 0;
9999
// @phpstan-ignore-next-line necessary until we can use proper unions
100100
} elseif (is_int($enable)) {
@@ -109,7 +109,7 @@ public static function warnOnce(string $errorMessage, int $warningId, ?int $mess
109109
{
110110
$messageLevel ??= E_USER_WARNING;
111111

112-
if (null !== self::$warningHandler) {
112+
if (self::$warningHandler !== null) {
113113
(self::$warningHandler)($errorMessage, $warningId, $messageLevel);
114114
} elseif ((self::$enableWarnings & $warningId) > 0 && ! isset(self::$warned[$warningId])) {
115115
self::$warned[$warningId] = true;
@@ -121,7 +121,7 @@ public static function warn(string $errorMessage, int $warningId, ?int $messageL
121121
{
122122
$messageLevel ??= E_USER_WARNING;
123123

124-
if (null !== self::$warningHandler) {
124+
if (self::$warningHandler !== null) {
125125
(self::$warningHandler)($errorMessage, $warningId, $messageLevel);
126126
} elseif ((self::$enableWarnings & $warningId) > 0) {
127127
trigger_error($errorMessage, $messageLevel);

src/Executor/ExecutionResult.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,16 @@ public function toArray(int $debug = DebugFlag::NONE): array
177177
);
178178

179179
// While we know that there were errors initially, they might have been discarded
180-
if ([] !== $handledErrors) {
180+
if ($handledErrors !== []) {
181181
$result['errors'] = $handledErrors;
182182
}
183183
}
184184

185-
if (null !== $this->data) {
185+
if ($this->data !== null) {
186186
$result['data'] = $this->data;
187187
}
188188

189-
if (null !== $this->extensions && count($this->extensions) > 0) {
189+
if ($this->extensions !== null && count($this->extensions) > 0) {
190190
$result['extensions'] = $this->extensions;
191191
}
192192

src/Executor/Promise/Adapter/AmpPromiseAdapter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ public function then(Promise $promise, ?callable $onFulfilled = null, ?callable
3030
{
3131
$deferred = new Deferred();
3232
$onResolve = static function (?Throwable $reason, $value) use ($onFulfilled, $onRejected, $deferred): void {
33-
if (null === $reason && null !== $onFulfilled) {
33+
if ($reason === null && $onFulfilled !== null) {
3434
self::resolveWithCallable($deferred, $onFulfilled, $value);
35-
} elseif (null === $reason) {
35+
} elseif ($reason === null) {
3636
$deferred->resolve($value);
37-
} elseif (null !== $onRejected) {
37+
} elseif ($onRejected !== null) {
3838
self::resolveWithCallable($deferred, $onRejected, $reason);
3939
} else {
4040
$deferred->fail($reason);
@@ -101,7 +101,7 @@ public function all(iterable $promisesOrValues): Promise
101101
$deferred = new Deferred();
102102

103103
$onResolve = static function (?Throwable $reason, ?array $values) use ($promisesOrValues, $deferred): void {
104-
if (null === $reason) {
104+
if ($reason === null) {
105105
assert(is_array($values), 'Either $reason or $values must be passed');
106106
$deferred->resolve(array_replace($promisesOrValues, $values));
107107

0 commit comments

Comments
 (0)