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

Coding style clarification on short array syntax (due date: 2022-12-29)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Low Low
    • Coding style
    • None

      Policy: Coding style clarification on short array syntax

      Since 29th Dec 2022, this rule applies:

      1. Short array syntax should be used for all arrays, in all uses. This includes:

        $myarray = [];
        $myarray = ['some', 'value'];
        $myarray = (object) [];
        $myarray = (object) ['some', 'value'];
        $myarray = [
            'some' => 'value',
        ];
        $myarray = (object) [
            'some' => 'value',
        ];
        

      2. The use of the array() syntax is not allowed in new code. The existing code will not be bulk-updated at this time. Scope-specific changes (methods, classes) will be allowed in related issues.

      Voting

      Timeframe

      Voting for this issue opens immediately (Wednesday 21st December 2022) and will remain open until Thursday 29th December 2022 at 12pm UTC.

      Voting options

      The following options are suggested for voting:

      Option A

      Short array syntax should be used for all arrays, in all uses. This includes:

      $myarray = [];
      $myarray = ['some', 'value'];
      $myarray (object) [];
      $myarray (object) ['some', 'value'];
      $myarray = [
        'some' => 'value',
      ];
      $myarray = (object) [
        'some' => 'value',
      ];
      

      The use of the array() syntax is not allowed in new code. Existing code will not be bulk-updated at this time. Scope-specific changes (methods, classes) will be allowed in related issues.

      Proposed wording

      Added to the existing Arrays section

      ## Arrays
       
      {quote}
      ### Array syntax
       
      When declaring new arrays, the Short Array Syntax should be used.
       
      <ValidExample>
       
      ```php
      $myarray = [];
      $myarray = ['some', 'value'];
      $myarray (object) [];
      $myarray (object) ['some', 'value'];
      $myarray = [
        'some' => 'value',
      ];
      $myarray = (object) [
        'some' => 'value',
      ];
      ```
       
      </ValidExample>
       
      The Long Array Syntax should not be used for new code, _but may be used_ where it matches surrounding code.
       
      <InvalidExample>
       
      ```php
      $myarray = array();
      $myarray = array('some', 'value');
      $myarray (object) array();
      $myarray (object) array('some', 'value');
      $myarray = array(
        'some' => 'value',
      );
      $myarray = (object) array(
        'some' => 'value',
      );
      ```
       
      </InvalidExample>
      {quote}
      

      Option B

      Long array syntax should used for all arrays, in all uses. This includes:

      $myarray = array();
      $myarray = array('some', 'value');
      $myarray (object) array();
      $myarray (object) array('some', 'value');
      $myarray = array(
        'some' => 'value',
      );
      $myarray = (object) array(
        'some' => 'value',
      );
      

      The use of the [] syntax is not allowed in new code. Existing code will not be bulk-updated. Scope-specific changes (methods, classes) will be allowed in related issues.

      Proposed wording

      Added to the existing Arrays section

      ## Arrays
       
      {quote}
      ### Array syntax
       
      When declaring new arrays, the Short Array Syntax should be used.
       
      <ValidExample>
      $myarray = array();
      $myarray = array('some', 'value');
      $myarray (object) array();
      $myarray (object) array('some', 'value');
      $myarray = array(
        'some' => 'value',
      );
      $myarray = (object) array(
        'some' => 'value',
      );
      </ValidExample>
       
      The Long Array Syntax should not be used for new code, _but may be used_ where it matches surrounding code.
       
      <InvalidExample>
      $myarray = [];
      $myarray = ['some', 'value'];
      $myarray (object) [];
      $myarray (object) ['some', 'value'];
      $myarray = [
        'some' => 'value',
      ];
      $myarray = (object) [
        'some' => 'value',
      ];
      </InvalidExample>
       
      {quote}
      

      Option C

      No change to existing coding style. That is to say that both Short, and Long Array Syntax are both permitted, though their use should be consistent with surrounding code.

            Created:
            Updated:
            Resolved:

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