Download the PHP package pinkary-project/type-guard without Composer
On this page you can find all versions of the php package pinkary-project/type-guard. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download pinkary-project/type-guard
More information about pinkary-project/type-guard
Files in pinkary-project/type-guard
Package type-guard
Short Description Type Guard module is part of the Gloss PHP, and allows you to narrow down the type of an variable to a more specific type.
License MIT
Informations about the package type-guard
This library is a work in progress. Please, do not use it in production.
Type Guard module is part of the Pinkary Project, and allows you to narrow down the type of a variable to a more specific type. Using the type
function, you can perform specific checks to determine the type of an object and then use that object in a way that is type-safe according to the PHPStan and Psalm static analyzers.
Here is an example, where we use the type
function to narrow down the type of a variable that previously had a mixed
type:
Here is another example, where we use the type
function to narrow down the type of a variable that previously could be null
. In the process, zero type information is lost:
And one more example, where we narrow down the type of a variable to a Collection without losing the type information:
Installation
Requires PHP 8.2+
You may use Composer to install Type Guard into your PHP project:
Usage
as
asInt()
asFloat()
asString()
asBool()
asNull()
asCallable()
not()->null()
asArray()
asIterable()
as
Asserts and narrows down the type of the given variable to a more specific type.
asInt()
Asserts and narrows down the type of the given variable to an integer.
asFloat()
Asserts and narrows down the type of the given variable to a float.
asString()
Asserts and narrows down the type of the given variable to a string.
asBool()
Asserts and narrows down the type of the given variable to a boolean.
asNull()
Asserts and narrows down the type of the given variable to a null.
asCallable()
Asserts and narrows down the type of the given variable to a callable.
not()->null()
Asserts and narrows down the type of the given variable to a non-null value.
asArray()
Asserts and narrows down the type of the given variable to an array.
asIterable()
Asserts and narrows down the type of the given variable to an iterable.
Type Guard is part of the Pinkary Project project. It was created by Nuno Maduro and open-sourced under the MIT license.