Write conditions in Yoda style (true
), non-Yoda style (['equal' => false,
'identical' => false, 'less_and_greater' => false]
) or ignore those conditions
(null
) based on configuration.
Whether variables should always be on non assignable side when applying Yoda style.
Allowed types: bool
Default value: false
Style for equal (==
, !=
) statements.
Allowed types: bool
and null
Default value: true
Style for identical (===
, !==
) statements.
Allowed types: bool
and null
Default value: true
Style for less and greater than (<
, <=
, >
, >=
) statements.
Allowed types: bool
and null
Default value: null
Default configuration.
--- Original
+++ New
<?php
- if ($a === null) {
+ if (null === $a) {
echo "null";
}
With configuration: ['equal' => true, 'identical' => false, 'less_and_greater' => null]
.
--- Original
+++ New
<?php
- $b = $c != 1; // equal
- $a = 1 === $b; // identical
+ $b = 1 != $c; // equal
+ $a = $b === 1; // identical
$c = $c > 3; // less than
With configuration: ['always_move_variable' => true]
.
--- Original
+++ New
<?php
-return $foo === count($bar);
+return count($bar) === $foo;
With configuration: ['equal' => false, 'identical' => false, 'less_and_greater' => false]
.
--- Original
+++ New
<?php
// Enforce non-Yoda style.
- if (null === $a) {
+ if ($a === null) {
echo "null";
}
The rule is part of the following rule sets:
- Fixer class: PhpCsFixer\Fixer\ControlStructure\YodaStyleFixer
- Test class: PhpCsFixer\Tests\Fixer\ControlStructure\YodaStyleFixerTest
The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.