Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-60403

Refactor Moodle code to change naming convention for variables

XMLWordPrintable

    • Icon: Task Task
    • Resolution: Duplicate
    • Icon: Minor Minor
    • None
    • 3.4
    • General
    • None
    • MOODLE_34_STABLE

      This is a MUA project aiming at "attracting more developers to Moodle project" - https://moodleassociation.org/mod/page/view.php?id=520.
      I would like to ask Moodle HQ for an estimate of the effort required here.

      Short version

      Change Moodle variables from using $moodlecase standard to $camelCase.

      Long version

      Description

      Change naming convention for variables from $moodlestandard to $camelCase. This is to bring Moodle naming convention in line with most of the current PHP projects.
      It seems that camelCase is by far the most popular standard of naming variables, it is used by:

      Initial idea of the tasks required

      • Update https://docs.moodle.org/dev/Coding_style#Variables.
      • Update code checker.
      • Extract a list of all variables used in Moodle code.
      • Create a file with mappings between an old and new variable name, e.g.:
        • node,node
          contextlevel,contextLevel
          courseorsystemcontext,courseOrSystemContext
      • Create a list of core Moodle files (exclude 3-rd party libraries).
      • Create a script to replace variables. Ideas: fancy sed-like script, tokenizer: http://www.php.net/manual/en/function.token-get-all.php or PHP parser: https://github.com/nikic/PHP-Parser
      • Run against the whole code.
      • To avoid changes in the DB schema, some DB API methods may need to be changed - e.g. conver members of $dataobject in $DB->insert_record to lowercase.
        See if Moodle still works.
      • Run all automated tests.
      • Commit, tag.

      Challenges

      git blame will be broken.

      After refactoring on this scale, most of the lines of code will change. This means that git blame will point to this change, until more commits over-write it.

      Solution:
      If a developer wants to find out the meaningful last commit that affected given line and git blame points to commit that introduced the refactoring - he must look at the commit before that. To make it easier, we can:

      • make all changes in one big commit
      • tag the commit, e.g. REFACTORING

      Then one can call git blame REFACTORING~1 file_name.php

       h3. cherry-picking to older branches will be more difficult

      The commit introduced after refactoring will not apply cleanly in an older branch. The variable names will need to be changed manually. If the lazy ones simple bash script that uses previously created substitution list can be created - you take git commit, pipe it into the variable substitution script and then to git apply.

      Breaking the code

      We could break the code in many ways here. For example imagine that core Moodle code uses a class Mailer from 3-rd party library.
      If it would use a code like this in core:

      $mymailer = new Mailer();
      $mymailer->mymailerattribute = 17;
      

      Then refactoring mymailerattribute into myMailerAttribute will not work - since we're doing the change in core files only.

      Solution: hopefully the above is not a problem at all, since we only do replacements for moodlecase variables - no other library seems to be using this standard, so they are safe.

            Unassigned Unassigned
            tmuras Tomasz Muras
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved:

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