PHPackages                             smoren/type-tools - 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. smoren/type-tools

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

smoren/type-tools
=================

Helpers for different operations with PHP data types

v2.1.3(3y ago)7874↑50%32MITPHPPHP &gt;=7.4.0

Since Jan 6Pushed 1y ago1 watchersCompare

[ Source](https://github.com/Smoren/type-tools-php)[ Packagist](https://packagist.org/packages/smoren/type-tools)[ RSS](/packages/smoren-type-tools/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (7)Dependencies (5)Versions (9)Used By (2)

PHP Type Tools
==============

[](#php-type-tools)

[![Packagist PHP Version Support](https://camo.githubusercontent.com/8a937ff515d9e62bb639e452648399eb2a83fba8fac91356167a12e014846970/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f736d6f72656e2f747970652d746f6f6c73)](https://camo.githubusercontent.com/8a937ff515d9e62bb639e452648399eb2a83fba8fac91356167a12e014846970/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f736d6f72656e2f747970652d746f6f6c73)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/f6f2489f806034a2d64a6facf4484fa151ae85a789160ae9c2f7155b4934c1d1/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f536d6f72656e2f747970652d746f6f6c732d7068702f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Smoren/type-tools-php/?branch=master)[![Coverage Status](https://camo.githubusercontent.com/3f488877ae10294876c0de29cd1e87a7f2686a2ba2b9403546d1fc4228db5964/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f536d6f72656e2f747970652d746f6f6c732d7068702f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/Smoren/type-tools-php?branch=master)[![Build and test](https://github.com/Smoren/type-tools-php/actions/workflows/test_master.yml/badge.svg)](https://github.com/Smoren/type-tools-php/actions/workflows/test_master.yml/badge.svg)[![License: MIT](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)](https://opensource.org/licenses/MIT)

Helpers for different operations with PHP data types, variables and containers.

How to install to your project
------------------------------

[](#how-to-install-to-your-project)

```
composer require smoren/type-tools

```

Quick Reference
---------------

[](#quick-reference)

### Unique Extractor

[](#unique-extractor)

MethodDescriptionCode Snippet[`getString`](#Get-String)Returns unique string of the given variable`UniqueExtractor::getString($var, $strict)`[`getHash`](#Get-Hash)Returns unique md5 hash string of the given variable`UniqueExtractor::getHash($var, $strict)`### Object Type Caster

[](#object-type-caster)

MethodDescriptionCode Snippet[`cast`](#Cast)Cast object to another relative type`ObjectTypeCaster::cast($sourceObject, $destinationClass)`### Object Access

[](#object-access)

MethodDescriptionCode Snippet[`getPropertyValue`](#Get-Property-Value)Returns value of the object property`ObjectAccess::getPropertyValue($object, $propertyName)`[`setPropertyValue`](#Set-Property-Value)Sets value of the object property`ObjectAccess::setPropertyValue($object, $propertyName, $value)`[`hasReadableProperty`](#Has-Readable-Property)Returns true if object has readable property by name or by getter`ObjectAccess::hasReadableProperty($object, $propertyName)`[`hasWritableProperty`](#Has-Writable-Property)Returns true if object has writable property by name or by getter`ObjectAccess::hasWritableProperty($object, $propertyName)`[`hasPublicProperty`](#Has-Public-Property)Returns true if object has public property`ObjectAccess::hasPublicProperty($object, $propertyName)`[`hasPublicMethod`](#Has-Public-Method)Returns true if object has public method`ObjectAccess::hasPublicMethod($object, $methodName)`[`hasProperty`](#Has-Property)Returns true if object has property`ObjectAccess::hasProperty($object, $propertyName)`[`hasMethod`](#Has-Method)Returns true if object has method`ObjectAccess::hasMethod($object, $methodName)`### Map Access

[](#map-access)

MethodDescriptionCode Snippet[`get`](#Get)Returns value from the container by key`MapAccess::get($container, $key, $defaultValue)`[`set`](#Set)Sets value to the container by key`MapAccess::set($container, $key, $value)`[`exists`](#Exists)Returns true if accessible key exists in the container`MapAccess::exists($container, $key)`Usage
-----

[](#usage)

### Unique Extractor

[](#unique-extractor-1)

Tool for extracting unique IDs and hashes of any PHP variables and data structures.

Works in two modes: strict and non-strict.

In strict mode:

- scalars: unique strictly by type;
- objects: unique by instance;
- arrays: unique by serialized value;
- resources: result is unique by instance.

In non-strict mode:

- scalars: unique by value;
- objects: unique by serialized value;
- arrays: unique by serialized value;
- resources: result is unique by instance.

#### Get String

[](#get-string)

Returns unique string of the given variable.

`UniqueExtractor::getString(mixed $var, bool $strict): string`

```
use Smoren\TypeTools\UniqueExtractor;

$intValue = 5;
$floatValue = 5.0;

$intValueStrictUniqueId = UniqueExtractor::getString($intValue, true);
$floatValueStrictUniqueId = UniqueExtractor::getString($floatValue, true);

var_dump($intValueStrictUniqueId === $floatValueStrictUniqueId);
// false

$intValueNonStrictUniqueId = UniqueExtractor::getString($intValue, false);
$floatValueNonStrictUniqueId = UniqueExtractor::getString($floatValue, false);

var_dump($intValueNonStrictUniqueId === $floatValueNonStrictUniqueId);
// true
```

#### Get Hash

[](#get-hash)

Returns unique md5 hash string of the given variable.

`UniqueExtractor::getHash(mixed $var, bool $strict): string`

```
use Smoren\TypeTools\UniqueExtractor;

$intValue = 5;
$floatValue = 5.0;

$intValueStrictHash = UniqueExtractor::getHash($intValue, true);
$floatValueStrictHash = UniqueExtractor::getHash($floatValue, true);

var_dump($intValueStrictHash === $floatValueStrictHash);
// false

$intValueNonStrictHash = UniqueExtractor::getHash($intValue, false);
$floatValueNonStrictHash = UniqueExtractor::getHash($floatValue, false);

var_dump($intValueNonStrictHash === $floatValueNonStrictHash);
// true
```

### Object Type Caster

[](#object-type-caster-1)

Tool for casting types of objects.

#### Cast

[](#cast)

Cast object to another relative type (upcast or downcast).

`ObjectTypeCaster::cast(object $sourceObject, string $destinationClass): mixed`

```
use Smoren\TypeTools\ObjectTypeCaster;

class ParentClass
{
    public int $a;
    protected int $b;

    public function __construct(int $a, int $b)
    {
        $this->a = $a;
        $this->b = $b;
    }

    public function toArray(): array
    {
        return [$this->a, $this->b];
    }
}

class ChildClass extends ParentClass
{
    private $c = null;

    public function __construct(int $a, int $b, int $c)
    {
        parent::__construct($a, $b);
        $this->c = $c;
    }

    public function toArray(): array
    {
        return [$this->a, $this->b, $this->c];
    }
}

/* Downcast */

$parentClassObject = new ParentClass(1, 2);
print_r($parentClassObject->toArray());
// [1, 2]

$castedToChildClass = ObjectTypeCaster::cast($parentClassObject, ChildClass::class);
print_r($castedToChildClass->toArray());
// [1, 2, null]

var_dump(get_class($castedToChildClass));
// ChildClass

/* Upcast */

$childClassObject = new ChildClass(1, 2, 3);
print_r($childClassObject->toArray());
// [1, 2, 3]

$castedToParentClass = ObjectTypeCaster::cast($childClassObject, ParentClass::class);
print_r($castedToParentClass->toArray());
// [1, 2]

var_dump(get_class($castedToParentClass));
// ParentClass
```

### Object Access

[](#object-access-1)

Tool for reflecting and accessing object properties and methods.

#### Get Property Value

[](#get-property-value)

Returns value of the object property.

`ObjectAccess::getPropertyValue(object $object, string $propertyName): mixed`

Can access property by its name or by getter.

Throws `Smoren\TypeTools\Exceptions\KeyError` if property is not accessible to read.

```
use Smoren\TypeTools\ObjectAccess;

class MyClass {
    public int $publicProperty = 1;
    private int $privateProperty = 2;

    public function getPrivateProperty(): int
    {
        return $this->privateProperty;
    }
}

$myObject = new MyClass();

// Getting by name:
var_dump(ObjectAccess::getPropertyValue($myObject, 'publicProperty'));
// 1

// Getting by getter (getPrivateProperty()):
var_dump(ObjectAccess::getPropertyValue($myObject, 'privateProperty'));
// 2
```

#### Set Property Value

[](#set-property-value)

Sets value of the object property.

`ObjectAccess::setPropertyValue(object $object, string $propertyName, mixed $value): void`

Can access property by its name or by setter.

Throws `Smoren\TypeTools\Exceptions\KeyError` if property is not accessible to write.

```
use Smoren\TypeTools\ObjectAccess;

class MyClass {
    public int $publicProperty = 1;
    private int $privateProperty = 2;

    public function setPrivateProperty(int $value): void
    {
        $this->privateProperty = $value;
    }

    public function toArray(): array
    {
        return [$this->publicProperty, $this->privateProperty];
    }
}

$myObject = new MyClass();

// Setting by name:
ObjectAccess::setPropertyValue($myObject, 'publicProperty', 11);

// Setting by setter (setPrivateProperty()):
ObjectAccess::getPropertyValue($myObject, 'privateProperty', 22);

print_r($myObject->toArray());
// [11, 22]
```

#### Has Readable Property

[](#has-readable-property)

Returns true if object has property that is readable by name or by getter.

`ObjectAccess::hasReadableProperty(object $object, string $propertyName): bool`

```
use Smoren\TypeTools\ObjectAccess;

class MyClass {
    public int $publicProperty = 1;
    private int $privateProperty = 2;
    private int $notAccessibleProperty = 3;

    public function getPrivateProperty(): int
    {
        return $this->privateProperty;
    }
}

$myObject = new MyClass();

// Accessible by name:
var_dump(ObjectAccess::hasReadableProperty($myObject, 'publicProperty'));
// true

// Accessible by getter:
var_dump(ObjectAccess::hasReadableProperty($myObject, 'privateProperty'));
// true

// Not accessible:
var_dump(ObjectAccess::hasReadableProperty($myObject, 'notAccessibleProperty'));
// false
```

#### Has Writable Property

[](#has-writable-property)

Returns true if object has property that is writable by name or by setter.

`ObjectAccess::hasWritableProperty(object $object, string $propertyName): bool`

```
use Smoren\TypeTools\ObjectAccess;

class MyClass {
    public int $publicProperty = 1;
    private int $privateProperty = 2;
    private int $notAccessibleProperty = 3;

    public function setPrivateProperty(int $value): void
    {
        $this->privateProperty = $value;
    }
}

$myObject = new MyClass();

// Accessible by name:
var_dump(ObjectAccess::hasWritableProperty($myObject, 'publicProperty'));
// true

// Accessible by setter:
var_dump(ObjectAccess::hasWritableProperty($myObject, 'privateProperty'));
// true

// Not accessible:
var_dump(ObjectAccess::hasWritableProperty($myObject, 'notAccessibleProperty'));
// false
```

#### Has Public Property

[](#has-public-property)

Returns true if object has public property.

`ObjectAccess::hasPublicProperty(object $object, string $propertyName): bool`

```
use Smoren\TypeTools\ObjectAccess;

class MyClass {
    public int $publicProperty = 1;
    private int $privateProperty = 2;
}

$myObject = new MyClass();

var_dump(ObjectAccess::hasPublicProperty($myObject, 'publicProperty'));
// true

var_dump(ObjectAccess::hasPublicProperty($myObject, 'privateProperty'));
// false
```

#### Has Public Method

[](#has-public-method)

Returns true if object has public method.

`ObjectAccess::hasPublicMethod(object $object, string $methodName): bool`

```
use Smoren\TypeTools\ObjectAccess;

class MyClass {
    public function publicMethod(): int
    {
        return 1;
    }

    private function privateMethod(): int
    {
        return 2;
    }
}

$myObject = new MyClass();

var_dump(ObjectAccess::hasPublicMethod($myObject, 'publicMethod'));
// true

var_dump(ObjectAccess::hasPublicMethod($myObject, 'privateMethod'));
// false
```

#### Has Property

[](#has-property)

Returns true if object has property.

`ObjectAccess::hasProperty(object $object, string $propertyName): bool`

```
use Smoren\TypeTools\ObjectAccess;

class MyClass {
    public int $publicProperty = 1;
    private int $privateProperty = 2;
}

$myObject = new MyClass();

var_dump(ObjectAccess::hasProperty($myObject, 'publicProperty'));
// true

var_dump(ObjectAccess::hasProperty($myObject, 'privateProperty'));
// true

var_dump(ObjectAccess::hasProperty($myObject, 'anotherProperty'));
// false
```

#### Has Method

[](#has-method)

Returns true if object has method.

`ObjectAccess::hasMethod(object $object, string $methodName): bool`

```
use Smoren\TypeTools\ObjectAccess;

class MyClass {
    public function publicMethod(): int
    {
        return 1;
    }

    private function privateMethod(): int
    {
        return 2;
    }
}

$myObject = new MyClass();

var_dump(ObjectAccess::hasMethod($myObject, 'publicMethod'));
// true

var_dump(ObjectAccess::hasMethod($myObject, 'privateMethod'));
// true
```

### Map Access

[](#map-access-1)

Tool for map-like accessing of different containers by string keys.

Can access:

- properties of objects (by name or by getter);
- elements of arrays and ArrayAccess objects (by key).

#### Get

[](#get)

Returns value from the container by key or default value if key does not exist or not accessible.

Throws `Smoren\TypeTools\Exceptions\KeyError` if key is not accessible to read.

`MapAccess::get(mixed $container, string $key, mixed $defaultValue = null): mixed`

```
use Smoren\TypeTools\MapAccess;

$array = [
    'a' => 1,
];

var_dump(MapAccess::get($array, 'a', 0));
// 1

var_dump(MapAccess::get($array, 'b', 0));
// 0

var_dump(MapAccess::get($array, 'b'));
// null

class MyClass {
    public int $publicProperty = 1;
    private int $privateProperty = 2;
    private int $notAccessibleProperty = 3;

    public function getPrivateProperty(): int
    {
        return $this->privateProperty;
    }
}

$myObject = new MyClass();

// Accessible by name:
var_dump(MapAccess::get($myObject, 'publicProperty', 0));
// 1

// Accessible by getter:
var_dump(MapAccess::get($myObject, 'privateProperty'));
// 2

// Not accessible:
var_dump(MapAccess::get($myObject, 'notAccessibleProperty', -1));
// -1

var_dump(MapAccess::get($myObject, 'notAccessibleProperty'));
// null

// Nonexistent:
var_dump(MapAccess::get($myObject, 'nonexistentProperty', -1));
// -1

var_dump(MapAccess::get($myObject, 'nonexistentProperty'));
// null
```

#### Set

[](#set)

Sets value to the container by key.

`MapAccess::set(mixed $container, string $key, mixed $value): void`

Throws `Smoren\TypeTools\Exceptions\KeyError` if key is not accessible to write.

```
use Smoren\TypeTools\MapAccess;

$array = [
    'a' => 1,
];

MapAccess::set($array, 'a', 11);
MapAccess::set($array, 'b', 22);

print_r($array);
// ['a' => 11, 'b' => 22]

class MyClass {
    public int $publicProperty = 1;
    private int $privateProperty = 2;

    public function setPrivateProperty(int $value): void
    {
        $this->privateProperty = $value;
    }

    public function toArray(): array
    {
        return [$this->publicProperty, $this->privateProperty];
    }
}

$myObject = new MyClass();

// Accessible by name:
MapAccess::get($myObject, 'publicProperty', 11);

// Accessible by getter:
MapAccess::get($myObject, 'privateProperty', 22);

print_r($myObject->toArray());
// [11, 22]
```

#### Exists

[](#exists)

Returns true if accessible key exists in the container.

`MapAccess::exists(mixed $container, string $key): bool`

```
use Smoren\TypeTools\MapAccess;

$array = [
    'a' => 1,
];

var_dump(MapAccess::exists($array, 'a'));
// true

var_dump(MapAccess::exists($array, 'b'));
// false
class MyClass {
    public int $publicProperty = 1;
    private int $privateProperty = 2;
    private int $notAccessibleProperty = 3;

    public function getPrivateProperty(): int
    {
        return $this->privateProperty;
    }
}

$myObject = new MyClass();

// Accessible by name:
var_dump(MapAccess::exists($myObject, 'publicProperty'));
// true

// Accessible by getter:
var_dump(MapAccess::exists($myObject, 'privateProperty'));
// true

// Not accessible:
var_dump(MapAccess::get($myObject, 'notAccessibleProperty'));
// false

// Nonexistent:
var_dump(MapAccess::get($myObject, 'nonexistentProperty', -1));
// false
```

Unit testing
------------

[](#unit-testing)

```
composer install
composer test-init
composer test

```

License
-------

[](#license)

PHP Type Tools is licensed under the MIT License.

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance31

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 87.5% 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 ~1 days

Total

8

Last Release

1216d ago

Major Versions

v0.0.1 → v1.0.02023-01-07

v1.1.0 → v2.0.02023-01-10

### Community

Maintainers

![](https://www.gravatar.com/avatar/fdc9e8df44cbbc0fc88885a4f1daa2cad755266e73eb0bac15efaf0d47ecbae2?d=identicon)[smoren](/maintainers/smoren)

---

Top Contributors

[![Smoren](https://avatars.githubusercontent.com/u/7403235?v=4)](https://github.com/Smoren "Smoren (21 commits)")[![markrogoyski](https://avatars.githubusercontent.com/u/10004372?v=4)](https://github.com/markrogoyski "markrogoyski (2 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (1 commits)")

---

Tags

accessordata-typeshelpersphpphp-libraryreflectiontype-castingaccessoruniquedata-types

###  Code Quality

TestsCodeception

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/smoren-type-tools/health.svg)

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

###  Alternatives

[joegreen0991/hyperloglog

A hyper log log with min hash data structure library, for counting cardinalities. Union and intersection capable

2116.5k](/packages/joegreen0991-hyperloglog)[infocyph/uid

UUID (RFC 4122 + Unofficial/Draft), ULID, Snowflake ID, Sonyflake ID, TBSL (library exclusive) generator!

105.1k](/packages/infocyph-uid)

PHPackages © 2026

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