Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
L

laravel-microscope

> 后端框架
Open source

Fearless refactoring, it does a lot of smart checks to find certain errors.

1.5K stars0 likes0 views
WebsiteGitHub

About

Fearless refactoring, it does a lot of smart checks to find certain errors.

Find Bugs Before They Bite

Built with :heart: for lazy Laravel developers ;)

Why repeat the old errors, if there are so many new errors to commit?

(Bertrand Russel)

Give your eyes a rest, we will detect and fix them for you.

[![Required Laravel Version][ico-laravel]][link-packagist] [![Required PHP Version][ico-php]][link-packagist] [![Latest Version on Packagist][ico-version]][link-packagist] [![Quality Score][ico-code-quality]][link-code-quality] [![Total Downloads][ico-downloads]][link-downloads] [![Today Downloads][ico-today-downloads]][link-downloads]

  • Table Of Contents
    • Key Things To Know

    • Installation

    • Usage

      • Useful Commands
      • Fewer Use Commands
    • What The Commands Do?

      show commands
      1. php artisan search_replace

        • Defining Patterns
        • Placeholders
        • Mutator
        • Filters
        • Capturing Php "statements"
        • Capturing Global Function Calls
        • Repeating Patterns
      2. php artisan check:early_returns

      3. php artisan check:psr4

      4. php artisan check:imports

      5. php artisan check:bad_practices

      6. php artisan check:routes

      7. php artisan check:compact

      8. php artisan check:blade_queries

      9. php artisan check:extract_blades

      10. php artisan check:action_comments

      11. php artisan check:views

      12. php artisan check:gates

      13. php artisan check:aliases

      14. php artisan check:dead_controllers

      15. php artisan check:generic_docblocks

      16. php artisan check:migrations

      17. php artisan check:empty_comment

      18. php artisan enforce:helper_functions

      19. php artisan check:extra_fqcn

      20. php artisan list:models

      21. php artisan check:extra_imports

      22. php artisan enforce:imports

      23. php artisan check:abort_if

  • Credits

  • License

  • Contributing

  • More From The Author

  • Contributors

Key things to know:

  • It is created to be smarter than phpstorm and other IDEs in finding errors.
  • It is created to understand laravel run-time and magic.
  • It does not show you stupid false errors, all the reported cases are really errors.
  • Even If you have written a lot of tests for your app, you may still need this.
  • It can refactor your code, by applying early returns automatically.
  • It is written from scratch to yield the maximum performance possible.

:film_strip: Video tutorial here

:star: Your Stars Make Us Do More

If you found this package useful, and you want to encourage the maintainer to work on it, just press the star button to declare your willingness.

Stargazers

⬇️ Installation

You can install the package via Composer:

bash
composer require imanghafoori/laravel-microscope --dev

You may also publish config file:

php artisan vendor:publish --provider="Imanghafoori\LaravelMicroscope\LaravelMicroscopeServiceProvider"

Usage:

Useful Commands:

You can run :point_down:

# Artisan Command
1 php artisan search_replace
2 php artisan check:early_returns
3 php artisan check:all

Less Used Commands:

# Artisan Command
1 php artisan check:views
2 php artisan check:routes
3 php artisan check:psr4 {-s|--nofix}
4 php artisan check:imports {-s|--nofix}
5 php artisan check:stringy_classes
6 php artisan check:dd
7 php artisan check:bad_practices
8 php artisan check:compact
9 php artisan check:blade_queries
10 php artisan check:action_comments
11 php artisan check:extract_blades
12 php artisan check:abort_if
13 php artisan list:models
14 php artisan check:endif
16 php artisan check:gates
17 php artisan check:dynamic_where
18 php artisan check:aliases
19 php artisan check:dead_controllers
20 php artisan check:generic_docblocks
21 php artisan enforce:helper_functions
22 php artisan enforce:imports
23 php artisan check:fqcn

What do the Commands do?

Let's start with the:

php artisan search_replace {--name=pattern_name} {--tag=some_tag} {--file=partial_file_name} {--folder=partial_folder_name} {--except-folder=} {--except-file=}

This is a smart and very powerful search/replace functionality that can be a real "time saver" for you.

:one: Defining patterns:

If you run the command artisan search_replace for the first time, it will create a search_replace.php file in the project's root. Then, you can define your patterns within that file.

Examples:

Let's define a pattern to replace the optional() global helper with the ?-> PHP 8 null-safe operator:

php
return [
    'optional_to_nullsafe' => [
        'search' => '"<global_func_call:optional>"("<in_between>")->',
        'replace' => '"<2>"?->',
        // 'tag' => 'php8,refactor',
        // 'predicate' => function($matches, $tokens) {...},
        // 'mutator' => function($matches) {...},
        // 'post_replace' => [...],
        // 'avoid_result_in' => [...],
        // 'avoid_syntax_errors' => false,
        // 'filters' => [...],
    ]
];
  • Here, the key optional_to_nullsafe is the "unique name" of your pattern. (You can target your pattern by running php artisan search_replace --name=optional_to_nullsafe)
  • The search pattern has a "<in_between>" placeholder which captures everything in between the pair of parentheses.
  • In the replace block, we substitute what we have captured by the first placeholder with the "<1>". If we have more placeholders, we could have had "<2>" etc.
  • In the tag block, we can mention some tags as an array of strings or a string separated by commas and target them by --tag flag: php artisan search_replace --tag=php8

:two: Placeholders:

Here is a comprehensive list of placeholders you can use:

# Placeholders Description
1 <var> or <variable> for variables like: $user
2 <str> or <string> for hard coded strings: 'hello' or "hello"
3 <class_ref> for class references: \App\User::where(... , User::where
4 <full_class_ref> only for full references: \App\User::
5 <until> to capture all the code until you reach a certain character.
6 <comment> for comments (it does not capture doc-blocks beginning with: /** )
7 <doc_block> for php doc-blocks
8 <statement> to capture a whole php statement.
9 <name:nam1,nam2> or <name> for method or function names. ->where or ::where
10 <white_space> for whitespace blocks
11 <bool> or <boolean> for true or false (acts case-insensitive)
12 <number> for numeric values
13 <cast> for type-casts like: (array) $a;
14 <int> or "<integer>" for integer values
15 <visibility> for public, protected, private
16 <float> for floating point number
17 "<global_func_call:func1,func2>" to detect global function calls
18 <in_between> to capture code within a pair of {...} or (...) or [...]
19 <any> captures any token.

You can also define your own keywords if needed!

You just define a class for your new keyword and append the classpath to the end of the Finder::$keywords[] = MyKeyword::class property. Just like the default keywords.

Example:

:one: Let's say you want to find only the "comments" that contain the word "todo:" in them.

php
 'todo_comments' => [
        'search' => '<comment>',
        'predicate' => function($matches) {    //   <====  here we check comment has "todo:"
            $comment = $matches[0]; // first placeholder value
            $content = $comment[1]; // get its content
            
            return Str::contains($content, 'todo:') ? true : false;
        },
]

Note If you do not mention the 'replace' key, it only searches and reports them to you.

:two: Ok, now let's say you want to remove the "todo:" word from your comments:

php
 'remove_todo_comments' => [
    'search' => '<comment>',      //   <=== we capture any comment
    'replace' => '<1>',

    'predicate' => function($matches) {
        $comment = $matches[0]; // first matched placeholder
        $content = $comment[1];

        return Str::contains($content, 'todo:') ? true : false;
    },

    'mutator' => function ($matches) {       //  <=== here we remove "todo:"
        $matches[0][1] = str_replace('todo:', '', $matches[0][1]);

        return $matches;
    }
]

Converts: // todo: refactor code Into: // refactor code

:three: Mutator: In mutators, you are free to manipulate the $matched values as much as you need to before replacing them in the results. You can also mention a static method instead of a function, like this: [MyClass::class, 'myStaticMethod']

:three: Let's say you want to put the optional comma for the Let's elements in the arrays if they are missing.

php
    'enforce_optional_comma' => [
        'search' => '<white_space>?]',
        'replace' => ',"<1>"]',
        'avoid_syntax_errors' => true,
        'avoid_result_in' => [
           ',,]',
           '[,]',
           '<var>[,]'
       ],
    ]

In this case, our pattern is not very accurate and in some cases, it may result in syntax errors. Because of that, we turn on the PHP syntax validator to check the result, but that costs us a performance penalty!!! To exclude the usage of PHP, to validate the results we have mentioned the avoid_result_in so that if they happen in the result, it skips.

  • Note: The ? in the "<white_space>?" notes this is an optional placeholder.

If you are curious to see a better pattern that does not need any syntax checking, try this:

'enforce_optional_comma' => [
       'search' => '<1:any><2:white_space>?[<3:until_match>]',
       'replace'

Issues· 3 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPlaravel

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架