Skip to content

[Downgrade PHP 8.0] Throw expression #4314

Description

@leoloso

Feature Request

Downgrade new feature from PHP 8.0 to its equivalent PHP 7.4 code. RFC with explanation

We must identify all the different places where throw can appear as an expression, and transform each of them to a statement.

If when executing the rule the code contains an unhandled example, it should throw a DowngradeNotImplementedException (and not a DowngradeNotPossibleException), asking the developer to report the code as a new issue in Rector, so it can be implemented.

From the RFC, the following examples have been identified. What others are there?

Diff

// This was previously not possible since arrow functions only accept a single expression while throw was a statement.
-$callable = fn() => throw new Exception();
+$callable = function() {
+    throw new Exception()
+};
 
// $value is non-nullable.
-$value = $nullableValue ?? throw new InvalidArgumentException();
+if ($nullableValue !== null) {
+    $value = $nullableValue;
+} else {
+    throw new InvalidArgumentException();
+}
 
// $value is truthy.
-$value = $falsableValue ?: throw new InvalidArgumentException();
+if ($falsableValue) {
+    $value = $falsableValue;
+} else {
+    throw new InvalidArgumentException();
+}
 
// $value is only set if the array is not empty.
-$value = !empty($array)
-    ? reset($array)
-    : throw new InvalidArgumentException();
+if (!empty($array)) {
+    $value = reset($array);
+} else {
+    throw new InvalidArgumentException();
+}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions