kero/typesafe-env

Wrapper around Laravel's Env to support type safety.

0.1.0 2023-11-02 08:22 UTC

This package is auto-updated.

Last update: 2024-05-01 00:17:36 UTC


README

Install via composer req kero/typesafe-env

GitHub latest tag codecov license: MIT

Usage

Assuming the following environment variables

API_URL=foo
APP_STRING_OR_NULL=null

APP_DEBUG=true
APP_LOG=false
APP_BOOL_OR_NULL=null

APP_PI=3

APP_BETTER_PI=3.14
use Kero\TypeSafeEnv\Env;

var_dump(Env::getString('API_URL')); // string(3) "foo"
var_dump(Env::getNullableString('APP_STRING_OR_NULL')); // NULL

var_dump(Env::getBool('APP_DEBUG')); // bool(true)
var_dump(Env::getBool('APP_LOG')); // bool(false)
var_dump(Env::getNullableBool('APP_BOOL_OR_NULL')); // NULL

var_dump(Env::getInt('APP_PI')); // int(3)

var_dump(Env::getFloat('APP_PI')); // float(3.14)

❗ Due to Laravel's implementation, both values null and (null) are treated as NULL.

Local Development

Code Style

Currently using plain PSR-12 via Laravel Pint. Apply the code style via

make pint

Tests

Using Pest for testing. Run existing tests via

make test

❗ Tests are relying on the environment variables defined in .env.testing

Type Coverage can also be tested via Pest. Current implementation inside Makefile is buggy ... (TODO: fix so command in Makefile via Docker can be used)

./vendor/bin/pest --type-coverage --min=100

Code Analysis

PHPStan is configured to check ./src/ and ./tests/ via

make analyse

Various

composer normalize