Prompt
Prioritize code readability over clever solutions by:
- Using early returns to reduce nesting
- Leveraging modern PHP features when they improve clarity
- Maintaining consistent style patterns
- Simplifying complex logic
Example - Before:
protected function parseIds($value)
{
if (is_null($value)) {
return [];
}
if (is_string($value)) {
return array_map('trim', explode(',', $value));
}
check_type($value, 'array', $key, 'Environment');
return $value;
}
Example - After:
protected function parseIds($value)
{
return match (true) {
$value === null => [],
is_string($value) => array_map('trim', explode(',', $value)),
default => check_type($value, 'array', $key, 'Environment'),
};
}
The improved version:
- Uses match expression for cleaner flow control
- Maintains consistent null comparison style
- Reduces nesting and cognitive load
- Leverages modern PHP features appropriately
Choose simpler constructs when they improve readability, but avoid sacrificing clarity for brevity. The goal is to write code that is easy to understand and maintain.