Back to all reviewers

Design flexible APIs

laravel/framework
Based on 5 comments
PHP

When designing APIs, prioritize flexibility and developer experience by: 1. **Accept broader parameter types** - Use `callable` instead of `Closure` to support various invocation patterns:

API PHP

Reviewer Prompt

When designing APIs, prioritize flexibility and developer experience by:

  1. Accept broader parameter types - Use callable instead of Closure to support various invocation patterns:
// Instead of this (restrictive):
public function throw(?Closure $callback = null)

// Do this (flexible):
public function throw(?callable $callback = null)
  1. Support fluent method chaining - Allow methods to return the instance for chainable calls:
// Example from date formatting:
public function format(string $format): static
{
    $this->format = $format;
    return $this;
}

// Usage:
$date->format('Y-m-d')->after('2023-01-01');
  1. Accept multiple parameter formats - Make your API handle different input types intelligently:
// Example with status code handling:
if (is_numeric($callback) || is_string($callback) || is_array($callback)) {
    $callback = static::response($callback);
}
  1. Use enums for constrained options - Instead of arbitrary integers, use typed enums:
// Instead of:
public static function query($array, $encodingType = PHP_QUERY_RFC3986)

// Do this:
public static function query($array, HttpQueryEncoding $encodingType = HttpQueryEncoding::Rfc3986)

Flexible APIs improve developer experience by being intuitive, reducing errors, and accommodating different coding styles while maintaining robustness and clarity.

5
Comments Analyzed
PHP
Primary Language
API
Category