PHPackages                             shan/artisan-refactor - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. shan/artisan-refactor

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

shan/artisan-refactor
=====================

Rename PHP classes in Laravel and automatically update all references across the project.

v1.0.0(2w ago)01↓100%MITPHPPHP ^8.1CI failing

Since May 20Pushed 2w agoCompare

[ Source](https://github.com/Randriantahina/laravel-refactor-package)[ Packagist](https://packagist.org/packages/shan/artisan-refactor)[ Docs](https://github.com/Randriantahina/laravel-refactor-package)[ RSS](/packages/shan-artisan-refactor/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (6)Versions (3)Used By (0)

laravel-refactor
================

[](#laravel-refactor)

Rename or move PHP classes in your Laravel project and automatically update every reference across PHP files, Blade templates, routes, config files, and service providers.

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

[](#installation)

```
composer require shan/artisan-refactor --dev
```

Laravel auto-discovers the service provider. Optionally publish the config:

```
php artisan vendor:publish --tag=laravel-refactor-config
```

Usage
-----

[](#usage)

### Rename a class

[](#rename-a-class)

```
php artisan refactor:rename "App\Models\User" "App\Models\Member"
```

### Move a class to a new namespace

[](#move-a-class-to-a-new-namespace)

```
php artisan refactor:move "App\Models\User" "App\Domain\Users\User"
```

### Preview changes without applying (dry-run)

[](#preview-changes-without-applying-dry-run)

```
php artisan refactor:rename "App\Models\User" "App\Models\Member" --dry-run
```

### Undo the last operation

[](#undo-the-last-operation)

```
php artisan refactor:rename "App\Models\User" "App\Models\Member" --rollback
```

### Override a naming conflict

[](#override-a-naming-conflict)

```
php artisan refactor:rename "App\Models\User" "App\Models\Member" --force
```

What gets updated
-----------------

[](#what-gets-updated)

Reference typeExample`use` statements`use App\Models\User;``new` instantiations`new User()``extends` / `implements``class Admin extends User`Type hints &amp; return types`function store(User $user): User`Static calls`User::find(1)``::class` constant`User::class`Service provider bindings`$app->bind(User::class, ...)`Route controllers`[UserController::class, 'index']`Blade `@inject``@inject('u', 'App\Models\User')`Quoted FQCN strings in Blade`'App\Models\User'`Configuration
-------------

[](#configuration)

```
// config/laravel-refactor.php

return [
    'scan_paths' => [
        'app',
        'routes',
        'config',
        'resources/views',
        'database',
        'tests',
    ],

    'excluded_paths' => [
        'vendor',
        'node_modules',
        'storage',
        'bootstrap/cache',
    ],

    'backup_path' => 'storage/app/refactor-backups',
];
```

How it works
------------

[](#how-it-works)

1. **Validates** that the source class exists via PSR-4 mappings in `composer.json`
2. **Detects conflicts** — errors out if the target class already exists
3. **Scans** all configured paths using an AST parser ([nikic/php-parser](https://github.com/nikic/PHP-Parser)) for PHP files and regex for Blade templates
4. **Snapshots** all affected files for rollback
5. **Updates** all references in-place
6. **Renames/moves** the source file and updates its `namespace` / `class` declaration
7. Runs `composer dump-autoload` automatically

Error: class already exists
---------------------------

[](#error-class-already-exists)

```
ERROR: Class App\Models\Member already exists at app/Models/Member.php
Rename aborted. Use --force to override (dangerous).

```

Testing locally before publishing
---------------------------------

[](#testing-locally-before-publishing)

You can test the package against a real Laravel project before pushing to Packagist.

### 1. Create a fresh Laravel project (or use an existing one)

[](#1-create-a-fresh-laravel-project-or-use-an-existing-one)

```
composer create-project laravel/laravel my-test-app
cd my-test-app
```

### 2. Point Composer at your local package

[](#2-point-composer-at-your-local-package)

Add a `path` repository to `composer.json` in the Laravel project:

```
{
    "repositories": [
        {
            "type": "path",
            "url": "../laravel-refactor"
        }
    ]
}
```

The `url` is the path from the Laravel project to the package directory — adjust it to match your folder layout.

### 3. Install the package

[](#3-install-the-package)

```
composer require shan/artisan-refactor:@dev --dev
```

Composer creates a symlink to your local package. Any change you make in `laravel-refactor/src/` is immediately reflected — no need to reinstall.

### 4. Create test fixtures

[](#4-create-test-fixtures)

Create a few classes and references to exercise the different scenarios:

```
# Source class
php artisan make:model User

# A class that references it
php artisan make:controller UserController
```

Open `app/Http/Controllers/UserController.php` and add a reference:

```
use App\Models\User;

class UserController extends Controller
{
    public function index(): User
    {
        return new User();
    }
}
```

Optionally add a Blade template:

```
echo "@inject('user', 'App\Models\User')" > resources/views/users/show.blade.php
```

### 5. Run through the checklist

[](#5-run-through-the-checklist)

**Dry-run first — never skip this step:**

```
php artisan refactor:rename "App\Models\User" "App\Models\Member" --dry-run
```

Expected output: a list of every file and line that would change. No files are modified.

**Apply the rename:**

```
php artisan refactor:rename "App\Models\User" "App\Models\Member"
```

Check that:

- `app/Models/User.php` no longer exists
- `app/Models/Member.php` exists with `class Member` and `namespace App\Models`
- `app/Http/Controllers/UserController.php` now has `use App\Models\Member` and `Member` everywhere
- `resources/views/users/show.blade.php` references `App\Models\Member`
- `composer dump-autoload` ran automatically (verify with `php artisan tinker` → `App\Models\Member::class`)

**Roll back:**

```
php artisan refactor:rename "App\Models\User" "App\Models\Member" --rollback
```

Check that all files are back to their original state.

**Move (namespace change):**

```
php artisan refactor:move "App\Models\Member" "App\Domain\Users\Member"
```

Check that:

- `app/Domain/Users/Member.php` exists (directory was created)
- `namespace App\Domain\Users;` is in the file
- `app/Http/Controllers/UserController.php` imports `use App\Domain\Users\Member`

**Conflict detection:**

```
php artisan refactor:rename "App\Models\Post" "App\Models\Member"
```

Expected: an error saying `Member` already exists. Then:

```
php artisan refactor:rename "App\Models\Post" "App\Models\Member" --force
```

**Publish and customise config:**

```
php artisan vendor:publish --tag=laravel-refactor-config
```

Open `config/laravel-refactor.php` and add a path to `scan_paths` (e.g. `'src'`). Re-run a dry-run to confirm the new path is scanned.

### 6. Run the package's own test suite

[](#6-run-the-packages-own-test-suite)

Back in the `laravel-refactor` directory:

```
./vendor/bin/pest --no-coverage
```

All 79 tests should be green before publishing.

---

Requirements
------------

[](#requirements)

- PHP 8.1+
- Laravel 10, 11, 12, or 13

License
-------

[](#license)

MIT

###  Health Score

38

—

LowBetter than 83% of packages

Maintenance96

Actively maintained with recent releases

Popularity2

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity43

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

Unknown

Total

1

Last Release

20d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/edc187beeb9e2101ca46630823e7f5745008b7966d8d9206318ec47f7ab2e660?d=identicon)[Randriantahina](/maintainers/Randriantahina)

---

Top Contributors

[![Randriantahina](https://avatars.githubusercontent.com/u/170296429?v=4)](https://github.com/Randriantahina "Randriantahina (21 commits)")

---

Tags

laravelclassnamespacerefactorrename

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/shan-artisan-refactor/health.svg)

```
[![Health](https://phpackages.com/badges/shan-artisan-refactor/health.svg)](https://phpackages.com/packages/shan-artisan-refactor)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3325.1M337](/packages/psalm-plugin-laravel)[laravel/ai

The official AI SDK for Laravel.

9782.1M153](/packages/laravel-ai)[livewire/flux

The official UI component library for Livewire.

9466.8M119](/packages/livewire-flux)[zidbih/laravel-deadlock

Make temporary Laravel workarounds expire and fail CI when ignored.

954.0k](/packages/zidbih-laravel-deadlock)[laravel/surveyor

Static analysis tool for Laravel applications.

8390.3k12](/packages/laravel-surveyor)[interaction-design-foundation/laravel-geoip

Support for multiple Geographical Location services.

19253.0k3](/packages/interaction-design-foundation-laravel-geoip)

PHPackages © 2026

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