Skip to content

Instantly share code, notes, and snippets.

@strebl
Last active September 21, 2015 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save strebl/ed62ab3d1ad101e0fcd8 to your computer and use it in GitHub Desktop.
Save strebl/ed62ab3d1ad101e0fcd8 to your computer and use it in GitHub Desktop.
Convert Laravel class strings to PHP 5.5 class constants using PhpStorm / Sublime Text / Regex

How-to

Replace the old class strings 'Illuminate\Foundation\Providers\ArtisanServiceProvider', with the new class constants Illuminate\Foundation\Providers\ArtisanServiceProvider::class, with following steps:

  1. Open an editor with a Regex search and replace function
  2. Search for '((?:Illuminate|App)\\.+)',
  3. Replace it with $1::class,

Be careful with that in files with namespaces. It most likely will break your code.

If you have namespace App\Console and a lot class strings like:

    'App\Console\Command1',
    'App\Console\Command2',
    'App\Console\Command3',

You can use following search term: '(?:Illuminate|App\\Console\\)(.+)', with the normal replace term: $1::class,

This will result in:

    'Command1::class',
    'Command2::class',
    'Command3::class',
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment