crumbls/laravel-detect-unused-files

A Laravel package to detect unused files in your project

0.0.1 2025-05-20 17:39 UTC

This package is auto-updated.

Last update: 2025-05-21 15:29:06 UTC


README

This package helps identify unused PHP files in your Laravel application by tracking which files are actually used during application execution.

Why does this exist?

I was asked to help migrate part of a huge app to a smaller app. Copying and pasting created tons of artifacts and unused files. We didn't want to expose anything that isn't critical to this app, so I created this package to find unused app PHP files.

Latest Version on Packagist Total Downloads

⚠️ Development Use Only

Warning: This package is intended for development environments only. It adds additional database queries and file tracking operations that may impact application performance in production.

Requirements

  • PHP 8.3 or higher
  • Laravel 12.0 or higher
  • A Laravel application with a database connection configured

Installation

You can install the package via composer:

composer require crumbls/laravel-detect-unused-files --dev

After installing the package, you need to run the migrations:

php artisan migrate

This will create the necessary file_usage table in your database to track file usage.

How It Works

This package works by:

  1. Tracking all files in your application's app directory
  2. Recording which files are actually loaded during application execution
  3. Allowing you to query for files that have never been loaded (potentially unused)

Usage

Step 1: Add App Files to Tracking

First, add all your application's PHP files to the tracking system:

php artisan files:add-app

This command will scan your app directory for PHP files and add them to the tracking database.

Step 2: Use Your Application

For accurate results, you should use your application normally after running the above command. Browse through different pages and features of your application to ensure that all necessary files are loaded.

The more comprehensive your browsing, the more accurate the results will be. Try to cover:

  • All major features
  • Admin areas
  • Different user roles
  • API endpoints
  • Console commands
  • Scheduled tasks

Step 3: List Unused Files

After using your application, you can check for unused files:

php artisan files:list-unused

This will display a list of PHP files that were never loaded during your application usage.

Additional Options

You can filter the results with various options:

# Limit the number of results
php artisan files:list-unused --limit=10

# Filter by a filename pattern
php artisan files:list-unused --pattern=Controller

# Only show files added more than X days ago
php artisan files:list-unused --days=7

Best Practices

For the most accurate results:

  • Run this in a development environment that mimics your production setup
  • Execute all your application's automated tests to cover edge cases
  • Consider running the tracker over multiple days of normal development use
  • Re-run files:add-app periodically to capture newly added files

How The Package Tracks Files

The package performs the following operations:

  1. Records all PHP files in your app directory
  2. Uses PHP's get_included_files() function to track file usage during request execution
  3. Updates the database on application shutdown to mark loaded files as "used"
  4. Provides commands to query the database for unused files

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

The MIT License (MIT). Please see License File for more information.

Credits