Uploaded image for project: 'Moodle Community Sites'
  1. Moodle Community Sites
  2. MDLSITE-7358

Update deprecation policy to take named arguments into consideration

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: High High
    • Coding style
    • None

      PHP 8.0 added support for named arguments. These are a wonderous creation and allow things like the following:

      class Greeter {
          public function greetPerson(
              string $name,
              ?int $age = null,
              array $pets = [],
          ): void {
              printf(
                  "Hello %s. I hear that you're %d years old. You have %d pets.",
                  $name,
                  $age,
                  count($pets),
              );
          }
      }
       
      $greeter = new Greeter();
      $greeter->greetPerson(
          name: "John",
          // Argument order doesn't matter!!!
          pets: ["dog", "cat", "fish"],
          age: 42,
      );
      

      But when we are told that we shouldn't be collecting age information, and we must drop the argument, our current deprecation policy has us do this:

      class Greeter {
          public function greetPerson(
              string $name,
              ?int $unused = null,
              array $pets = [],
          ): void {
              if ($unused !== null) {
                  debugging(
                      "The age argument is deprecated",
                      DEBUG_DEVELOPER,
                  );
              }
              printf(
                  "Hello %s. You have %d pets.",
                  $name,
                  count($pets),
              );
          }
      }
       
      $greeter = new Greeter();
      $greeter->greetPerson(
          name: "John",
          // Argument order doesn't matter!!!
          pets: ["dog", "cat", "fish"],
          age: 42,
      );
      

      But oh no!!! This now makes the calling code explode because we are supplying an argument which does not exist:

      Fatal error: Uncaught Error: Unknown named parameter $age in /in/WjMLa:28
      Stack trace:
      #0 {main}
        thrown in /in/WjMLa on line 28
       
      Process exited with code 255.
      

      Therefore we must amend our deprecation policy to no longer allow the rename of unused arguments.

            Unassigned Unassigned
            dobedobedoh Andrew Lyons
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:

                Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.