Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F12741303
CallableElement.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Referenced Files
None
Subscribers
None
CallableElement.php
View Options
<?php
declare
(
strict_types
=
1
);
namespace
Keruald\OmniTools\Reflection
;
use
Closure
;
use
http\Exception\InvalidArgumentException
;
use
ReflectionException
;
use
ReflectionFunction
;
use
ReflectionFunctionAbstract
;
use
ReflectionMethod
;
class
CallableElement
{
private
ReflectionFunctionAbstract
$callable
;
/**
* @throws ReflectionException
*/
public
function
__construct
(
callable
$callable
)
{
$this
->
callable
=
self
::
getReflectionFunction
(
$callable
);
}
/**
* @throws ReflectionException
*/
private
static
function
getReflectionFunction
(
callable
$callable
)
:
ReflectionFunctionAbstract
{
///
/// Functions
///
if
(
$callable
instanceof
Closure
)
{
return
new
ReflectionFunction
(
$callable
);
}
///
/// Objets and methods
///
if
(
is_array
(
$callable
))
{
return
new
ReflectionMethod
(
$callable
[
0
],
$callable
[
1
]);
}
if
(
is_object
(
$callable
))
{
// If __invoke() doesn't exist, the objet isn't a callable.
// Calling this method with such object would throw a TypeError
// before reaching this par of the code, so it is safe to assume
// we can correctly call it.
return
new
ReflectionMethod
([
$callable
,
'__invoke'
]);
}
///
/// Hybrid cases
///
if
(
is_string
(
$callable
))
{
if
(!
str_contains
(
$callable
,
"::"
))
{
return
new
ReflectionFunction
(
$callable
);
}
return
new
ReflectionMethod
(
$callable
);
}
throw
new
InvalidArgumentException
(
"Callable not recognized: "
.
gettype
(
$callable
)
);
}
public
function
countArguments
()
:
int
{
return
$this
->
callable
->
getNumberOfParameters
();
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sun, Nov 16, 13:24 (1 d, 14 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3132665
Default Alt Text
CallableElement.php (1 KB)
Attached To
Mode
rKERUALD Keruald libraries development repository
Attached
Detach File
Event Timeline
Log In to Comment