PHPackages                             shrink/examples - 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. [Testing &amp; Quality](/categories/testing)
4. /
5. shrink/examples

AbandonedArchivedLibrary[Testing &amp; Quality](/categories/testing)

shrink/examples
===============

Compose example objects

v2.0.1(3y ago)025[3 issues](https://github.com/shrink/examples/issues)MITPHPPHP ^8.1

Since May 2Pushed 3y ago1 watchersCompare

[ Source](https://github.com/shrink/examples)[ Packagist](https://packagist.org/packages/shrink/examples)[ RSS](/packages/shrink-examples/feed)WikiDiscussions main Synced today

READMEChangelog (6)Dependencies (4)Versions (8)Used By (0)

Examples
========

[](#examples)

[![Packagist](https://camo.githubusercontent.com/7b5d2ae82c14e57a3cfa37dfd3ad7b139b1a3a3240586c8e139753d8d4dfb923/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736872696e6b2f6578616d706c65732e737667)](https://packagist.org/packages/shrink/examples)

Compose example objects.

```
use Shrink\Examples\E;
use Shrink\Examples\Examples;

final class Person
{
  public function __construct(
    public readonly string $name,
    public readonly int $age
  ) {
  }
}

$examplePersonDefinition = E::define(Person::class, name: "Alice", age: 30);

($examples = new Examples())->register($examplePersonDefinition);

$person = $examples->make(E::g(Person::class, name: "Bob"));

self::assertSame(
  "Hello, Bob (age 30).",
  "Hello, {$person->name} (age {$person->age})."
);
```

Usage
-----

[](#usage)

1. [**Install**](#install) the library with composer
2. [**Define**](#define-example) examples
3. [**Make**](#make-an-object) objects

### Install

[](#install)

```
dev:~$ composer require shrink/examples --dev
```

🧶 The latest version of Examples requires PHP 8.1, use [Examples v1](https://github.com/shrink/examples/tree/v1) for PHP 7.4 and PHP 8.0 support.

### Instantiate Examples

[](#instantiate-examples)

An `Examples::class` instance holds your example definitions and creates your objects from these definitions.

```
use Shrink\Examples\Examples;

$examples = new Examples();
```

### Define Examples

[](#define-examples)

The `E::define()` method accepts a class `type` and zero or more named arguments, which map to the `type`'s constructor arguments. `E::define()` returns a definition to register with your instance of Examples.

```
use Shrink\Examples\E;

$examples->register(E::define(Person::class, name: "Alice", age: 30));
```

✨ Since v2, named arguments are used instead of a parameters array.

🧬 `E::define()` is a shortcut to create a simple example definition, see [Internals -&gt; Definition](#definition) for building your own implementation.

### Make An Object

[](#make-an-object)

The `E::g()` method accepts a class `type` (referring to a registered example) and zero or more named arguments to overwrite the example defaults. `E::g()`returns an example configuration, which your instance of Examples will `make()`.

```
use Shrink\Examples\E;

$example = $examples->make(E::g(Person::class, name: "Bob"));

echo "Hello, {$example->name} (age {$example->age}).";
// Hello, Bob (age 30).
```

🧬 `E::g()` is a shortcut to create a simple example configuration, see [Internals -&gt; Creation](#creation) for building your own implementation.

### Features

[](#features)

#### Nested Examples

[](#nested-examples)

[Examples::class](src/Examples/Examples.php) will resolve any example definitions it encounters when making an example, allowing for many levels of nested example definitions and configuration.

```
final class Person
{
  public function __construct(
    public readonly string $name,
    public readonly int $age,
    public readonly ?Location $location
  ) {
  }
}

final class Location
{
  public function __construct(
    public readonly string $streetAddress,
    public readonly string $country
  ) {
  }
}

$examples = new Examples();

$examples->register(
  E::define(
    Location::class,
    streetAddress: "123 Default Street",
    country: "England"
  )
);

$examples->register(
  E::define(
    Person::class,
    name: "Alice",
    age: 30,
    location: E::g(Location::class, country: "United States")
  )
);

$person = $examples->make(
  E::g(
    Person::class,
    name: "Bob",
    location: E::g(Location::class, country: "The Netherlands")
  )
);

self::assertSame(
  "Hello, {$person->name} (age {$person->age}) from {$person->location->country}.",
  "Hello, Bob (age 30) from The Netherlands."
);
```

Internals
---------

[](#internals)

### Definition

[](#definition)

Examples are registered using an example definition (`DefinesExample`) which in turn uses a builder (`BuildsExampleInstances`) to create an object using optional configuration.

```
use Shrink\Examples\Definition;
use Shrink\Examples\Examples;
use Shrink\Examples\Example;

$examples = new Examples();

$examples->register(new Definition(
    Person::class,
    BuildsExampleInstances $builder,
    array $defaults
));

$person = $examples->make(new Example(
    Person::class,
    array $parameters
));
```

Implicit instance building is handled through Reflection by `ReflectionBuilder`which accepts a class name to build.

```
use Shrink\Examples\Definition;
use Shrink\Examples\ReflectionBuilder;

$examples->register(
  new Definition(Person::class, new ReflectionBuilder(Person::class), [
    "name" => "Alice",
    "age" => 30,
  ])
);
```

`E::define()` is a shortcut for creating an example definition with implicit building. Explicit instance building is handled by providing a callable which is called with the Example parameters as method parameters.

```
use Shrink\Examples\CallableBuilder;
use Shrink\Examples\Definition;

$examples->register(
  new Definition(
    Person::class,
    new CallableBuilder(
      fn(string $name, int $age): Person => new Person($name, $age)
    ),
    [
      "name" => "Alice",
      "age" => 30,
    ]
  )
);
```

#### Creation

[](#creation)

Objects are made, from an example, with an optional configuration of `parameters`. Ask the `Examples` instance to `make(ConfiguresExample $configuration)`. A default implementation of `ConfiguresExample` is included which is constructed with the type and parameters.

```
use Shrink\Examples\Example;

$person = $examples->make(new Example(Person::class));
```

Parameters may be provided to overwrite any defaults.

```
use Shrink\Examples\Example;

$person = $examples->make(
  new Example(Person::class, [
    "name" => "Alice",
  ])
);
```

License
-------

[](#license)

Examples is open-sourced software licensed under the [MIT license](https://choosealicense.com/licenses/mit/).

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~144 days

Recently: every ~179 days

Total

7

Last Release

1383d ago

Major Versions

v0.2.0 → v1.0.02020-10-01

v1.2.0 → v2.0.02022-09-18

PHP version history (3 changes)v0.1.0PHP ^7.4

v1.1.0PHP ^7.4 || ^8

v2.0.0PHP ^8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/349689?v=4)[sam](/maintainers/shrink)[@shrink](https://github.com/shrink)

---

Top Contributors

[![shrink](https://avatars.githubusercontent.com/u/349689?v=4)](https://github.com/shrink "shrink (25 commits)")

---

Tags

dddphptesting

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/shrink-examples/health.svg)

```
[![Health](https://phpackages.com/badges/shrink-examples/health.svg)](https://phpackages.com/packages/shrink-examples)
```

###  Alternatives

[dms/phpunit-arraysubset-asserts

This package provides ArraySubset and related asserts once deprecated in PHPUnit 8

14429.2M360](/packages/dms-phpunit-arraysubset-asserts)

PHPackages © 2026

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