Enforces that methods with boolean return types should not start with "get".
This rule detects methods that start with "get" (or "_get") and return a boolean value, suggesting the use of more descriptive naming conventions like "is" or "has" for boolean getters.
This rule supports the following configuration options:
- Type:
bool - Default:
false - Description: When
true, the rule also applies to methods that have parameters. Whenfalse(default), only methods with no parameters are checked.
Add the rule to your PHPStan configuration:
includes:
- vendor/orrison/meliorstan/config/extension.neon
rules:
- Orrison\MeliorStan\Rules\BooleanGetMethodName\BooleanGetMethodNameRule
parameters:
meliorstan:
boolean_get_method_name:
check_parameterized_methods: false<?php
class Example
{
// Valid boolean methods
public function isActive(): bool {} // ✓ Valid
public function hasItems(): bool {} // ✓ Valid
public function canPerform(): bool {} // ✓ Valid
// Valid get methods (non-boolean return)
public function getName(): string {} // ✓ Valid
public function getCount(): int {} // ✓ Valid
// Invalid - get methods with boolean return
public function getEnabled(): bool {} // ✗ Error: Method "getEnabled" starts with "get" and returns boolean, consider using "is" or "has" instead.
public function getValid(): bool {} // ✗ Error: Method "getValid" starts with "get" and returns boolean, consider using "is" or "has" instead.
/**
* @return bool
*/
public function getFlag() {} // ✗ Error: Method "getFlag" starts with "get" and returns boolean, consider using "is" or "has" instead.
// Methods with parameters are ignored by default
public function getIsActive(string $type): bool {} // ✓ Valid (has parameters)
}parameters:
meliorstan:
boolean_get_method_name:
check_parameterized_methods: truepublic function getActiveFlag(string $type): bool {} // ✗ Now invalid
public function getValidated(array $data): bool {} // ✗ Now invalid- This rule applies to methods that start with "get" or "_get" (case-insensitive)
- Boolean return types include:
bool,true,false(both in type declarations and@returnannotations) - By default, only methods with no parameters are checked
- When
check_parameterized_methodsis enabled, methods with parameters are also validated - Consider using "is", "has", "can", "should", or "will" prefixes for boolean getters instead of "get"