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

Coding style ruling about function/method return types and nullables

    XMLWordPrintable

Details

    • Task
    • Status: Closed
    • Low
    • Resolution: Fixed
    • Coding style

    Description

      Policy: Function/methods whitespace rules about return types and nullables

      Since 02nd April 2020, this has been agreed:

      (matching the PSR-12)

      1) No whitespace between the nullable character (question mark -?) and params or return types.
      2) No whitespace between the function closing brackets and the colon.

      With an image speaking a thousand words. Correct:

      public function foo(string $bar, ?int $baz): ?bool {}
      

      Since PHP 7.0 it's possible to specify returning types to functions & methods (examples from php.net):

      function sum($a, $b): float {
          return $a + $b;
      }
      

      More yet, since PHP 7.1 (so, eligible for Moodle 3.7 and up) it's also possible to use the nullify operator to allow also nulls to be returned:

      function sum($a, $b): ?float {
          return $a + $b;
      }
      

      This issue (followup of MDLSITE-5660) is about to decide if we want to rule how to write code using those return types, both spacing and the "?" symbol. Basically, between:

      A) function xxx(): [?]int <<== default syntax used in php site
      B) function xxx() : [?]int <<== already used syntax in core - privacy stuff mainly
      C) Current status, unruled. Basically any number of chars is allowed. See this fixture file to view all the crazy possibilities.

      Ciao

      Attachments

        Issue Links

          Activity

            People

              stronk7 Eloy Lafuente (stronk7)
              stronk7 Eloy Lafuente (stronk7)
              Marina Glancy, Eloy Lafuente (stronk7)
              Votes:
              2 Vote for this issue
              Watchers:
              16 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: