Package Data | |
---|---|
Maintainer Username: | imanghafoori1 |
Maintainer Contact: | imanghafoori1@gmail.com (Iman) |
Package Create Date: | 2020-03-16 |
Package Last Update: | 2024-09-21 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-15 15:23:51 |
Package Statistics | |
---|---|
Total Downloads: | 468,611 |
Monthly Downloads: | 8,017 |
Daily Downloads: | 287 |
Total Stars: | 1,477 |
Total Watchers: | 23 |
Total Forks: | 96 |
Total Open Issues: | 9 |
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.
Stargazers: https://github.com/imanghafoori1/microscope/stargazers
You can install the package via composer:
composer require imanghafoori/laravel-microscope --dev
Although this project has already a lot of features, but it is still under active development, so you have to update it almost everyday in order to get the latest improvements and bug fixes.
composer update imanghafoori/laravel-microscope
You can run:
Also You will have access to some global helper functions:
In case you wonder what are the listeners and where are they?!
You can use this (0_o) microscope_dd_listeners(MyEvent::class);
This call, also can be in boot
or register
as well.
And it works like a normal dd(...);
meaning that it will halt.
Lets start with:
php artisan check:early_returns
This will scan all your Psr-4 loaded classes and flattens your functions ans loops by applying the early return rule. For example:
<?php
forearch ($products as $product) {
if ($someCond) {
// A lot of code 1
// A lot of code 1
// A lot of code 1
// A lot of code 1
// A lot of code 1
if ($someOtherCond) {
// A lot more code 2
// A lot more code 2
// A lot more code 2
// A lot more code 2
// A lot more code 2
//
} // <--- closes second if
} // <--- closes first if
}
Will be discovered and converted into:
<?php
forearch ($products as $product) {
if (! $someCond) {
continue;
}
// A lot of code 1
// A lot of code 1
// A lot of code 1
// A lot of code 1
// A lot of code 1
if (! $someOtherCond) {
continue;
}
// A lot more code 2
// A lot more code 2
// A lot more code 2
// A lot more code 2
// A lot more code 2
}
The same thing will apply for functions and methods, but with return
<?php
if ($var1 > 1) {
if ($var2 > 2) {
echo 'Hey Man';
}
}
// will be converted into:
if ($var1 > 1 && $var2 > 2) {
echo 'Hey Man';
}
<?php
if ($var1 > 1):
if ($var2 > 2):
echo 'Hey Man';
endif;
endif;
// or if you avoid putting curly braces...
if ($var1 > 1)
if ($var2 > 2)
echo 'Hey Man';
Although this type of refactor is totally safe and is guaranteed to do the same thing as before, but anyway be careful to commit everything before trying this feature, in case of a weird bug or something.
php artisan check:events
For example consider:
Event::listen(MyEvent::class, '\App\Listeners\MyListener@myMethod');
1 - It checks the \App\Listeners\MyListener
class path to be valid.
2 - It checks the myMethod
to exist on the MyListener
class
3 - It checks the myMethod
to have the right type-hint (if any) in its signature, for example:
public function myMethod(OtherEvent $e) // <---- notice type-hint here
{
//
}
This is a valid but wrong type-hint, and will be reported to you. Very cool, isn't it ??!
1- in the EventServiceProvider
,
2- By Event::listen
facade,
3- By Subscriber class... or any other way. The error would be found. :)
php artisan check:gates
It checks the validity of all the gates you have defined, making sure that they refer to a valid class and method.
It also checks for the policy definitions to be valid.
Gate::policy(User::class, 'UserPolicy@someMethod');
Gate::define('someAbility', 'UserGate@someMethod');
1 - It checks the User
class path to be valid.
2 - It checks the UserPolicy
class path to be valid.
3 - It checks the someMethod
to exist.
php artisan check:psr4
php artisan check:generate
You make empty file, we fill it, based on naming conventions.
If you create an empty .php
file which ends with ServiceProvider.php
after running this command:
1 - It will be filled with boiler plate and correct Psr-4 namespace.
2 - It will be appnded to the providers
array in the config/app.php
php artisan check:imports
use
statements) to be valid and reports invalid ones.use Request;
would be valid.php artisan check:bad_practices
env()
calls outside of the config files.php artisan check:routes
route()
, redirect()->route()
, \Redirect::route()
to refer to valid routes.dead controllers
are detected.php artisan check:compact
compact()
calls and reports to you, which parameters should be removed.php artisan check:blade_queries
Eloquent models
and DB
query builder and shows them if any.php artisan check:extract_blades
@include('myPartials.someFile')
you can use {!! extractBlade('myPartials.someFile') !!}
in your blade files to indicate start/end line
and the path/name
of the partial you intend to be made.
<html>
{!! extractBlade('myPartials.head') !!}
<head>...</head>
{!! extractBlade() !!}
{!! extractBlade('myPartials.body') !!}
<body>...</body>
{!! extractBlade() !!}
</html>
After you execute php artisan check:extract_blades
it will become:
<html>
@include('myPartials.head')
@include('myPartials.body')
</html>
Also it will create:
resources/views/myPartials/head.blade.php
resources/views/myPartials/body.blade.php
and put the corresponding content in them.
'MyMod::myPartials.body'
php artisan check:action_comments
php artisan pp:route
microscope_pretty_print_route('my.route.name');
php artisan check:views
view()
and View::make()
and reports if they refer to wrong files.@include()
and @extends()
and reports if they refer to wrong files.Also, it can detect unused variables
which are passed into your view from the controller line this: view('hello', [...]);
For that you must open up the page in the browser and then visit the log file to see a message like this:
local.INFO: Laravel Microscope: The view file: welcome.index-1 at App\Http\Controllers\HomeController@index has some unused variables passed to it:
local.INFO: array ('$var1' , '$var2');
Remember some variables are passed into your view from a view composer
and not the controller.
Those variables are also taken into consideration when detecting unused variables.
and more features will be added soon. ;)
The MIT License (MIT). Please see License File for more information.
If you find an issue, or have a better way to do something, feel free to open an issue , or a pull request. If you use laravel-microscope in your open source project, create a pull request to provide it's url as a sample application in the README.md file.
If you discover any security related issues, please email imanghafoori1@gmail.com
instead of using the issue tracker.
:gem: It allows us to write expressive code to authorize, validate and authenticate.
:gem: A minimal yet powerful package to give you the opportunity to refactor your controllers.
:gem: It allows you to login with any password in the local environment only.
:gem: It allows you to decouple your eloquent models to reach a modular structure
If you think that my work has saved you a lot of time hence a lot of money, please take your time and send me 1 dollar, I appritiate it, a lot... (a single dollar is enough, please so do not send more.)
You can contact me at telegram, after donation: https://t.me/imanghafoori so I can put your logo and name on the readme file.
I would be happy to answer you.
return abort();
A man will never fail, unless he stops trying.
Albert einstein