Automatic Code refactor and error detection with laravel-microscope

Automatic Code refactor and error detection with laravel-microscope

https://ift.tt/362PkbG

Find Bugs Before They Bite

Flatten your entire project’s code by a single artisan command

So, Give your eyes a rest, this will check it for you.

widgetize_header

Built with ❤️ for lazy laravel developers 😉

Latest Version on Packagist Build Status Quality Score Daily Downloads Total Downloads Software License

Key things to know:

  • It is create 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 errors are really errors.
  • If you have written a lot of tests for your app, you may not need this.
  • It can refactor your code, by applying early returns automatically.

⬇️ Installation

You can install the package via composer:

composer require imanghafoori/laravel-microscope --dev

💎 Usage

You can run:

🔹 php artisan check:events

🔹 php artisan check:gates

🔹 php artisan check:views

🔹 php artisan check:routes

🔹 php artisan check:psr4

🔹 php artisan check:imports

🔹 php artisan check:stringy_classes

🔹 php artisan check:dd

🔹 php artisan check:early_returns

🔹 php artisan check:compact

🔹 php artisan check:all

📖 What the Commands do?

Lets start with:

php artisan check:early_returns

This will scan all your Psr-4 loaded classes and flattens your functions ans loops by applting the early return rule. For example:

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:

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

if ($var1) {  if ($var2 > 2) {  echo 'Hey Man';  } }  // will be converted into: if ($var1 && $var2 > 2) {  echo 'Hey Man'; } 
  • It also supports the ruby like if():/endif; syntax;
if ($var1):  if ($var2 > 2):  echo 'Hey Man';  endif; endif;

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.


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 ??!

  • Note that it does not matter how you are setting your event listener,

1- in the EventServiceProvider,

2- By Event::listen facade,

3- By Subscriber class… or any other way. The error would be found. 🙂


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.


  • It checks for all the psr4 autoloads defined in the composer.json file and goes through all the classes to have the right namespace, according to PSR-4 standard.
  • It automatically corrects namespaces (according to PSR-4 rules)

php artisan check:imports
  • It checks all the imports (use statements) to be valid.
  • It can understand the laravel aliased classes so use Request; would be valid.

  • It checks that your routes refer to valid controller classes and methods.
  • It checks the all the controller methods to have valid type-hints.
  • It scans for route(), redirect()->route(), \Redirect::route() to refer to valid routes.
  • It will report the public methods of controllers, which have no routes pointing to them. In other words dead controllers are detected.

php artisan check:compact
  • In php 7.3 if you “compact” a non-existent variable you will get an error, so this command checks the entire project for wrong compact() calls and reports to you, which parameters should be removed.

  • It scans your code and find the view() and View::make() and reports if they refer to wrong files.
  • It scans your blade files for @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. 😉

Credits

License

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


🙋 Contributing

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.

❗ Security

If you discover any security related issues, please email imanghafoori1@gmail.com instead of using the issue tracker.

⭐ Your Stars Make Us Do More ⭐

As always if you found this package useful , and you want to encourage us to maintain and work on it. Just press the star button to declare your willing.

Stargazers: https://github.com/imanghafoori1/microscope/stargazers

More from the author:

Laravel HeyMan

💎 It allows us to write expressive code to authorize, validate and authenticate.


Laravel Terminator

💎 A minimal yet powerful package to give you the opportunity to refactor your controllers.


Laravel AnyPass

💎 It allows you to login with any password in the local environment only.


Eloquent Relativity

💎 It allows you to decouple your eloquent models to reach a modular structure


🍌 Reward me a banana 🍌

Send me as much as a banana costs in your region:

  • DogeCoin: DJEZr6GJ4Vx37LGF3zSng711AFZzmJTouN
  • LiteCoin: ltc1q82gnjkend684c5hvprg95fnja0ktjdfrhcu4c4
  • BitCoin: bc1q53dys3jkv0h4vhl88yqhqzyujvk35x8wad7uf9
  • Ripple: rJwrb2v1TR6rAHRWwcYvNZxjDN2bYpYXhZ
  • Etherium: 0xa4898246820bbC8f677A97C2B73e6DBB9510151e

Todo:

  • Detect Bad code
  • Facadize static method calls
  • Detect return keyword in eloquent relations
  • Detect wrong action() calls
  • Enhance blocky code detection
  • Fullly decouple the error logger
  • Detect return abort();
A man will never fail, unless he stops trying. Albert einstein 

programming

via Laravel News Links https://laravel-news.com/links

May 12, 2020 at 09:45PM