Enforce type constraints
PHP
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src/InterNations
tests/InterNations
.gitignore
.travis.yml
README.md
build.xml
composer.json
composer.phar
humbug.json
phpunit.xml.dist

README.md

Type Jail

Gitter Build Status Dependency Status Average time to resolve an issue Percentage of issues still open

Enforce super type contract of an object

Usage

use InterNations\Component\TypeJail\Factory\SuperTypeFactory;
use InterNations\Component\TypeJail\Factory\JailFactory;
use InterNations\Component\TypeJail\Factory\SuperTypeJailFactory;

$file = new SplFileObject(__FILE__);


$factory = new JailFactory();
$file = $factory->createInstanceJail($file, 'SplFileInfo');

// Will return true
var_dump($file instanceof SplFileInfo);

// Will return the file path because that method is declared in SplFileInfo
$file->getFilePath();

// Will throw an exception indicating a type violation because that method
// is declared in SplFileObject
$file->flock();


$factory = new SuperTypeJailFactory();
$file = $factory->createInstanceJail($file, 'SplFileInfo');

// Will return false
var_dump($file instanceof SplFileInfo);

// Will return the file path because that method is declared in SplFileInfo
$file->getFilePath();

// Will throw an exception indicating a type violation because that method
// is declared in SplFileObject
$file->flock();


$factory = new SuperTypeFactory();
$file = $factory->createInstanceJail($file, 'SplFileInfo');

// Will return false
var_dump($file instanceof SplFileInfo);

// Will return the file path because that method is declared in SplFileInfo
$file->getFilePath();

// Fatal error: method not found
$file->flock();

Acknowledgement

Standing on the shoulders of ocramius/proxy-manager by Marco Pivetta that makes it super-duper easy to work with proxies.