-
Notifications
You must be signed in to change notification settings - Fork 0
/
PHPScalarTypeHinting.php
34 lines (33 loc) · 1.15 KB
/
PHPScalarTypeHinting.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
set_error_handler(
function ($errno, $errstr, $errfile, $errline) {
if ( E_RECOVERABLE_ERROR===$errno ) {
$matches = array();
if(preg_match('/Argument \d* passed to [\s\S]* must be an instance of ([^, ]*), ([^ ]*) given/', $errstr, $matches)){
$expectedClass = strtolower($matches[1]);
$actualClass = strtolower($matches[2]);
switch($expectedClass){
case 'int':
case 'integer':
return $actualClass === 'integer';
case 'float':
case 'double':
case 'real':
return $actualClass === 'double';
case 'bool':
case 'boolean':
return $actualClass === 'boolean';
case 'numeric':
return $actualClass === 'integer' || $actualClass === 'double';
case 'string':
return $actualClass === 'string';
case 'scalar':
return $actualClass === 'integer' || $actualClass === 'double' ||
$actualClass == 'boolean' || $actualClass === 'string';
}
}else {
return 1 === preg_match('/Argument \d* passed to [\s\S]* must be an instance of object, instance of ([^ ]*) given/i', $errstr);
}
}
return false;
}, E_RECOVERABLE_ERROR);