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

coding style change - method chaining

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: Minor
    • Resolution: Unresolved
    • Component/s: Coding style
    • Labels:
      None

      Description

      The moodle coding standards guideline does not cover method chaining in the line wrapping section.

      It would be nice if a standard on method chaining could be added. At the moment, the only way to do method chaining is to keep everything on one line which is not practical in terms of the line size limit.

      E.g:

      $courses = myservice::instance()->get_program($idnumber)->get_set(0)->get_courses();
      

      Method chaining reads really badly if it's all on one line.
      Splitting the methods over lines makes it much easier to understand IMO:

      $courses = myservice::instance()
              ->get_program($idnumber)
              ->get_set(0)
              ->get_courses();
      

      However, the current coding standards don't permit white space before object operators.
      The above code will result in a code sniff error of:
      42 | ERROR | [x] Space found before object operator

      There are two ways I can see to resolve this problem:

      1) Amend the coding standards to prohibit method chaining. (would make me sad).

      2) Amend the coding standards to allow line wrapping for method calls.

      I think it would be a shame to prohibit method chaining as it's often a very useful pattern.

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            brudinie guy thomas
            Participants:
            Component watchers:
            Marina Glancy, Eloy Lafuente (stronk7)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated: