Skip to content

Enable ordered_class_elements rule #5265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

paulbalandan
Copy link
Member

Description
Enables the rule to be consistent in ordering the class's properties and methods.

$ vendor/bin/php-cs-fixer describe ordered_class_elements
Description of ordered_class_elements rule.
Orders the elements of classes/interfaces/traits.

Fixer is configurable using following options:
* order (a subset of ['use_trait', 'public', 'protected', 'private', 'constant', 'constant_public', 'constant_protected', 'constant_private', 'property', 'property_static', 'property_public', 'property_protected', 'property_private', 'property_public_readonly', 'property_protected_readonly', 'property_private_readonly', 'property_public_static', 'property_protected_static', 'property_private_static', 'method', 'method_abstract', 'method_static', 'method_public', 'method_protected', 'method_private', 'method_public_abstract', 'method_protected_abstract', 'method_public_abstract_static', 'method_protected_abstract_static', 'method_public_static', 'method_protected_static', 'method_private_static', 'construct', 'destruct', 'magic', 'phpunit']): list of strings defining order of elements; defaults to ['use_trait', 'constant_public', 'constant_protected', 'constant_private', 'property_public', 'property_protected', 'property_private', 'construct', 'destruct', 'magic', 'phpunit', 'method_public', 'method_protected', 'method_private']
* sort_algorithm ('alpha', 'none'): how multiple occurrences of same type statements should be sorted; defaults to 'none'

Fixing examples:
 * Example #1. Fixing with the default configuration.
   ---------- begin diff ----------
   --- Original
   +++ New
   @@ -1,30 +1,30 @@
    <?php
    final class Example
    {
        use BarTrait;
        use BazTrait;
        const C1 = 1;
        const C2 = 2;
   -    protected static $protStatProp;
        public static $pubStatProp1;
        public $pubProp1;
   +    var $pubProp2;
   +    public static $pubStatProp2;
   +    public $pubProp3;
   +    protected static $protStatProp;
        protected $protProp;
   -    var $pubProp2;
        private static $privStatProp;
        private $privProp;
   -    public static $pubStatProp2;
   -    public $pubProp3;
        protected function __construct() {}
   -    private static function privStatFunc() {}
   +    public function __destruct() {}
   +    public function __toString() {}
        public function pubFunc1() {}
   -    public function __toString() {}
   -    protected function protFunc() {}
        function pubFunc2() {}
        public static function pubStatFunc1() {}
        public function pubFunc3() {}
        static function pubStatFunc2() {}
   -    private function privFunc() {}
        public static function pubStatFunc3() {}
   +    protected function protFunc() {}
        protected static function protStatFunc() {}
   -    public function __destruct() {}
   +    private static function privStatFunc() {}
   +    private function privFunc() {}
    }

   ----------- end diff -----------

 * Example #2. Fixing with configuration: ['order' => ['method_private', 'method_public']].
   ---------- begin diff ----------
   --- Original
   +++ New
   @@ -1,6 +1,6 @@
    <?php
    class Example
    {
   +    private function B(){}
        public function A(){}
   -    private function B(){}
    }

   ----------- end diff -----------

 * Example #3. Fixing with configuration: ['order' => ['method_public'], 'sort_algorithm' => 'alpha'].
   ---------- begin diff ----------
   --- Original
   +++ New
   @@ -1,8 +1,8 @@
    <?php
    class Example
    {
   -    public function D(){}
   +    public function A(){}
        public function B(){}
   -    public function A(){}
        public function C(){}
   +    public function D(){}
    }

   ----------- end diff -----------

As a possible future enhancement, we can streamline this to sort also the elements by their visibilities (i.e. public, protected, private).

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@paulbalandan paulbalandan merged commit 1ecae2f into codeigniter4:develop Nov 1, 2021
@paulbalandan paulbalandan deleted the ordered_class_elements branch November 1, 2021 05:05
paulbalandan added a commit to CodeIgniter/coding-standard that referenced this pull request Nov 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants