php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76054 is_a does not accept scalar types
Submitted: 2018-03-05 18:27 UTC Modified: 2021-08-01 04:22 UTC
Votes:1
Avg. Score:2.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: tom at r dot je Assigned: cmb (profile)
Status: No Feedback Package: Reflection related
PHP Version: 7.2.3 OS: any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
47 + 39 = ?
Subscribe to this entry?

 
 [2018-03-05 18:27 UTC] tom at r dot je
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()




Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-07-20 14:15 UTC] cmb@php.net
-Status: Open +Status: Feedback -Assigned To: +Assigned To: cmb
 [2021-07-20 14:15 UTC] cmb@php.net
ReflectionNamedType::isBuiltin()[1] to the rescue?

[1] <https://www.php.net/manual/en/reflectionnamedtype.isbuiltin.php>
 [2021-08-01 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 08:01:30 2024 UTC