Neos\Flow\Utility\PhpAnalyzer::extractClassName PHP Method

extractClassName() public method

Extracts the className of the given PHP code Note: This only returns the class name without namespace, @see extractFullyQualifiedClassName()
public extractClassName ( ) : string
return string
    public function extractClassName()
    {
        $tokens = token_get_all($this->phpCode);
        $numberOfTokens = count($tokens);
        for ($i = 0; $i < $numberOfTokens; $i++) {
            $token = $tokens[$i];
            if (is_string($token) || $token[0] !== T_CLASS) {
                continue;
            }
            for (++$i; $i < $numberOfTokens; $i++) {
                $token = $tokens[$i];
                if (is_string($token)) {
                    break;
                }
                list($type, $value) = $token;
                if ($type === T_STRING) {
                    return $value;
                }
                if ($type !== T_WHITESPACE) {
                    break;
                }
            }
        }
        return null;
    }

Usage Example

 /**
  * @param string $phpCode
  * @param string $namespace
  * @param string $className
  * @test
  * @dataProvider sampleClasses
  */
 public function extractClassNameTests($phpCode, $namespace, $className)
 {
     $phpAnalyzer = new PhpAnalyzer($phpCode);
     $this->assertSame($className, $phpAnalyzer->extractClassName());
 }