Enforces that method names should follow camelCase naming convention.
This rule validates that all method names in classes follow the camelCase pattern, where the first letter is lowercase and subsequent words start with uppercase letters.
This rule supports the following configuration options:
- Type:
bool - Default:
false - Description: When enabled, allows consecutive uppercase letters in method names (e.g.,
getHTTPResponsebecomes valid).
- Type:
bool - Default:
false - Description: When enabled, allows underscores in test method names that start with "test" (e.g.,
test_with_underscoresbecomes valid).
- Type:
bool - Default:
false - Description: When enabled, allows a single underscore prefix for method names (e.g.,
_privateMethodbecomes valid).
Add the rule to your PHPStan configuration:
includes:
- vendor/orrison/meliorstan/config/extension.neon
rules:
- Orrison\MeliorStan\Rules\CamelCaseMethodName\CamelCaseMethodNameRule
parameters:
meliorstan:
camel_case_method_name:
allow_consecutive_uppercase: false
allow_underscore_in_tests: false
allow_underscore_prefix: false<?php
class Example
{
// Valid method names
public function doSomething() {} // ✓ Valid camelCase
public function getHttpResponse() {} // ✓ Valid camelCase
public function testMethod() {} // ✓ Valid camelCase
// Magic methods are always ignored
public function __construct() {} // ✓ Always valid
public function __destruct() {} // ✓ Always valid
// Invalid method names
public function do_something_invalid() {} // ✗ Error: Method name "do_something_invalid" is not in camelCase.
public function DoSomethingInvalid() {} // ✗ Error: Method name "DoSomethingInvalid" is not in camelCase.
public function getHTTPResponseInvalid() {} // ✗ Error: Method name "getHTTPResponseInvalid" is not in camelCase.
public function test_with_underscores() {} // ✗ Error: Method name "test_with_underscores" is not in camelCase.
}parameters:
meliorstan:
camel_case_method_name:
allow_consecutive_uppercase: truepublic function getHTTPResponse() {} // ✓ Now valid
public function parseXMLAPI() {} // ✓ Now validparameters:
meliorstan:
camel_case_method_name:
allow_underscore_in_tests: truepublic function test_with_underscores() {} // ✓ Now validparameters:
meliorstan:
camel_case_method_name:
allow_underscore_prefix: truepublic function _privateMethod() {} // ✓ Now valid