Skip to content

name-casefixable

Ensure that the name case is correct

Magic methods and super global varaibles are excluded from this rule.

Options

class

Class name case. Default is 'pascal'

Type: string

Default: pascal

Possible values: camel, pascal, snake, upper

interface

Interface name case. If not set, class name case will be used

Type: string

Default: undefined

Possible values: camel, pascal, snake, upper

trait

Trait name case. If not set, class name case will be used

Type: string

Default: undefined

Possible values: camel, pascal, snake, upper

enum

Enum name case. If not set, class name case will be used

Type: string

Default: undefined

Possible values: camel, pascal, snake, upper

function

Function name case. Default is snake

Type: string

Default: snake

Possible values: camel, pascal, snake, upper

method

Method name case. Default is camel

Type: string

Default: camel

Possible values: camel, pascal, snake, upper

constant

Constant name case. Default is upper

Type: string

Default: upper

Possible values: camel, pascal, snake, upper

property

Property name case. Default is snake

Type: string

Default: snake

Possible values: camel, pascal, snake, upper

variable

Variable name case. Default is snake

Type: string

Default: snake

Possible values: camel, pascal, snake, upper

Examples

  • Examples of correct code for this rule using default options

Class, interface, trait and enum declarations

php
<?php

class PascalCase {
	
	private $max_connections = 1;
	public $min_requests = 1;
	
	public function camelCase() {
		$snake_case = 1;
	}
}

interface PascalCaseInterface {
	public function camelCase();
}

trait PascalCaseTrait {
	public function camelCase() {
		$snake_case = 1;
	}
}

enum Seasons {
	case Spring;
	case Summer;
	case Autumn;
	case Winter;
}

Function declarations

php
<?php
function snake_case() {
	echo "function name is in snake_case";
}

Method declarations

php
<?php
class CheckMethodNameCase {
	public function camelCase() {
		echo "method name is in camelCase";
	}
	
	private function printLog() {
		echo "method name is in camelCase";
	}
}

interface CheckMethodNameCaseInterface {
	public function camelCase();
}

trait CheckMethodNameCaseTrait {
	public function camelCase() {
		echo "method name is in camelCase";
	}
}

Constant declarations

php
<?php
const FOO = "something";
const FOO2 = "something else";
const FOO_BAR = "something more";
const MIN_VALUE = '0.0';

Variable declarations

php
<?php
$foo_bar = 1;
$min_max = min_max(1, 2);
$variable_with_underscores = 1;
  • Examples of incorrect code for this rule using default options

Class, interface, trait and enum declarations

php
<?php

class className {
	
	private $maxConnections = 1;
	public $minRequests = 1;
	
	public function connect_database() {
		echo "connected";
	}
}

interface camelCaseInterface {
	public function get_database_type();
}

trait camelCaseTrait {
	public function log_database_connection() {
		echo "logged ...";
	}
}

enum allSeasons {
	case Spring;
	case Summer;
	case Autumn;
	case Winter;
}

function, constants and variables

php
<?php
const foo = "something";
const foo2 = "something else";
const fooBar = "something more";
const MinValue = '0.0';

function printLogs() {
	$fooBar = 1;
	$MIN_MAX = min_max(1, 2);
	$VariableWithUnderscores = 1;

	echo "function name is in camel case";
}

class

  • Examples of correct code for this rule using class option

Class, interface, and trait declarations in snake_case

php
<?php

/* taqwim "psr/name-case": {class: "snake"} */
class database_class {
	private $max_connections = 1;
	public $min_requests = 1;
	
	public function camelCase() {
		$snake_case = 1;
		
		
		// Anonymous class are not checked
		$test = new class {
           public function log($msg) {
                echo $msg;
            }
        };
	}
}

interface database_interface {
	public function camelCase();
}

trait database_trait {
	public function camelCase() {
		$snake_case = 1;
	}
}

variable

  • Examples of correct code for this rule using variable option

Variable declarations in camelCase

php
<?php
/* taqwim "psr/name-case": {variable: "camel"} */
$fooBar = 1;
$fooBarBaz = 1;
$maxMemory = 1;
$minMax = 1;

Released under the MIT License.