Skip to content

Exempt names and declarations that Laravel and Nova dictate - #395

Merged
mikebronner merged 3 commits into
mainfrom
feature/391-exempt-names-and-declarations-that-laravel-and-nova
Oct 5, 2026
Merged

mikebronner merged 3 commits into
mainfrom
feature/391-exempt-names-and-declarations-that-laravel-and-nova

Conversation

@mikebronner

@mikebronner mikebronner commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Implements #391. Four CleanCode sniffs stop reporting names and declarations that Laravel or Nova dictate. Each exemption is narrow, and every error code is unchanged.

Why DisallowStaticMembers fired on crossbible

Nova 5's Laravel\Nova\Resource never declares $model. It only reads static::$model (src/Resource.php line 250), so each resource must declare it. The existing exemption looks for an ancestor declaration and found none.

Evidence from crossbible's own vendor/bin/phpcs on app/Nova/Book.php, clean-code 0.2.3: only $model reported StaticProperty. $group, $title and $search were already silent, because Nova declares them.

DisallowStaticMembersSniff/vendor-resource.php reproduces it. With main's two files swapped in:

main:   vendor-resource.php:20:12 Static property $model is not allowed
        vendor-resource.php:27:12 Static property $ownCache is not allowed
branch: vendor-resource.php:27:12 Static property $ownCache is not allowed

The fix: a static property that no ancestor declares, but that an ancestor reads as static::$name, does not report. The sniff tokenises each ancestor's file once per run and caches the result.

Two places this reads the AC differently

  • "Still reports a static property that no ancestor declares." It still reports when no ancestor declares or reads the property ($ownCache above). Without that reading, the crossbible miss cannot be fixed, and the AC requires that fix.
  • PropertyTypeHint on Nova's $model. The AC lists $model among the covered properties, but its rule is "the nearest ancestor declaration is untyped". Nova 5 declares no $model, so public static string $model = Book::class; is legal, and the report has a code fix. It still reports, and inherited-reported.php pins this.

Changes

  • CleanCode/Helpers/EloquentModels.php: new helper with the model test (an Eloquent parent, or a Models namespace segment). ModelNamingConventions and BooleanGetMethodName both use it, so they cannot disagree.
  • RedundantNamespaceSuffix: skips a *ServiceProvider class with a prefix, declared directly in App\Providers. The namespace match ignores case.
  • BooleanGetMethodName: skips get{Name}Attribute on a model class.
  • InheritedMembers: adds the nearest-declaration walk and the static::$name read check. An ancestor whose load throws now counts as unresolvable, so the sniff no longer crashes on it.
  • PropertyTypeHint and DisallowStaticMembers use those checks.
  • tests/fixtures/_ancestors/: test ancestors, autoloaded through a new autoload-dev entry, which is how a consumer's vendor parent loads. CONTRIBUTING.md documents the directory.
  • The four guidelines and the README summaries name each exemption.

Removing each exemption fails its own test

Exemption removed Test that fails New reports
Service provider skips a *ServiceProvider class declared directly in App\Providers AppServiceProvider (line 8) and the rest
get…Attribute skips a boolean get{Name}Attribute accessor… (both datasets) getHasDetailsAttribute() (line 14) and the rest
Inherited untyped property skips a property whose nearest ancestor declaration is untyped… Post::$group (line 15) and the rest
Inherited static property skips every static a Nova-shaped vendor parent declares or reads… lines 15, 22, 24 ($group, $title, $search)
Static read (crossbible) the same test line 20 ($model)

Three guards were also mutation-checked: the declared-ancestor guard in the read check, the catch around class_exists, and the class-only check for accessors. Each one reddens its test.

Acceptance Criteria

Service providers

  • A *ServiceProvider class with a prefix, declared directly in App\Providers, is silent. The namespace check ignores case and no base class is required (service-providers.php).
  • PaymentProvider, a bare ServiceProvider, App\Providers\Billing\BillingServiceProvider and App\Services\BillingService still report, each with a fixture (service-provider-lookalikes.php).
  • The existing RedundantNamespaceSuffix tests pass unmodified.

Eloquent accessors

  • A get{Name}Attribute method on a model is silent for bool, ?bool and @return bool.
  • Imported, aliased and fully qualified Model parents and a Models namespace segment each have a fixture, with no Laravel app needed. The model test is shared with ModelNamingConventions.
  • The accessor name on a non-model, getHasDetails(), getAttribute() and a lowercase gethasOwnerAttribute() still report.
  • ModelNamingConventions still reports LegacyAttributeAccessor on every skipped accessor, and a test asserts it.

Inherited untyped properties

  • An untyped, non-private nearest ancestor declaration silences MissingAnyTypeHint and MissingNativeTypeHint, for static and instance properties.
  • This holds two levels up (Post extends AppResource extends VendorResource, and Member extends User extends Model), with the ancestors loaded through Composer.
  • No fixture property needs a docblock.
  • An undeclared property, a private nearest declaration, and a typed nearest declaration over a private untyped one still report.
  • An unresolvable parent, and a parent whose load throws, report as before with no warning or crash.
  • DisallowStaticMembers is silent on the crossbible-shaped resource, still reports $ownCache, and keeps the typed-static exemption (Labelled).
  • The crossbible cause is named above. Its fixture reports on main and is silent here.

Across all three

  • Sniff names and error codes are unchanged.
  • Removing each exemption fails its own test (table above).
  • The four guideline files name each exemption and its condition.
  • composer test (2919 passed) and composer lint:self both exit 0 locally. composer lint also exits 0.

Test Plan

  • All existing tests pass
  • New tests cover the changes
  • Ran crossbible's own phpcs to confirm the cause (read only, nothing in crossbible changed)

Fixes #391

RedundantNamespaceSuffix skips a *ServiceProvider class declared
directly in App\Providers. BooleanGetMethodName skips a get{Name}Attribute
accessor on an Eloquent model, using a model test shared with
ModelNamingConventions through the new EloquentModels helper.

PropertyTypeHint skips a property whose nearest ancestor declaration is
untyped and not private. DisallowStaticMembers also skips a static
property no ancestor declares but an ancestor reads as static::$name.
Nova 5's Resource reads static::$model that way and never declares it,
which is why the existing exemption missed it on crossbible.

Fixes: #391
@mikebronner
mikebronner marked this pull request as ready for review October 5, 2026 18:29

@mr-sherlock-holmes mr-sherlock-holmes Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved

Review Summary

  • Reviewed the four exemptions, the shared EloquentModels helper, the nearest-declaration walk and the static::$name read check in InheritedMembers, plus the fixtures, tests and guidelines.
  • All 18 acceptance criteria are met. This repo has no CI, so I ran the checks on the PR head: composer test passed 2919 tests and composer lint:self exited 0.
  • Everything in the unit is clean. The model test moved into the helper unchanged, so ModelNamingConventions and BooleanGetMethodName cannot disagree.

Two divergences from the AC wording, both met by intent

  • AC 3f/3g, the crossbible miss. The AC describes the shape as overriding a static that a vendor parent declares. Nova 5 declares no $model and only reads static::$model. Silencing a static that an ancestor reads but never declares is what fixes the miss. $ownCache still reports, so a static that no ancestor declares or reads still fails.
  • AC 3a, $model. The AC lists $model as covered, but its rule is "nearest ancestor declaration is untyped". Nova 5 has no declaration, so public static string $model is legal and the report has a code fix. Keeping that report satisfies the rule's intent, and inherited-reported.php pins it.

Claims I checked and dropped

  • The typed-over-untyped fixture uses a private grandparent. That is the only legal shape: PHP refuses a typed property over a non-private untyped ancestor ("Type of P::$count must be omitted").
  • protected $a, $b; checks only $a. Slevomat's parent already handles only the first variable, so no report is lost.
  • The file_get_contents read in staticReadsIn has no readability guard. A real run cannot reach that path, because Composer loads an ancestor only from a readable file.
  • The static-read scan covers the whole ancestor file. A false match needs a second class in the file, which breaks PSR-4.

📋 Non-blocking follow-ups

  • None.

Ready for @mikebronner to merge.

@mikebronner
mikebronner merged commit 9402b4a into main Oct 5, 2026
0 of 3 checks passed
@mikebronner
mikebronner deleted the feature/391-exempt-names-and-declarations-that-laravel-and-nova branch October 5, 2026 19:34
mikebronner added a commit that referenced this pull request Oct 5, 2026
Resolving the conflict in TypeHintsInheritanceTest.php dropped the `);`
that ends the last PropertyTypeHint test and left a whitespace-only
line. Pest stopped on the ParseError before running any test, and
`composer lint` reported the trailing whitespace.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exempt names and declarations that Laravel and Nova dictate

1 participant