imanghafoori1 / laravel-microscope by imanghafoori1

Automatically test your laravel application
476,890
1,482
23
Package Data
Maintainer Username: imanghafoori1
Maintainer Contact: imanghafoori1@gmail.com (Iman)
Package Create Date: 2020-03-16
Package Last Update: 2024-12-11
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-12-15 15:23:55
Package Statistics
Total Downloads: 476,890
Monthly Downloads: 8,198
Daily Downloads: 152
Total Stars: 1,482
Total Watchers: 23
Total Forks: 97
Total Open Issues: 9

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 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.

: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: https://github.com/imanghafoori1/microscope/stargazers

⬇️ Installation

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

💎 Usage

You can run:

Also You will have access to some global helper functions:

  • microscope_dd_listeners($event);

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.

📖 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 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';
}

  • It also supports the ruby like if():/endif; syntax;
<?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 ??!

  • 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. :)


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
  • 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)
  • It also checks for references to the old namespace with the system and replaces them with the new one.

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
  • It checks all the imports (use statements) to be valid and reports invalid ones.
  • It auto-corrects some of the refrences, it no ambiguity is around the class name.
  • It can understand the laravel aliased classes so use Request; would be valid.

php artisan check:bad_practices
  • It detects bad practices like env() calls outside of the config files.

php artisan check:routes
  • 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.

php artisan check:blade_queries
  • Blade files should not contain DB queries. we should move them back into controllers and pass variables. This command searches all the blade files for Eloquent models and DB query builder and shows them if any.

php artisan check:extract_blades
  • If you want to extract a blade partial out and make it included like: @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.

  • It is also compatible with namespaced views in modular laravel applications. So this syntax will work: 'MyMod::myPartials.body'

php artisan check:action_comments
  • This adds annotations in the controller actions so that you know which route is pointing to the current controller action.

php artisan pp:route
  • First you have to put this in your route file: microscope_pretty_print_route('my.route.name');
  • You can also pass the Controller@method syntax to the function.
  • You can call it multiple times in otder to pretty-print multiple routes.

php artisan check:views
  • 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.


:raising_hand: 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.

:exclamation: Security

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

More from the author:

Laravel HeyMan

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

  • https://github.com/imanghafoori1/laravel-heyman

Laravel Terminator

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

  • https://github.com/imanghafoori1/laravel-terminator

Laravel AnyPass

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

  • https://github.com/imanghafoori1/laravel-anypass

Eloquent Relativity

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

  • https://github.com/imanghafoori1/eloquent-relativity

🍌 Pay after it pays off:

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.)

  • BitCoin: bc1q53dys3jkv0h4vhl88yqhqzyujvk35x8wad7uf9
  • Etherium: 0xa4898246820bbC8f677A97C2B73e6DBB9510151e
  • USDC: 0xB3F88d2334C9A5eFBe9c0932A969E8a971139547

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.


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();
  • Detect un-registered service providers
  • Detect unused middlewares
A man will never fail, unless he stops trying.

Albert einstein