Skip to content

Commit 0fb53e0

Browse files
eliashaeusslernhovratov
authored andcommitted
[BUGFIX] Avoid yoda-style conditions in PHP
The core team agreed on not using yoda-style conditions in PHP. An appropriate PHP-CS-Fixer rule to explicitly disable yoda-style has been added to ensure that this coding style is respected. Additionally, all yoda-style conditions have been transformed to normal conditions. Resolves: #97839 Releases: main, 11.5 Change-Id: Ic3a77fe412c15ec8c8d793f2b4aa6017562a4863 Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/75005 Reviewed-by: Björn Jacob <bjoern.jacob@tritum.de> Reviewed-by: Mathias Brodala <mbrodala@pagemachine.de> Reviewed-by: Oliver Hader <oliver.hader@typo3.org> Reviewed-by: Nikita Hovratov <nikita.h@live.de> Tested-by: Mathias Brodala <mbrodala@pagemachine.de> Tested-by: core-ci <typo3@b13.com> Tested-by: Oliver Hader <oliver.hader@typo3.org> Tested-by: Nikita Hovratov <nikita.h@live.de>
1 parent bc7e7de commit 0fb53e0

File tree

19 files changed

+34
-33
lines changed

19 files changed

+34
-33
lines changed

Build/php-cs-fixer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,6 @@
9999
'single_trait_insert_per_statement' => true,
100100
'trailing_comma_in_multiline' => ['elements' => ['arrays']],
101101
'whitespace_after_comma_in_array' => true,
102+
'yoda_style' => ['equal' => false, 'identical' => false, 'less_and_greater' => false],
102103
])
103104
->setFinder($finder);

typo3/sysext/core/Classes/Charset/CharsetConverter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function conv(string $inputString, string $fromCharset, string $toCharset
106106
if ($toCharset === 'utf-8') {
107107
// Returns FALSE for unsupported charsets
108108
$convertedString = mb_convert_encoding($inputString, $toCharset, $fromCharset);
109-
if (false !== $convertedString) {
109+
if ($convertedString !== false) {
110110
return $convertedString;
111111
}
112112
}

typo3/sysext/core/Classes/Command/Descriptor/TextDescriptor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ protected function describeApplication(Application $application, array $options
8686
// calculate max. width based on available commands per namespace
8787
$width = $this->getColumnWidth($namespaces);
8888
foreach ($namespaces as $namespace) {
89-
if (ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) {
89+
if ($namespace['id'] !== ApplicationDescription::GLOBAL_NAMESPACE) {
9090
$this->write("\n");
9191
$this->write(' <comment>' . $namespace['id'] . '</comment>', true);
9292
}

typo3/sysext/core/Classes/Error/DebugExceptionHandler.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ protected function formatPath(string $path, int $line): string
494494
return sprintf(
495495
'<span class="block trace-file-path">in <strong>%s</strong>%s</span>',
496496
$this->escapeHtml($path),
497-
0 < $line ? ' line ' . $line : ''
497+
$line > 0 ? ' line ' . $line : ''
498498
);
499499
}
500500

@@ -508,15 +508,15 @@ protected function formatArgs(array $args): string
508508
{
509509
$result = [];
510510
foreach ($args as $key => $item) {
511-
if ('object' === $item[0]) {
511+
if ($item[0] === 'object') {
512512
$formattedValue = sprintf('<em>object</em>(%s)', $item[1]);
513-
} elseif ('array' === $item[0]) {
513+
} elseif ($item[0] === 'array') {
514514
$formattedValue = sprintf('<em>array</em>(%s)', is_array($item[1]) ? $this->formatArgs($item[1]) : $item[1]);
515-
} elseif ('null' === $item[0]) {
515+
} elseif ($item[0] === 'null') {
516516
$formattedValue = '<em>null</em>';
517-
} elseif ('boolean' === $item[0]) {
517+
} elseif ($item[0] === 'boolean') {
518518
$formattedValue = '<em>' . strtolower(var_export($item[1], true)) . '</em>';
519-
} elseif ('resource' === $item[0]) {
519+
} elseif ($item[0] === 'resource') {
520520
$formattedValue = '<em>resource</em>';
521521
} else {
522522
$formattedValue = str_replace("\n", '', $this->escapeHtml(var_export($item[1], true)));
@@ -546,7 +546,7 @@ protected function flattenArgs(array $args, int $level = 0, int &$count = 0): ar
546546
} else {
547547
$result[$key] = ['array', $this->flattenArgs($value, $level + 1, $count)];
548548
}
549-
} elseif (null === $value) {
549+
} elseif ($value === null) {
550550
$result[$key] = ['null', null];
551551
} elseif (is_bool($value)) {
552552
$result[$key] = ['boolean', $value];

typo3/sysext/core/Classes/Http/JsonResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private function jsonEncode(array $data, int $encodingOptions): string
9898
// Clear json_last_error()
9999
json_encode(null);
100100
$json = json_encode($data, $encodingOptions);
101-
if (JSON_ERROR_NONE !== json_last_error()) {
101+
if (json_last_error() !== JSON_ERROR_NONE) {
102102
throw new \InvalidArgumentException(sprintf(
103103
'Unable to encode data to JSON in %s: %s',
104104
__CLASS__,

typo3/sysext/core/Classes/Http/UploadedFile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function __construct($input, $size, $errorStatus, $clientFilename = null,
9797
}
9898
$this->size = $size;
9999

100-
if (!is_int($errorStatus) || 0 > $errorStatus || 8 < $errorStatus) {
100+
if (!is_int($errorStatus) || $errorStatus < 0 || $errorStatus > 8) {
101101
throw new \InvalidArgumentException('Invalid error status for an uploaded file. See UPLOAD_ERR_* constant in PHP.', 1436717303);
102102
}
103103
$this->error = $errorStatus;

typo3/sysext/core/Classes/Log/Writer/FileWriter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public function writeLog(LogRecord $record)
154154
$data
155155
);
156156

157-
if (false === fwrite(self::$logFileHandles[$this->logFile], $message . LF)) {
157+
if (fwrite(self::$logFileHandles[$this->logFile], $message . LF) === false) {
158158
throw new \RuntimeException('Could not write log record to log file', 1345036335);
159159
}
160160

typo3/sysext/core/Classes/Log/Writer/PhpErrorLogWriter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function writeLog(LogRecord $record)
5151
$this->interpolate($message, $context),
5252
$data
5353
);
54-
if (false === error_log($message)) {
54+
if (error_log($message) === false) {
5555
throw new \RuntimeException('Could not write log record to PHP error log', 1345036336);
5656
}
5757
return $this;

typo3/sysext/core/Classes/Log/Writer/SyslogWriter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function getMessageForSyslog(LogRecord $record): string
135135
*/
136136
public function writeLog(LogRecord $record)
137137
{
138-
if (false === syslog(LogLevel::normalizeLevel($record->getLevel()), $this->getMessageForSyslog($record))) {
138+
if (syslog(LogLevel::normalizeLevel($record->getLevel()), $this->getMessageForSyslog($record)) === false) {
139139
throw new \RuntimeException('Could not write log record to syslog', 1345036337);
140140
}
141141
return $this;

typo3/sysext/core/Classes/Routing/PageSlugCandidateProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ protected function getPagesFromDatabaseForCandidates(array $slugCandidates, int
274274
// it must never be accessible directly, but only in the MountPoint context. Therefore we change
275275
// the current ID and slug.
276276
// This needs to happen before the regular case, as the $pageToAdd contains the MPvar information
277-
if (PageRepository::DOKTYPE_MOUNTPOINT === (int)$row['doktype'] && $row['mount_pid_ol']) {
277+
if ((int)$row['doktype'] === PageRepository::DOKTYPE_MOUNTPOINT && $row['mount_pid_ol']) {
278278
// If the mounted page was already added from above, this should not be added again (to include
279279
// the mount point parameter).
280280
if (in_array((int)$mountedPage['uid'], $excludeUids, true)) {

typo3/sysext/core/Classes/Routing/PageUriMatcher.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected function matchCollection(string $urlPath, RouteCollection $routes): ?a
8282
$compiledRoute = $route->compile();
8383

8484
// check the static prefix of the URL first. Only use the more expensive preg_match when it matches
85-
if ('' !== $compiledRoute->getStaticPrefix() && !str_starts_with($urlPath, $compiledRoute->getStaticPrefix())) {
85+
if ($compiledRoute->getStaticPrefix() !== '' && !str_starts_with($urlPath, $compiledRoute->getStaticPrefix())) {
8686
continue;
8787
}
8888

@@ -155,7 +155,7 @@ protected function getAttributes(Route $route, string $name, array $attributes):
155155
protected function mergeDefaults(array $params, array $defaults): array
156156
{
157157
foreach ($params as $key => $value) {
158-
if (!is_int($key) && null !== $value) {
158+
if (!is_int($key) && $value !== null) {
159159
$defaults[$key] = $value;
160160
}
161161
}

typo3/sysext/core/Resources/PHP/ClassMapGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public static function createMap($path, $blacklist = null, IOInterface $io = nul
9090

9191
foreach ($classes as $class) {
9292
// skip classes not within the given namespace prefix
93-
if (null !== $namespace && !str_starts_with($class, $namespace)) {
93+
if ($namespace !== null && !str_starts_with($class, $namespace)) {
9494
continue;
9595
}
9696

@@ -154,7 +154,7 @@ private static function findClasses($path)
154154
$contents = preg_replace('{\?>.+<\?}s', '?><?', $contents);
155155
// strip trailing non-php code if needed
156156
$pos = strrpos($contents, '?>');
157-
if (false !== $pos && !str_contains(substr($contents, $pos), '<?')) {
157+
if ($pos !== false && !str_contains(substr($contents, $pos), '<?')) {
158158
$contents = substr($contents, 0, $pos);
159159
}
160160

typo3/sysext/fluid/Classes/ViewHelpers/Form/AbstractFormFieldViewHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ protected function getNameWithoutPrefix(): string
115115
}
116116
if ($this->hasArgument('value') && is_object($this->arguments['value'])) {
117117
// @todo Use $this->persistenceManager->isNewObject() once it is implemented
118-
if (null !== $this->persistenceManager->getIdentifierByObject($this->arguments['value'])) {
118+
if ($this->persistenceManager->getIdentifierByObject($this->arguments['value']) !== null) {
119119
$name .= '[__identity]';
120120
}
121121
}

typo3/sysext/form/Classes/Domain/Model/Renderable/RenderableVariant.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function __construct(
6464
array $options,
6565
VariableRenderableInterface $renderable
6666
) {
67-
if ('' === $identifier) {
67+
if ($identifier === '') {
6868
throw new IdentifierNotValidException('The given variant identifier was empty.', 1519998923);
6969
}
7070
$this->identifier = $identifier;

typo3/sysext/frontend/Classes/ContentObject/Menu/AbstractMenuContentObject.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1821,7 +1821,7 @@ protected function hasParentMenuArr()
18211821
*/
18221822
protected function hasParentMenuItemKey()
18231823
{
1824-
return null !== $this->parentMenuArrItemKey;
1824+
return $this->parentMenuArrItemKey !== null;
18251825
}
18261826

18271827
/**

typo3/sysext/frontend/Classes/Utility/CompressionUtility.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function compressionOutputHandler($outputBuffer, $mode)
5555
if (0 != ($mode & PHP_OUTPUT_HANDLER_END)) {
5656
// Check if we have content-length header
5757
foreach (headers_list() as $header) {
58-
if (0 == strncasecmp('Content-length:', $header, 15)) {
58+
if (strncasecmp('Content-length:', $header, 15) == 0) {
5959
header('Content-length: ' . $this->contentLength);
6060
break;
6161
}

typo3/sysext/indexed_search/Classes/Indexer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,12 +1530,12 @@ public function checkMtimeTstamp($mtime, $phash)
15301530
->fetchAssociative();
15311531
// If there was an indexing of the page...:
15321532
if (!empty($row)) {
1533-
if ($this->tstamp_maxAge && $row['tstamp'] + $this->tstamp_maxAge < $GLOBALS['EXEC_TIME']) {
1533+
if ($this->tstamp_maxAge && $GLOBALS['EXEC_TIME'] > $row['tstamp'] + $this->tstamp_maxAge) {
15341534
// If max age is exceeded, index the page
15351535
// The configured max-age was exceeded for the document and thus it's indexed.
15361536
$result = 1;
15371537
} else {
1538-
if (!$this->tstamp_minAge || $row['tstamp'] + $this->tstamp_minAge < $GLOBALS['EXEC_TIME']) {
1538+
if (!$this->tstamp_minAge || $GLOBALS['EXEC_TIME'] > $row['tstamp'] + $this->tstamp_minAge) {
15391539
// if minAge is not set or if minAge is exceeded, consider at mtime
15401540
if ($mtime) {
15411541
// It mtime is set, then it's tested. If not, the page must clearly be indexed.

typo3/sysext/recycler/Tests/Functional/Recycle/Pages/AdminRecycleTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public function retrieveDeletedPagesNoRecursion(): void
4747
self::assertCount(1, $deletedPages);
4848
self::assertArrayHasKey('pages', $deletedPages);
4949
self::assertCount(3, $deletedPages['pages']);
50-
self::assertTrue(0 < (int)($assertData['pages'][0]['uid'] ?? 0));
51-
self::assertTrue(0 < (int)($deletedPages['pages'][0]['uid'] ?? 0));
50+
self::assertGreaterThan(0, (int)($assertData['pages'][0]['uid'] ?? 0));
51+
self::assertGreaterThan(0, (int)($deletedPages['pages'][0]['uid'] ?? 0));
5252
self::assertSame((int)$assertData['pages'][0]['uid'], (int)$deletedPages['pages'][0]['uid']);
5353
}
5454

@@ -62,8 +62,8 @@ public function retrieveDeletedPagesOneLevelRecursion(): void
6262
self::assertCount(1, $deletedPages);
6363
self::assertArrayHasKey('pages', $deletedPages);
6464
self::assertCount(4, $deletedPages['pages']);
65-
self::assertTrue(0 < (int)($assertData['pages'][0]['uid'] ?? 0));
66-
self::assertTrue(0 < (int)($deletedPages['pages'][0]['uid'] ?? 0));
65+
self::assertGreaterThan(0, (int)($assertData['pages'][0]['uid'] ?? 0));
66+
self::assertGreaterThan(0, (int)($deletedPages['pages'][0]['uid'] ?? 0));
6767
self::assertSame((int)$assertData['pages'][0]['uid'], (int)$deletedPages['pages'][0]['uid']);
6868
}
6969
}

typo3/sysext/recycler/Tests/Functional/Recycle/Pages/UserRecycleTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public function retrieveDeletedPagesNoRecursion(): void
4747
self::assertCount(1, $deletedPages);
4848
self::assertArrayHasKey('pages', $deletedPages);
4949
self::assertCount(2, $deletedPages['pages']);
50-
self::assertTrue(0 < (int)($assertData['pages'][0]['uid'] ?? 0));
51-
self::assertTrue(0 < (int)($deletedPages['pages'][0]['uid'] ?? 0));
50+
self::assertGreaterThan(0, (int)($assertData['pages'][0]['uid'] ?? 0));
51+
self::assertGreaterThan(0, (int)($deletedPages['pages'][0]['uid'] ?? 0));
5252
self::assertSame((int)$assertData['pages'][0]['uid'], (int)$deletedPages['pages'][0]['uid']);
5353
}
5454

@@ -62,8 +62,8 @@ public function retrieveDeletedPagesOneLevelRecursion(): void
6262
self::assertCount(1, $deletedPages);
6363
self::assertArrayHasKey('pages', $deletedPages);
6464
self::assertCount(3, $deletedPages['pages']);
65-
self::assertTrue(0 < (int)($assertData['pages'][0]['uid'] ?? 0));
66-
self::assertTrue(0 < (int)($deletedPages['pages'][0]['uid'] ?? 0));
65+
self::assertGreaterThan(0, (int)($assertData['pages'][0]['uid'] ?? 0));
66+
self::assertGreaterThan(0, (int)($deletedPages['pages'][0]['uid'] ?? 0));
6767
self::assertSame((int)$assertData['pages'][0]['uid'], (int)$deletedPages['pages'][0]['uid']);
6868
}
6969

0 commit comments

Comments
 (0)