PHPackages                             plook/type-guard - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. plook/type-guard

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

plook/type-guard
================

Library to ensure correctness of types with a readable interface.

1.1.0(2mo ago)230.2k↑32.4%1[1 PRs](https://github.com/pl-github/php-type-guard/pulls)LGPL-2.1-or-laterPHPPHP ^8.3CI passing

Since Feb 12Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/pl-github/php-type-guard)[ Packagist](https://packagist.org/packages/plook/type-guard)[ RSS](/packages/plook-type-guard/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (21)Versions (14)Used By (0)

Type Guard
==========

[](#type-guard)

[![codecov](https://camo.githubusercontent.com/b5d04b746ac5fe843626f6e8a59a968d62484e11c6f6baa3307d2d4860c82adb/68747470733a2f2f636f6465636f762e696f2f67682f706c2d6769746875622f7068702d545950452d47554152442f67726170682f62616467652e7376673f746f6b656e3d49594e56435851533841)](https://codecov.io/gh/pl-github/php-TYPE-GUARD)

A PHP library to ensure correctness of types providing a readable interface.

Installation
------------

[](#installation)

```
$ composer require plook/type-guard
```

Example
-------

[](#example)

```
use function Plook\TypeGuard\asBool;
use function Plook\TypeGuard\asDateTimeImmutable;
use function Plook\TypeGuard\asFloat;
use function Plook\TypeGuard\asInt;
use function Plook\TypeGuard\asString;
use function Plook\TypeGuard\notNull;

$row = $this->fetchProjectRow(123);

$project = new Project(
     notNull(asInt($row['id'])),
     notNull(asString($row['name'])),
     notNull(asDateTimeImmutable($row['createdAt'])),
     notNull(asBool($row['is_assigned'])),
     asDateTimeImmutable($row['closedAt']),
     asFloat($row['rating']),
);
```

Provided helper functions
-------------------------

[](#provided-helper-functions)

### Ensure Types

[](#ensure-types)

- `asBool($value)` Converts input value to a boolean, but passes `null`.
- `asFloat($value)` Converts input value to a float, but passes `null`.
- `asInt($value)` Converts input value to a int, but passes `null`.
- `asDateTimeImmutable($value)` Converts input value to a `DateTimeImmutable` object, but passes `null`.
- `asDateTimeZone($value)` Converts input value to a `DateTimeZone` object, but passes `null`.
- `asDateTimeString($value)` Converts input value to a date string including the timezone, but passes `null`.
- `asString($value)` Converts input value to a string, but passes `null`.

### Converters

[](#converters)

- `blankAsNull($value)` Converts input value to `null`, if it is a blank string `''`.
- `falseAsNull($value)` Converts input value to `null`, if it is a boolean `false`.
- `falsyAsNull($value)` Converts input value to `null`, if it is a falsy value `false`, `''`, `0`, ...
- `zeroAsNull($value)` Converts input value to `null`, if it is a zero `0` or `0.0`.

### Assertions

[](#assertions)

- `notNull($value)` Throws an exception if the value is `null` otherwise it passes the original value.

Configuration
-------------

[](#configuration)

### Setting the default target time zone of `DateTimeImmutable` objects

[](#setting-the-default-target-time-zone-of-datetimeimmutable-objects)

```
use Plook\TypeGuard\TypeGuard;

TypeGuard::instance()->timeZone('Australia/Adelaide');
TypeGuard::instance()->timeZone(new DateTimeZone('Australia/Adelaide'));
```

### Setting the default format of date time strings

[](#setting-the-default-format-of-date-time-strings)

```
use Plook\TypeGuard\TypeGuard;

TypeGuard::instance()->dateTimeFormat(DateTimeInterface::ATOM);
```

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance88

Actively maintained with recent releases

Popularity32

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 85.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~126 days

Recently: every ~189 days

Total

7

Last Release

66d ago

Major Versions

0.8.0 → 1.0.02024-11-06

PHP version history (2 changes)0.5.0PHP ^8.2

1.0.1PHP ^8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/af5decb62b44eeee60d94d96c0bb22bc160aff4754e5ea1c332789efcd1183e2?d=identicon)[pl-github](/maintainers/pl-github)

---

Top Contributors

[![pl-github](https://avatars.githubusercontent.com/u/1941578?v=4)](https://github.com/pl-github "pl-github (46 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (8 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Rector

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/plook-type-guard/health.svg)

```
[![Health](https://phpackages.com/badges/plook-type-guard/health.svg)](https://phpackages.com/packages/plook-type-guard)
```

###  Alternatives

[webpatser/laravel-uuid

Laravel integration for webpatser/uuid - High-performance drop-in UUID replacements (15% faster than Ramsey). Provides Str macros, HasUuids trait, facades, and casts. RFC 4122/9562 compliant.

1.8k17.3M129](/packages/webpatser-laravel-uuid)[typo3/cms-install

TYPO3 CMS Install Tool - The Install Tool is used for installation, upgrade, system administration and setup tasks.

1811.5M309](/packages/typo3-cms-install)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
