Skip to content

id-length

Ensure that identifiers are at within the length limit.

Options

min

Minimum length of identifiers.

Type: number

Default: 3

max

Maximum length of identifiers.

Type: number

Default: 30

exceptions

List of identifiers to ignore.

Type: array

Default: []

Examples

  • Examples of correct code for this rule using default options

Classes, interfaces, traits, enum, methods and variables ids within the limit

php
<?php

class ConnectDatabaseToController {
	public function connectSQLDatabase() {
		$databaseConnectionString = 1;
	}
}

interface ConnectDatabaseToModel {
	public function connectSQLDatabase();
}

trait ConnectDatabaseToTrait {
	public function connectSQLDatabase() {
		$databaseConnectionString = 1;
	}
}

enum Databases {
	case SQL;
	case NoSQL;
	case Graph;
	case Key_Value;
}

Functions, constants and loop variables ids within the limit

php
<?php

const SERVER_CONNECTION_STRING = '';
const SERVER_NAME = '';
const SERVER_USERNAME = '';

function connectDatabase() {
	echo "Connecting to database...";
}

for ($index = 0; $index < 10; $index++) {
	for ($secondIndex = 0; $secondIndex < 10; $secondIndex++) {
		echo "Index: $index, Second Index: $secondIndex";
	}
}
  • Examples of incorrect code for this rule using default options

Classes, interfaces, traits, methods and variables ids outside the limit

php
<?php

class ConnectDatabaseToControllerUsingSQLServer {
	private $SQLServerdatabaseConnectionString = '';
	
	public function connectSQLServerDatabaseWithThisFunction() {
		$this->SQLServerdatabaseConnectionString = '';
	}
}

interface ConnectSqlServerDatabaseToModel {
	public function youHaveToConnectSQLServerDatabase();
}

trait ConnectSqlServerDatabaseToTrait {
	public function helpMeConnectSqlServerDatabases() {
		$SQLServerdatabaseConnectionString = '';
	}
}

enum DatabasesTypesConnectingToServer {
	case SQL;
	case NoSQL;
	case Graph;
	case Key_Value;
}

Functions, constants and loop variables ids outside the limit

php
<?php
const LONG_SERVER_VARIABLE_INCLUDED_IN_FILE = '';
const ANOTHER_LONG_SERVER_VARIABLE_INCLUDED_IN_FILE = '';

function connectSqlServerDatabaseUsingThisFunction() {
	echo "Connecting to database...";
}

for($i; $i < 10; $i++) {
	for($j; $j < 10; $j++) {
		echo "Index: $i, Second Index: $j";
	}
}

min

  • Examples of correct code for this rule using min option

Nested for loop variables ids within the limit

php
<?php
/* taqwim "taqwim/id-length": {min: 1} */
for($i; $i < 10; $i++) {
	for($j; $j < 10; $j++) {
		for($k; $k < 10; $k++){
			echo "Index: $i, Second Index: $j, Third Index: $k";
		}
	}
}

max

  • Examples of incorrect code for this rule using max option

Functions, constants and loop variables ids outside the limit

php
<?php
/* taqwim "taqwim/id-length": { max: 10 } */
const SERVER_CONNECTION_STRING = '';
const SERVER_NAME = '';
const SERVER_USERNAME = '';

function connectDatabase() {
	echo "Connecting to database...";
}

for ($index = 0; $index < 10; $index++) {
	for ($secondIndex = 0; $secondIndex < 10; $secondIndex++) {
		echo "Index: $index, Second Index: $secondIndex";
	}
}

exceptions

  • Examples of correct code for this rule using exceptions option

Certain ids are ignored

php
<?php
/* taqwim "taqwim/id-length": { exceptions: ['i', 'j', 'k'] } */
for($i; $i < 10; $i++) {
	for($j; $j < 10; $j++) {
		for($k; $k < 10; $k++){
			echo "Index: $i, Second Index: $j, Third Index: $k";
		}
	}
}

Released under the MIT License.