Skip to content
On this page

object-members-limit

Ensure that objects in PHP have a maximum number of members

Options

max

Maximum number of members. Default is 50

Type: number

Default: 50

Examples

  • Examples of correct code for this rule using default options

Class members within the limit

php
<?php
class ClassWithFewMembers {
	public $a;
	public $b;
	
	public function __construct() {
		$this->a = 1;
		$this->b = 2;
	}
	
	public function getA() {
		return $this->a;
	}
	
	public function getB() {
		return $this->b;
	}
	
	public function setA($a) {
		$this->a = $a;
	}
	
	public function setB($b) {
		$this->b = $b;
	}
}

Interface members within the limit

php
<?php
interface InterfaceWithFewMembers {
	
	public function getA();
	public function getB();
	
	public function setA($a);
	public function setB($b);
}

Trait members within the limit

php
<?php
trait TraitWithFewMembers {
	private $a;
	private $b;
	private $c;
	private $d;
	private $e;
	private $f;
	
	public function getA() {
		return $this->a;
	}
	
	public function getB() {
		return $this->b;
	}
	
	public function getC() {
		return $this->c;
	}
}

Enum members within the limit

php
<?php
enum Months {
	case January;
	case February;
	case March;
	case April;
	case May;
	case June;
	case July;
	case August;
	case September;
	case October;
	case November;
	case December;
}
  • Examples of incorrect code for this rule using default options

Class members over the limit

php
<?php
/* taqwim "taqwim/object-members-limit": {max: 10 } */
class ClassWithManyMembers {
	public $a;
	public $b;
	public $c;
	public $d;
	public $e;
	public $f;
	public $g;
	public $h;
	public $i;
	public $j;
	public $k;
	public $l;
	
	function setA($a) {
		$this->a = $a;
	}
	
	function setB($b) {
		$this->b = $b;
	}
	
	function setC($c) {
		$this->c = $c;
	}
	
	function setD($d) {
		$this->d = $d;
	}
	
	function setE($e) {
		$this->e = $e;
	}
	
	function setF($f) {
		$this->f = $f;
	}
	
	function setG($g) {
		$this->g = $g;
	}
	
	
}

Interface members over the limit

php
<?php
/* taqwim "taqwim/object-members-limit": {max: 5 } */
interface InterfaceWithManyMembers {
	
	public function getA();
	public function getB();
	public function getC();
	public function getD();
	public function getE();
	public function getF();
	public function getG();
	
	public function setA($a);
	public function setB($b);
	public function setC($c);
	public function setD($d);
	public function setE($e);
	public function setF($f);
	public function setG($g);
}

Trait members over the limit

php
<?php
/* taqwim "taqwim/object-members-limit": {max: 15 } */
trait TraitWithManyMembers {
	private $a;
	private $b;
	private $c;
	private $d;
	private $e;
	private $f;
	private $g;
	private $h;
	private $i;
	private $j;
	private $k;
	
	public function getA() {
		return $this->a;
	}
	
	public function getB() {
		return $this->b;
	}
	
	public function getC() {
		return $this->c;
	}
	
	public function getD() {
		return $this->d;
	}
	
	public function getE(){
		return $this->e;
	}
}

Enum members over the limit

php
<?php
/* taqwim "taqwim/object-members-limit": {max: 11 } */
enum Months {
	case January;
	case February;
	case March;
	case April;
	case May;
	case June;
	case July;
	case August;
	case September;
	case October;
	case November;
	case December;
}

Released under the MIT License.