Bug #76054 [NEW]: is_a does not accept scalar types

From: Date: Mon, 05 Mar 2018 18:27:46 +0000
Subject: Bug #76054 [NEW]: is_a does not accept scalar types
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-214211@lists.php.net to get a copy of this message
From:             tom at r dot je
Operating system: any
PHP version:      7.2.3
Package:          Reflection related
Bug Type:         Bug
Bug description:is_a does not accept scalar types

Description:
------------
I cannot easily work out whether a function can be successfully called
with a given set of values if there are scalar type hints. 

Ideally I'd like to do this:


<?php
function foo(string $a, callable $bar) {

}


//I want to check to see whether these arguments can be used to call the
function
$arguments = [123, 'not-callable!'];

//Or these valid arguments
// $arguments = ['A string', function() {}]; //This should pass the
check

// To do this, I need to check to see whether the arguments match the
type hint

$reflect = new ReflectionFunction('foo');

$valid = true;
foreach ($reflect->getParameters() as $i => $param) {
	if ($param->getType()) {
		//check to see if the type $arguments[$i] matches the type hint
		//e.g. would evaluate to is_a('string', 123);
		if (!is_a($param->getType()->getName(), $arguments[$i])) $valid =
false;

	}
}


echo $valid ? 'Function foo can be called with supplied $arguments' :
'$arguments do not match function definition.';

?>


However, because is_a does not allow scalar types in the first argument,
it's not possible. Instead, I need to do this:


<?php

function foo(string $a, callable $bar) {

}


//I want to check to see whether these arguments can be used to call the
function
$arguments = [123, 'not-callable!'];

//Or these valid arguments
// $arguments = ['A string', function() {}]; //This should pass the
check

// To do this, I need to check to see whether the arguments match the
type hint

$reflect = new ReflectionFunction('foo');

$valid = true;
foreach ($reflect->getParameters() as $i => $param) {
	if ($param->getType()) {
		$type = $param->getType()->getName();
		if ($type == 'string' && !is_string($arguments[$i])) $valid = false;
		else if ($type == 'int' && !is_int($arguments[$i])) $valid = false;
		else if ($type == 'callable' && !is_callable($arguments[$i])) $valid =
false;
		else if ($type == 'array' && !is_array($arguments[$i])) $valid =
false;
	}
}


echo $valid ? 'Function foo can be called with supplied $arguments' :
'$arguments do not match function definition.';

?>

Or, alternatively, this hacky as hell workaround:


<?php

function foo(string $a, callable $bar) {

}


//I want to check to see whether these arguments can be used to call the
function
$arguments = [123, 'not-callable!'];

//Or these valid arguments
// $arguments = ['A string', function() {}]; //This should pass the
check

// To do this, I need to check to see whether the arguments match the
type hint

$reflect = new ReflectionFunction('foo');

$valid = true;
foreach ($reflect->getParameters() as $i => $param) {
	if ($param->getType()) {
		if (!call_user_func('is_' . $param->getType()->getName(),
$arguments[$i])) $valid = false;
	}
}


echo $valid ? 'Function foo can be called with supplied $arguments' :
'$arguments do not match function definition.';



?>



Ideally, is_a should support scalar types but perhaps adjusting
reflection to allow $param->getType()->willAccept($value); or similar
would  work as well because at the moment, to determine this, we need to
do two checks: One for $param->getClass() and another for
$param->getType()




-- 
Edit bug report at https://bugs.php.net/bug.php?id=76054&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=76054&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=76054&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=76054&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=76054&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=76054&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=76054&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=76054&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=76054&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=76054&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=76054&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=76054&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=76054&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=76054&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=76054&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=76054&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=76054&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=76054&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=76054&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=76054&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=76054&r=mysqlcfg



Thread (3 messages)

« previous php.bugs (#214211) next »