Skip to content

Instantly share code, notes, and snippets.

@tbreuss
Last active October 25, 2022 19:03
Show Gist options
  • Save tbreuss/b23ae00af0d653a31543f2c4af06bc2f to your computer and use it in GitHub Desktop.
Save tbreuss/b23ae00af0d653a31543f2c4af06bc2f to your computer and use it in GitHub Desktop.
Get class name from file
<?php
/**
* Get class name from file
*
* This is a working example, that works for PHP 7.4 and 8.x.
*
* @see https://stackoverflow.com/questions/7153000/get-class-name-from-file
*/
function get_class_name(string $file): string
{
$tokens = token_get_all(file_get_contents($file));
$tokensCount = count($tokens);
$className = '';
$namespaceName = '';
for ($i = 0; $i < $tokensCount; $i++) {
// namespace token
if (is_array($tokens[$i]) && (token_name($tokens[$i][0]) === 'T_NAMESPACE')) {
for ($j = $i + 1; $j < $tokensCount; $j++) {
if ($tokens[$j][0] === ';') {
break;
}
$tokenName = token_name($tokens[$j][0]);
if ($tokenName === 'T_WHITESPACE') {
continue;
}
if (in_array($tokenName, ['T_NAME_QUALIFIED', 'T_NS_SEPARATOR', 'T_STRING'])) {
$namespaceName .= $tokens[$j][1];
}
}
}
// class token
if (is_array($tokens[$i]) && (token_name($tokens[$i][0]) === 'T_CLASS')) {
for ($j = $i + 1; $j < count($tokens); $j++) {
$tokenName = token_name($tokens[$j][0]);
if ($tokenName === 'T_WHITESPACE') {
continue;
}
if ($tokenName === 'T_STRING') {
$className = $tokens[$j][1];
break;
}
}
}
}
if (strlen($namespaceName) === 0) {
return $className;
}
return $namespaceName . '\\' . $className;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment