henzeb/enumhancer-ide-helper
Laravel IDE helper for enhanced PHP enums. Improves autocompletion and type hints for Enumhancer features in PhpStorm and similar IDEs, generating stubs/metadata so enum methods, cases, and helpers are easier to discover and use.
Installation:
composer require --dev henzeb/enumhancer-ide-helper
Add to composer.json under autoload-dev:
"autoload-dev": {
"psr-4": {
"": "vendor/"
},
"classmap": ["vendor/henzeb/enumhander-ide-helper/"]
}
Run composer dump-autoload.
First Use Case: Create a basic enum (PHP 8.1+):
// app/Enums/UserRole.php
namespace App\Enums;
enum UserRole: string
{
case ADMIN = 'admin';
case EDITOR = 'editor';
case VIEWER = 'viewer';
}
The package will now provide IDE autocompletion for enum values in real-time.
IDE Integration:
UserRole::ADMIN in controllers, services, or Blade templates—your IDE (PHPStorm, VSCode) will auto-suggest enum values.Type Safety:
// Before
if ($role === 'admin') { ... }
// After
if ($role === UserRole::ADMIN) { ... }
UserRole::ADMIN === 'admin').Database/Validation:
use App\Enums\UserRole;
class User extends Model
{
protected $attributes = [
'role' => UserRole::VIEWER,
];
protected $casts = [
'role' => UserRole::class,
];
}
use App\Enums\UserRole;
use Illuminate\Validation\Rule;
public function rules()
{
return [
'role' => ['required', Rule::in(array_column(UserRole::cases(), 'value'))],
];
}
Localization:
trans() for human-readable labels:
// lang/en/user_roles.php
return [
'admin' => 'Administrator',
'editor' => 'Content Editor',
'viewer' => 'Read-only User',
];
// Usage
trans('user_roles.' . UserRole::ADMIN->value);
enum type in migrations (MySQL) or string with validation (PostgreSQL/SQLite):
Schema::table('users', function (Blueprint $table) {
$table->string('role')->default(UserRole::VIEWER->value);
});
PHP Version:
spatie/enum or myclabs/php-enum as a fallback.IDE Limitations:
php-ide-helper is installed for full autocompletion.composer dump-autoload and restart IDE.Database Schema:
ENUM type is not recommended for enums with >64 values (hard limit).string column with validation for scalability.Backward Compatibility:
// Serialize
$role->value; // Use value, not the enum object
// Deserialize
UserRole::from($serializedValue);
Autoloading Issues:
vendor/autoload-dev.php is included in your IDE’s PHP include path.composer dump-autoload --optimize if autocompletion is missing.Enum Not Recognized:
psr-4 autoloaded directory.Custom IDE Helpers:
@method annotations for static methods:
/**
* @method static UserRole from(string $value)
*/
enum UserRole { ... }
Dynamic Enums:
class DynamicRole extends Enum
{
public static function cases(): array
{
return config('roles.available');
}
}
Testing:
$this->partialMock(UserRole::class, ['from'])
->shouldReceive('from')
->with('invalid')
->andThrow(new \InvalidArgumentException());
Performance:
static function allValues(): array
{
return array_column(self::cases(), 'value');
}
How can I help you explore Laravel packages today?