Add daverandom/callback-validator as a dependency

This commit is contained in:
Dylan K. Taylor 2018-12-04 17:14:37 +00:00
parent e3f46987f5
commit 762405d16a
3 changed files with 83 additions and 3 deletions

View File

@ -29,7 +29,8 @@
"pocketmine/binaryutils": "^0.1.0",
"pocketmine/nbt": "^0.2.1",
"pocketmine/math": "^0.2.0",
"pocketmine/snooze": "^0.1.0"
"pocketmine/snooze": "^0.1.0",
"daverandom/callback-validator": "dev-master"
},
"autoload": {
"psr-4": {

45
composer.lock generated
View File

@ -4,8 +4,48 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "3536995c56bfc3dbd6ccc0994e88a636",
"content-hash": "2d120a3dd7d68958809c3662d1cb7c99",
"packages": [
{
"name": "daverandom/callback-validator",
"version": "dev-master",
"source": {
"type": "git",
"url": "https://github.com/DaveRandom/CallbackValidator.git",
"reference": "d87a08cddbc6099816ed01e50ce25cdfc43b542f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/DaveRandom/CallbackValidator/zipball/d87a08cddbc6099816ed01e50ce25cdfc43b542f",
"reference": "d87a08cddbc6099816ed01e50ce25cdfc43b542f",
"shasum": ""
},
"require": {
"ext-reflection": "*",
"php": ">=7.0"
},
"require-dev": {
"phpunit/phpunit": "^6.0"
},
"type": "library",
"autoload": {
"psr-4": {
"DaveRandom\\CallbackValidator\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Chris Wright",
"email": "cw@daverandom.com"
}
],
"description": "Tools for validating callback signatures",
"time": "2017-04-03T15:22:41+00:00"
},
{
"name": "pocketmine/binaryutils",
"version": "0.1.1",
@ -227,7 +267,8 @@
"aliases": [],
"minimum-stability": "stable",
"stability-flags": {
"ext-pthreads": 20
"ext-pthreads": 20,
"daverandom/callback-validator": 20
},
"prefer-stable": false,
"prefer-lowest": false,

View File

@ -27,6 +27,7 @@ declare(strict_types=1);
namespace pocketmine\utils;
use DaveRandom\CallbackValidator\CallbackType;
use pocketmine\ThreadManager;
/**
@ -593,4 +594,41 @@ class Utils{
return true; //stfu operator
}
public static function testValidInstance(string $className, string $baseName) : void{
try{
$base = new \ReflectionClass($baseName);
}catch(\ReflectionException $e){
throw new \InvalidArgumentException("Base class $baseName does not exist");
}
try{
$class = new \ReflectionClass($className);
}catch(\ReflectionException $e){
throw new \InvalidArgumentException("Class $className does not exist");
}
if(!$class->isSubclassOf($baseName)){
throw new \InvalidArgumentException("Class $className does not " . ($base->isInterface() ? "implement" : "extend") . " " . $baseName);
}
if(!$class->isInstantiable()){
throw new \InvalidArgumentException("Class $className cannot be constructed");
}
}
/**
* Verifies that the given callable is compatible with the desired signature. Throws a TypeError if they are
* incompatible.
*
* @param callable $signature Dummy callable with the required parameters and return type
* @param callable $subject Callable to check the signature of
*
* @throws \DaveRandom\CallbackValidator\InvalidCallbackException
* @throws \TypeError
*/
public static function validateCallableSignature(callable $signature, callable $subject) : void{
if(!($sigType = CallbackType::createFromCallable($signature))->isSatisfiedBy($subject)){
throw new \TypeError("Declaration of callable `" . CallbackType::createFromCallable($subject) . "` must be compatible with `" . $sigType . "`");
}
}
}