2

The tool rector has a configuration option for certain rules called TREAT_AS_NON_EMPTY. For example, https://github.com/rectorphp/rector/blob/main/docs/rector_rules_overview.md#disallowedemptyrulefixerrector

What does this configuration actually do?

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • I don't know if this is helpful or if you found this already, but the only place that I found the result of that constant being used was here: https://github.com/rectorphp/rector-src/blob/7da67eebfc9011b316e08d80be83a6223f83a371/rules/Strict/NodeFactory/ExactCompareFactory.php#L220 – Chris Haas May 12 '23 at 19:08

1 Answers1

1

Depending on which rules you use this setting for, it performs any of the following refactors:

BooleanInBooleanNotRuleFixerRector: !$string_or_null => $string === null
BooleanInIfConditionRuleFixerRector: if ($string) => if ($string !== '')
BooleanInTernaryOperatorRuleFixerRector: $array ? 1 : 2 => $array !== [] ? 1 : 2
DisallowedEmptyRuleFixerRector: empty($array) => $array === []
DisallowedShortTernaryRuleFixerRector: $array ?: 2 => $array !== [] ? $array : 2

The general idea is that it replaces implicit conversions of types to booleans with explicit comparisons with the empty value.

The first transformation also avoids treating an empty string as falsey. The assumption being that when you declare the variable's type as string|null, you didn't intend for an empty string to be treated equivalent to null.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Could you clarify the difference when it is switched on vs off? Would you be able to provide an example on https://getrector.com/demo ? – Dharman May 12 '23 at 20:12
  • I've never used Rector (I never even heard of it before), so I'm not sure how to make a demo. But if you switch it off, I assume it simply doesn't perform the corresponding conversion. – Barmar May 12 '23 at 20:15
  • I've updated the answer to add the names of the rules corresponding to each conversion. – Barmar May 12 '23 at 20:18
  • No, the conversion is performed either way. The option should only change how it is performed, but I can't understand what the difference is between it off and on. – Dharman May 12 '23 at 20:38
  • In that case I'm as confused as you are. It seems like it should control whether a particular conversion is done, not how. – Barmar May 12 '23 at 20:45