Fearless refactoring, it does a lot of smart checks to find certain errors.
Fearless refactoring, it does a lot of smart checks to find certain errors.
[![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]
early returns automatically.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.
You can install the package via Composer:
composer require imanghafoori/laravel-microscope --devYou may also publish config file:
php artisan vendor:publish --provider="Imanghafoori\LaravelMicroscope\LaravelMicroscopeServiceProvider"You can run :point_down:
| # | Artisan Command |
|---|---|
| 1 | php artisan search_replace |
| 2 | php artisan check:early_returns |
| 3 | php artisan check:all |
| # | 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 |
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.
If you run the command
artisan search_replacefor the first time, it will create asearch_replace.phpfile 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:
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' => [...],
]
];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)"<in_between>" placeholder which captures everything in between the pair of parentheses.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.--tag flag: php artisan search_replace --tag=php8Here 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::classproperty. Just like the default keywords.
Example:
:one: Let's say you want to find only the "comments" that contain the word "todo:" in them.
'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:
'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.
'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.
? 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'No open issues yet, or sync has not completed.