PHPackages                             juampi92/phpstan-eloquent-bounded-context - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. juampi92/phpstan-eloquent-bounded-context

ActivePhpstan-extension[Database &amp; ORM](/categories/database)

juampi92/phpstan-eloquent-bounded-context
=========================================

PHPStan rules that make sure your models are only being mutated from within its Domain.

v1.1(3y ago)08MITPHPPHP ^7.4|^8.0|^8.1

Since Sep 26Pushed 3y ago1 watchersCompare

[ Source](https://github.com/juampi92/phpstan-eloquent-bounded-context)[ Packagist](https://packagist.org/packages/juampi92/phpstan-eloquent-bounded-context)[ RSS](/packages/juampi92-phpstan-eloquent-bounded-context/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (5)Versions (3)Used By (0)

PHPStan Extension: Eloquent Bounded Context
===========================================

[](#phpstan-extension-eloquent-bounded-context)

A set of PHPStan rules to make sure your models are **only being mutated from within their Domains**.

 [![Latest Version on Packagist](https://camo.githubusercontent.com/c699916dd9d69d4885a1de8b247d5ed5b54538bc0f18d58491ff23b4d8ddc834/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a75616d706939322f7068707374616e2d656c6f7175656e742d626f756e6465642d636f6e746578742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/juampi92/phpstan-eloquent-bounded-context) [![GitHub Tests Action Status](https://camo.githubusercontent.com/815ba0f7b2aa2d8c6e8634d374702121e356b3f348cbd9b31079323ea08fee5a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6a75616d706939322f7068707374616e2d656c6f7175656e742d626f756e6465642d636f6e746578742f72756e2d74657374733f6c6162656c3d7465737473)](https://github.com/juampi92/phpstan-eloquent-bounded-context/actions?query=workflow%3Arun-tests+branch%3Amain) [![Total Downloads](https://camo.githubusercontent.com/26ff88d4a759d0646acd27a793ac083a06d3f5c092af32d3ebb2a6818221eaa8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a75616d706939322f7068707374616e2d656c6f7175656e742d626f756e6465642d636f6e746578742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/juampi92/phpstan-eloquent-bounded-context)

Description
-----------

[](#description)

These rules will detect when your Eloquent Model is being mutated outside of its Domain. The moment the Model leaves the Domain namespace, it becomes read-only.

Let's assume the following structure:

```
📁 app
├─ 📁 Http
│  └─ 📁 Controllers
│     └─ 📃 PostController.php
└─ 📁 Domains
   └─ 📁 Posts
      └─ 📁 Actions
         └─ 📃 CreatePostAction.php
      └─ 📁 Models
         └─ 📃 Post.php
         └─ 📃 Comment.php

```

If `app/Http/Controllers/PostController.php` has the following method:

```
public function store(Request $request)
{
    $post = new Post($request->validated());
    $post->save();
}
```

PHPStan will fail saying:

```
 ---------------------------------------------------------------------------
  app/Http/Controllers/PostController.php
 ---------------------------------------------------------------------------
  Calling 'save' on 'App\Models\Post' outside of its Domain is not allowed.
 ---------------------------------------------------------------------------

```

> To fix this error, instead of calling `save` inside the Controller directly, create an Action inside the Post domain (or a PostService class) and use that class to create and persist the model.

### This package will also detect:

[](#this-package-will-also-detect)

- Model attribute mutations like: `$post->title = 'My title';`,
- Methods persist the data in the database, like `save`, `update`, `delete`, etc,
- Static methods that persist the data, like `::create()`, `::updateOrCreate()`, etc.

Installation
------------

[](#installation)

To use this extension, require it in [Composer](https://getcomposer.org/):

```
composer require --dev juampi92/phpstan-eloquent-bounded-context
```

If you also install [phpstan/extension-installer](https://github.com/phpstan/extension-installer) then you're all set!

 Manual installationIf you don't want to use `phpstan/extension-installer`, include extension.neon in your project's PHPStan config:

```
includes:
    - vendor/juampi92/phpstan-eloquent-bounded-context/extension.neon
```

Configuration
-------------

[](#configuration)

### No configuration

[](#no-configuration)

If your models are placed inside their Domain folder, the package will know they are not using Laravel's default folders, and assume the domain is `App/Domains/Posts`. Any class inside that domain is allowed to mutate an eloquent model.

```
📁 app
├─ 📁 Domains
│  └─ 📁 Posts
│  |  └─ 📁 Actions
│  |  └─ 📁 Repositories
│  |  └─ 📁 Models ✅
│  |     └─ 📃 Post.php
│  |     └─ 📃 Comment.php
│  └─ 📁 Users
│     └─  ...
└── ...

```

### Manual configuration

[](#manual-configuration)

If your models are placed inside `App\Models`, then you will have to configure your domain manually. To do so, you must first create a configuration file that holds the information about your models and domains:

```
domains:
  -
    namespace: App\Domain\Posts
    models:
      - App\Models\Post
      - App\Models\Comment
  -
    namespace: App\Domain\Users
    models:
      - App\Models\User
      - App\Models\UserProfile
```

And after you must reference this file inside the `phpstan.neon.dist` config:

```
parameters:
	eloquentBoundedContextConfigFiles:
	    - app/Domain/domains.yml
```

### Configuring ignored namespaces

[](#configuring-ignored-namespaces)

You might want to ignore some namespaces and allow Models to be modified there.

```
parameters:
	eloquentBoundedContextConfigFiles:
		- app/Domain/domains.yml
	eloquentBoundedContextIgnoredNamespaces:
		- Database\Factories
		- Tests
```

Contribute
----------

[](#contribute)

### Features to do:

[](#features-to-do)

- Ignore no-configuration resolution when a config is used.
- Propose /\*\* @mutates \*/ tag or #Attribute to prohibit these methods being called outside of the domain.

Testing
-------

[](#testing)

```
./vendor/bin/phpunit
```

Credits
-------

[](#credits)

- [Juan Pablo Barreto](https://github.com/juampi92)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

1320d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1f8d31c9bb6d7862e21aac7af0f7b2615dc06485b4c30b19b8d9fdffaa42c712?d=identicon)[juampi92](/maintainers/juampi92)

---

Top Contributors

[![juampi92](https://avatars.githubusercontent.com/u/2080215?v=4)](https://github.com/juampi92 "juampi92 (22 commits)")

---

Tags

PHPStaneloquentdddbounded-context

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/juampi92-phpstan-eloquent-bounded-context/health.svg)

```
[![Health](https://phpackages.com/badges/juampi92-phpstan-eloquent-bounded-context/health.svg)](https://phpackages.com/packages/juampi92-phpstan-eloquent-bounded-context)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k7.2M71](/packages/mongodb-laravel-mongodb)[cviebrock/eloquent-sluggable

Easy creation of slugs for your Eloquent models in Laravel

4.0k13.6M251](/packages/cviebrock-eloquent-sluggable)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k25.2M34](/packages/kirschbaum-development-eloquent-power-joins)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[watson/rememberable

Query caching for Laravel

1.1k5.2M13](/packages/watson-rememberable)[watson/validating

Eloquent model validating trait.

9723.3M46](/packages/watson-validating)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
