Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Php Examples Self Documenting Code
Php Examples Self Documenting Code
pekral/arch-app-services
cursor
self-documenting-code
naming
magic-values
comments
extract-method
constants
auth-guards

Prefer naming the literal and extracting the condition, keeping only the residue no name can carry:

Install
php artisan boost:add-skill pekral/arch-app-services

Save this content to: .cursor/rules/php/examples/self-documenting-code.md

# Self-Documenting Code Examples

### Avoid: a *why* comment narrating a condition built from unnamed literals

```php
$firstFailedGuard = Arr::get($exception->guards(), '0');

// The `passport` guard is a bearer-token guard (MCP/OAuth clients among
// others) and must never answer with a browser login redirect: without a
// 401 + WWW-Authenticate challenge the client cannot discover the
// authorization server and the OAuth flow never starts (ECOMAIL-6655).
if ($request->expectsJson() || $firstFailedGuard === 'passport') {
    return response()->json(['error' => 'Unauthenticated.'], 401);
}
```

Prefer naming the literal and extracting the condition, keeping only the residue no name can carry:

```php
private const string BEARER_TOKEN_GUARD = 'passport';

if ($request->expectsJson() || $this->expectsTokenChallenge($exception)) {
    return response()->json(['error' => 'Unauthenticated.'], 401);
}

/**
 * @see ECOMAIL-6655
 */
private function expectsTokenChallenge(AuthenticationException $exception): bool
{
    return Arr::get($exception->guards(), '0') === self::BEARER_TOKEN_GUARD;
}
```

### Avoid: a magic value explained by a comment

```php
// 3 retries — Stripe recommends this before giving up on a webhook delivery
if ($attempt >= 3) {
    return;
}
```

Prefer naming the value, keeping the residual *why* as a `@see` pointer:

```php
/**
 * @see https://docs.stripe.com/webhooks#retry-logic
 */
private const int MAX_WEBHOOK_DELIVERY_ATTEMPTS = 3;

if ($attempt >= self::MAX_WEBHOOK_DELIVERY_ATTEMPTS) {
    return;
}
```

### Avoid: a comment narrating a step instead of naming it

```php
// normalize the phone number before matching it against existing customers
$normalized = preg_replace('/[^\d+]/', '', $phone);
$normalized = ltrim($normalized, '+');
```

Prefer extracting an intention-revealing method, naming the purpose the comment carried:

```php
$normalized = $this->normalizePhoneNumberForCustomerMatch($phone);
```

Self-Documenting Code Examples

Avoid: a why comment narrating a condition built from unnamed literals

$firstFailedGuard = Arr::get($exception->guards(), '0');

// The `passport` guard is a bearer-token guard (MCP/OAuth clients among
// others) and must never answer with a browser login redirect: without a
// 401 + WWW-Authenticate challenge the client cannot discover the
// authorization server and the OAuth flow never starts (ECOMAIL-6655).
if ($request->expectsJson() || $firstFailedGuard === 'passport') {
    return response()->json(['error' => 'Unauthenticated.'], 401);
}

Prefer naming the literal and extracting the condition, keeping only the residue no name can carry:

private const string BEARER_TOKEN_GUARD = 'passport';

if ($request->expectsJson() || $this->expectsTokenChallenge($exception)) {
    return response()->json(['error' => 'Unauthenticated.'], 401);
}

/**
 * @see ECOMAIL-6655
 */
private function expectsTokenChallenge(AuthenticationException $exception): bool
{
    return Arr::get($exception->guards(), '0') === self::BEARER_TOKEN_GUARD;
}

Avoid: a magic value explained by a comment

// 3 retries — Stripe recommends this before giving up on a webhook delivery
if ($attempt >= 3) {
    return;
}

Prefer naming the value, keeping the residual why as a @see pointer:

/**
 * @see https://docs.stripe.com/webhooks#retry-logic
 */
private const int MAX_WEBHOOK_DELIVERY_ATTEMPTS = 3;

if ($attempt >= self::MAX_WEBHOOK_DELIVERY_ATTEMPTS) {
    return;
}

Avoid: a comment narrating a step instead of naming it

// normalize the phone number before matching it against existing customers
$normalized = preg_replace('/[^\d+]/', '', $phone);
$normalized = ltrim($normalized, '+');

Prefer extracting an intention-revealing method, naming the purpose the comment carried:

$normalized = $this->normalizePhoneNumberForCustomerMatch($phone);
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jinas/dhivatar
store-doc/laravel-storage-driver
plus-it/laravel-translation-manager
diephp/perhaps
diephp/telegraph
osmaviation/spreadsheet
microsoft/fluentui-system-icons
mmccook/php-json-canonicalization-scheme
middlewares/firewall
qubiqx/montapacking
nyoncode/laravel-package-console
pnscripts/filament-broadquill-lite
pnscripts/laravel-sentfold-lite
vleap/mx-sdk-laravel-internal
twidpay/utils
htetmyathlaing/scout-elasticsearch-driver
artisan-toolbox/front-awesome
ls/client-assistant
users-au/laravel-client
cgv/localized-resource