Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions blink/internal/fixer/rules/yoda_style.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,20 @@ func (f YodaStyle) Fix(s *tokens.Stream) bool {
if rs < 0 {
continue
}
// with always_move_variable=false an operand wrapped in "(" is not
// treated as a variable, so a grouped/assigned right side stays put (a
// leading cast like "(int) $x" is not a grouping paren)
if s.At(rs).Kind == token.Punct && s.At(rs).Value == "(" && !isCastParen(s, rs) {
continue
}
re := rightComparisonOperandEnd(s, rs)
if re < 0 {
continue
}
// with always_move_variable=false a right side that is purely a
// parenthesized group "(...)" is not treated as a variable and stays put
// (a leading cast like "(int) $x" is not a grouping paren). A group that
// is only the base of a larger primary, e.g. "(new X())->y()", is a
// variable expression and is still swapped.
if s.At(rs).Kind == token.Punct && s.At(rs).Value == "(" && !isCastParen(s, rs) {
if c := s.MatchForward(rs); c < 0 || c == re {
continue
}
}
if spanIsConstant(s, rs, re) {
continue // both sides constant
}
Expand Down
3 changes: 3 additions & 0 deletions blink/internal/fixer/rules/yoda_style_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ func TestYodaStyle(t *testing.T) {
assertFix(t, f, "<?php\nif (null === static::$r) {}", "<?php\nif (static::$r === null) {}", true)
assertFix(t, f, "<?php\nif (null !== $this->pending) {}", "<?php\nif ($this->pending !== null) {}", true)
assertFix(t, f, "<?php\nif (true === $app->resolved($a)) {}", "<?php\nif ($app->resolved($a) === true) {}", true)
// right side is a parenthesized group that is the base of a larger primary,
// e.g. "(new X())->y()": still a variable expression, so it is de-yoda'd
assertFix(t, f, "<?php\nif (null !== (new static($a))->user()) {}", "<?php\nif ((new static($a))->user() !== null) {}", true)

// constant name, ::class, signed number, empty array on the left
assertFix(t, f, "<?php\nif (JSON_ERROR_NONE === json_last_error()) {}", "<?php\nif (json_last_error() === JSON_ERROR_NONE) {}", true)
Expand Down
Loading