public static function fromCode($code)
    {
        $calculateCodeHashRefl = new \ReflectionMethod(BaseTokens::class, 'calculateCodeHash');
        $calculateCodeHashRefl->setAccessible(true);
        $hasCacheRefl = new \ReflectionMethod(BaseTokens::class, 'hasCache');
        $hasCacheRefl->setAccessible(true);
        $getCacheRefl = new \ReflectionMethod(BaseTokens::class, 'getCache');
        $getCacheRefl->setAccessible(true);

        $codeHash = $calculateCodeHashRefl->invoke(null, $code);

        if ($hasCacheRefl->invoke(null, $codeHash)) {
            /** @var Tokens $tokens */
            $tokens = $getCacheRefl->invoke(null, $codeHash);

            // generate the code to recalculate the hash
            $tokens->generateCode();

            if ($codeHash === $tokens->getCodeHash()) {
                $tokens->clearEmptyTokens();
                $tokens->clearChanged();

                return $tokens;
            }
        }

        $tokens = new static();
        $tokens->setCode($code);
        $tokens->clearChanged();

        return $tokens;
    }
public static function fromCode($code)
    {
        $calculateCodeHashRefl = new \ReflectionMethod(BaseTokens::class, 'calculateCodeHash');
        $calculateCodeHashRefl->setAccessible(true);
        $hasCacheRefl = new \ReflectionMethod(BaseTokens::class, 'hasCache');
        $hasCacheRefl->setAccessible(true);
        $getCacheRefl = new \ReflectionMethod(BaseTokens::class, 'getCache');
        $getCacheRefl->setAccessible(true);

        $codeHash = $calculateCodeHashRefl->invoke(null, $code);

        if ($hasCacheRefl->invoke(null, $codeHash)) {
            /** @var Tokens $tokens */
            $tokens = $getCacheRefl->invoke(null, $codeHash);

            // generate the code to recalculate the hash
            $tokens->generateCode();

            if ($codeHash === $tokens->getCodeHash()) {
                $tokens->clearEmptyTokens();
                $tokens->clearChanged();

                return $tokens;
            }
        }

        $tokens = new static();
        $tokens->setCode($code);
        $tokens->clearChanged();

        return $tokens;
    }
public static function fromCode ( $ code ) { $ calculateCodeHashRefl = new \ ReflectionMethod ( BaseTokens :: class , 'calculateCodeHash' ) ; $ calculateCodeHashRefl -> setAccessible ( true ) ; $ hasCacheRefl = new \ ReflectionMethod ( BaseTokens :: class , 'hasCache' ) ; $ hasCacheRefl -> setAccessible ( true ) ; $ getCacheRefl = new \ ReflectionMethod ( BaseTokens :: class , 'getCache' ) ; $ getCacheRefl -> setAccessible ( true ) ; $ codeHash = $ calculateCodeHashRefl -> invoke ( null , $ code ) ; if ( $ hasCacheRefl -> invoke ( null , $ codeHash ) ) { /** @var Tokens $tokens */ $ tokens = $ getCacheRefl -> invoke ( null , $ codeHash ) ; // generate the code to recalculate the hash $ tokens -> generateCode ( ) ; if ( $ codeHash === $ tokens -> getCodeHash ( ) ) { $ tokens -> clearEmptyTokens ( ) ; $ tokens -> clearChanged ( ) ; return $ tokens ; } } $ tokens = new static ( ) ; $ tokens -> setCode ( $ code ) ; $ tokens -> clearChanged ( ) ; return $ tokens ; }
Create token collection directly from code.

@param string $code PHP code

@return Tokens
Create token collection directly from code .