PHPackages                             roave/strict-php - 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. roave/strict-php

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

roave/strict-php
================

A strict runtime type and invariant checker for developing safer PHP applications

2571289[7 issues](https://github.com/Roave/StrictPhp/issues)[1 PRs](https://github.com/Roave/StrictPhp/pulls)PHP

Since Jul 1Pushed 10y ago5 watchersCompare

[ Source](https://github.com/Roave/StrictPhp)[ Packagist](https://packagist.org/packages/roave/strict-php)[ RSS](/packages/roave-strict-php/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (3)Used By (0)

StrictPhp
=========

[](#strictphp)

[![Build Status](https://camo.githubusercontent.com/22c56eecd9a4d3e6ad3171b510c16db36ac881c2992c29a654bf9a932ee870f4/68747470733a2f2f7472617669732d63692e6f72672f526f6176652f5374726963745068702e737667)](https://travis-ci.org/Roave/StrictPhp)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/372e230b90abc737d54a10bd556dde468682c4f942dc67d4a02757c3fa1db3b6/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f526f6176652f5374726963745068702f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Roave/StrictPhp/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/b8de3dc97e8271c13dc457a5b1b812025e16ffdff91f60c2b602ce66529767d5/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f526f6176652f5374726963745068702f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Roave/StrictPhp/?branch=master)

StrictPhp is a development tool aimed at bringing stricter runtime assertions into PHP applications and libraries.

Authors
-------

[](#authors)

- [Marco Pivetta](https://github.com/Ocramius)
- [Jefersson Nathan](https://github.com/malukenho)

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

[](#installation)

```
$ composer require roave/strict-php
```

Please note that the current version has unstable dependencies.

In order to install those dependencies, you can set `"minimum-stability"` in your `composer.json`:

```
{
    "minimum-stability": "dev"
}
```

Usage
-----

[](#usage)

After installing `StrictPhp`, point it at the directory to be checked at runtime (the code that you are writing) via following code:

```
\StrictPhp\StrictPhpKernel::bootstrap([
    'debug'        => true,
    // change this if you use this tool on multiple projects:
    'cacheDir'     => sys_get_temp_dir(),
    'includePaths' => [
        __DIR__ . '/path/to/your/sources',
    ],
]);
```

StrictPhp will then intercept any runtime operations that are considered "illegal" and throw an exception or a catchable fatal error.

Please remember to execute this code **before** any code that may autoload any of the classes that should be checked.

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

[](#configuration)

The `StrictPhp\StrictPhpKernel` can be initialized with a set of [options to be passed to go-aop-php](http://go.aopphp.com/docs/initial-configuration/) and a set of feature flags:

- `StrictPhp\StrictPhpKernel::CHECK_STATE_AFTER_CONSTRUCTOR_CALL`
- `StrictPhp\StrictPhpKernel::JAIL_PUBLIC_METHOD_PARAMETERS`
- `StrictPhp\StrictPhpKernel::CHECK_STATE_AFTER_PUBLIC_METHOD_CALL`
- `StrictPhp\StrictPhpKernel::CHECK_PUBLIC_METHOD_PARAMETER_TYPE`
- `StrictPhp\StrictPhpKernel::CHECK_PUBLIC_METHOD_RETURN_TYPE`
- `StrictPhp\StrictPhpKernel::CHECK_PROPERTY_WRITE_IMMUTABILITY`
- `StrictPhp\StrictPhpKernel::CHECK_PROPERTY_WRITE_TYPE`

Each of these features are described below.

Features
--------

[](#features)

`StrictPhp` currently supports following features:

#### Per-property type checks

[](#per-property-type-checks)

Enabled via flag `StrictPhp\StrictPhpKernel::CHECK_PROPERTY_WRITE_TYPE`.

This feature will prevent your application from assigning illegal values to properties that are type-hinted (via docblock) differently. As an example, consider following class:

```
class Example
{
    /**
     * @var int|null
     */
    public $integer;
}
```

Following code will work:

```
$object = new Example();

$object->integer = 123;
```

Following code will crash:

```
$object = new Example();

$object->integer = '123';
```

Please note that this kind of feature currently only works with public and protected properties.

#### Return type checks

[](#return-type-checks)

Quite similar to the above functionality, this feature will prevent your application from **returning** illegal values from methods that are type-hinted (via docblock) differently. As an example, consider following method:

```
class Example
{
    /**
     * @return string
     */
    public function dummyReturn($value)
    {
        return $value;
    }
}
```

Following code will work:

```
(new Example())->dummyReturn('string');
```

Following code will crash:

```
(new Example())->dummyReturn(123);
```

Please note that this kind of feature currently only works with public and protected methods.

#### immutable properties

[](#immutable-properties)

Enabled via flag `StrictPhp\StrictPhpKernel::CHECK_PROPERTY_WRITE_IMMUTABILITY`.

This feature will prevent your application from overwriting object properties that are marked as `@immutable`. As an example, consider following class:

```
class Example
{
    /**
     * @immutable
     */
    public $immutableProperty;
}
```

Following code will crash:

```
$object = new Example();

$object->immutableProperty = 'a value';

echo 'Works till here!';

$object->immutableProperty = 'another value'; // crash
```

Please note that this kind of feature currently only works with public and protected properties.

#### Public constructor property initialization checks

[](#public-constructor-property-initialization-checks)

Enabled via flag `StrictPhp\StrictPhpKernel::CHECK_STATE_AFTER_CONSTRUCTOR_CALL`.

This feature of StrictPhp allows checking whether a public constructor of a class fully initialized an object.

Following code will make StrictPhp crash your application:

```
class Example
{
    /**
     * @var array
     */
    private $arrayProperty;

    public function __construct()
    {
    }
}
```

In order to make this code work, you have to either annotate `$arrayProperty`with `@var array|null`, or make the constructor initialize the property correctly:

```
class Example
{
    /**
     * @var array
     */
    private $arrayProperty;

    public function __construct()
    {
        $this->arrayProperty = ['initial status'];
    }
}
```

#### Parameter interface jailing

[](#parameter-interface-jailing)

Enabled via flag `StrictPhp\StrictPhpKernel::JAIL_PUBLIC_METHOD_PARAMETERS`.

This feature of StrictPhp "jails" (restricts) calls to non-interfaced methods whenever an interface is used as a type-hint.

Following example will work, but will crash if StrictPhp is enabled:

```
interface HornInterface
{
    public function honk();
}

class TheUsualHorn implements HornInterface
{
    public function honk() { var_dump('honk'); }
    public function sadTrombone() { var_dump('pooapooapooapoaaaa'); }
}

class Car
{
    public function honk(HornInterface $horn, $sad = false)
    {
        if ($sad) {
            // method not covered by interface: crash
            $horn->sadTrombone();

            return;
        }

        // interface respected
        $horn->honk();
    }
}
```

```
$car  = new Car();
$horn = new TheUsualHorn();

$car->honk($horn, false); // works
$car->honk($horn, true); // crashes
```

This prevents consumers of your APIs to design their code against non-API methods.

#### Parameter checking

[](#parameter-checking)

Enabled via flag `StrictPhp\StrictPhpKernel::CHECK_PUBLIC_METHOD_PARAMETER_TYPE`.

StrictPhp also provides a way to check parameters types in more detail during public method calls.

Specifically, the following code will work in PHP:

```
final class Invoice
{
    /**
     * @param LineItem[] $lineItems
     */
    public function __construct(array $lineItems)
    {
        // ...
    }
}

$invoice = new Invoice(['foo', 'bar']);
```

This code will crash in StrictPhp due to the type mismatch in `$lineItems` (which should be a collection of `LineItem` objects instead).

Current limitations
-------------------

[](#current-limitations)

This package uses [voodoo magic](http://ocramius.github.io/voodoo-php/) to operate, specifically [go-aop-php](https://github.com/lisachenko/go-aop-php).

Go AOP PHP has some limitations when it comes to intercepting access to private class members, so please be aware that it has limited scope (for now).

This package only works against autoloaded classes; classes that aren't handled by an autoloader cannot be rectified by StrictPhp.

License
-------

[](#license)

This package is released under the [MIT license](LICENSE).

Contributing
------------

[](#contributing)

If you wish to contribute to the project, please read the [CONTRIBUTING notes](CONTRIBUTING.md).

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 96.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.

### Community

Maintainers

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

---

Top Contributors

[![Ocramius](https://avatars.githubusercontent.com/u/154256?v=4)](https://github.com/Ocramius "Ocramius (307 commits)")[![samsonasik](https://avatars.githubusercontent.com/u/459648?v=4)](https://github.com/samsonasik "samsonasik (3 commits)")[![nikic](https://avatars.githubusercontent.com/u/216080?v=4)](https://github.com/nikic "nikic (3 commits)")[![GeeH](https://avatars.githubusercontent.com/u/613376?v=4)](https://github.com/GeeH "GeeH (2 commits)")[![asgrim](https://avatars.githubusercontent.com/u/496145?v=4)](https://github.com/asgrim "asgrim (2 commits)")[![lisachenko](https://avatars.githubusercontent.com/u/640114?v=4)](https://github.com/lisachenko "lisachenko (1 commits)")[![jaapio](https://avatars.githubusercontent.com/u/1060433?v=4)](https://github.com/jaapio "jaapio (1 commits)")

### Embed Badge

![Health badge](/badges/roave-strict-php/health.svg)

```
[![Health](https://phpackages.com/badges/roave-strict-php/health.svg)](https://phpackages.com/packages/roave-strict-php)
```

###  Alternatives

[mnapoli/fluent-symfony

8036.6k](/packages/mnapoli-fluent-symfony)[phuml/phuml

phUML is a fully automatic UML class diagram generator for PHP code

11411.3k1](/packages/phuml-phuml)[tcb13/substringy

A sub string manipulation library with multibyte support that extends Stringy

1760.6k1](/packages/tcb13-substringy)[humanmade/meta-tags

Meta tag generator for Opengraph, Twitter &amp; JSON+LD

1052.2k](/packages/humanmade-meta-tags)[illuminatech/nova-config

A Laravel Nova tool for application configuration management.

134.2k](/packages/illuminatech-nova-config)[media24si/upn-generator

UPN form generator

152.3k](/packages/media24si-upn-generator)

PHPackages © 2026

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