Skip to content

Latest commit

 

History

History
111 lines (83 loc) · 2.97 KB

CamelCaseMethodName.md

File metadata and controls

111 lines (83 loc) · 2.97 KB

CamelCaseMethodName

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.

Configuration

This rule supports the following configuration options:

allow_consecutive_uppercase

  • Type: bool
  • Default: false
  • Description: When enabled, allows consecutive uppercase letters in method names (e.g., getHTTPResponse becomes valid).

allow_underscore_in_tests

  • Type: bool
  • Default: false
  • Description: When enabled, allows underscores in test method names that start with "test" (e.g., test_with_underscores becomes valid).

allow_underscore_prefix

  • Type: bool
  • Default: false
  • Description: When enabled, allows a single underscore prefix for method names (e.g., _privateMethod becomes valid).

Usage

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

Examples

Default Configuration

<?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.
}

Configuration Examples

Allow Consecutive Uppercase

parameters:
    meliorstan:
        camel_case_method_name:
            allow_consecutive_uppercase: true
public function getHTTPResponse() {} // ✓ Now valid
public function parseXMLAPI() {} // ✓ Now valid

Allow Underscores in Tests

parameters:
    meliorstan:
        camel_case_method_name:
            allow_underscore_in_tests: true
public function test_with_underscores() {} // ✓ Now valid

Allow Underscore Prefix

parameters:
    meliorstan:
        camel_case_method_name:
            allow_underscore_prefix: true
public function _privateMethod() {} // ✓ Now valid