/** * This method can be used to register an existing class in the current * class context. * * @param PHP_Depend_Code_Class $class The class instance. * * @return void */ public function registerClass(PHP_Depend_Code_Class $class) { self::$builder->restoreClass($class); }
/** * Parses an optional statement or returns <b>null</b>. * * @return PHP_Depend_Code_ASTNode * @since 0.9.8 */ private function _parseOptionalStatement() { $tokenType = $this->tokenizer->peek(); switch ($tokenType) { case self::T_ECHO: return $this->_parseEchoStatement(); case self::T_SWITCH: return $this->_parseSwitchStatement(); case self::T_TRY: return $this->_parseTryStatement(); case self::T_THROW: return $this->_parseThrowStatement(); case self::T_IF: return $this->_parseIfStatement(); case self::T_FOR: return $this->_parseForStatement(); case self::T_FOREACH: return $this->_parseForeachStatement(); case self::T_DO: return $this->_parseDoWhileStatement(); case self::T_WHILE: return $this->_parseWhileStatement(); case self::T_RETURN: return $this->_parseReturnStatement(); case self::T_BREAK: return $this->_parseBreakStatement(); case self::T_CONTINUE: return $this->_parseContinueStatement(); case self::T_GOTO: return $this->_parseGotoStatement(); case self::T_GLOBAL: return $this->_parseGlobalStatement(); case self::T_UNSET: return $this->_parseUnsetStatement(); case self::T_STRING: if ($this->tokenizer->peekNext() === self::T_COLON) { return $this->_parseLabelStatement(); } break; case self::T_CONST: return $this->_parseConstantDefinition(); case self::T_FUNCTION: return $this->_parseFunctionOrClosureDeclaration(); case self::T_COMMENT: return $this->_parseCommentWithOptionalInlineClassOrInterfaceReference(); case self::T_DOC_COMMENT: return $this->builder->buildASTComment($this->consumeToken(self::T_DOC_COMMENT)->image); case self::T_CURLY_BRACE_OPEN: return $this->_parseRegularScope(); case self::T_DECLARE: return $this->_parseDeclareStatement(); case self::T_ELSE: case self::T_ENDIF: case self::T_ELSEIF: case self::T_ENDFOR: case self::T_ENDWHILE: case self::T_ENDSWITCH: case self::T_ENDDECLARE: case self::T_ENDFOREACH: case self::T_CURLY_BRACE_CLOSE: return null; case self::T_DECLARE: return $this->_parseDeclareStatement(); case self::T_CLOSE_TAG: if (($tokenType = $this->_parseNonePhpCode()) === self::T_EOF) { return null; } return $this->_parseOptionalStatement(); case self::T_TRAIT: $package = $this->_getNamespaceOrPackage(); $package->addType($trait = $this->_parseTraitDeclaration()); $this->builder->restoreTrait($trait); $this->_sourceFile->addChild($trait); return $trait; case self::T_INTERFACE: $package = $this->_getNamespaceOrPackage(); $package->addType($interface = $this->_parseInterfaceDeclaration()); $this->builder->restoreInterface($interface); $this->_sourceFile->addChild($interface); return $interface; case self::T_CLASS: case self::T_FINAL: case self::T_ABSTRACT: $package = $this->_getNamespaceOrPackage(); $package->addType($class = $this->_parseClassDeclaration()); $this->builder->restoreClass($class); $this->_sourceFile->addChild($class); return $class; } $this->_tokenStack->push(); $stmt = $this->builder->buildASTStatement(); if (($expr = $this->_parseOptionalExpression()) != null) { $stmt->addChild($expr); } $this->_parseStatementTermination(); return $this->_setNodePositionsAndReturn($stmt); }
/** * Parses the contents of the tokenizer and generates a node tree based on * the found tokens. * * @return void */ public function parse() { // Get currently parsed source file $this->_sourceFile = $this->tokenizer->getSourceFile(); $this->_sourceFile->setCache($this->cache)->setUUID($this->_uuidBuilder->forFile($this->_sourceFile)); $hash = md5_file($this->_sourceFile->getFileName()); if ($this->cache->restore($this->_sourceFile->getUUID(), $hash)) { return; } $this->cache->remove($this->_sourceFile->getUUID()); $this->setUpEnvironment(); $this->_tokenStack->push(); // Debug currently parsed source file. PHP_Depend_Util_Log::debug('Processing file ' . $this->_sourceFile); $tokenType = $this->tokenizer->peek(); while ($tokenType !== self::T_EOF) { switch ($tokenType) { case self::T_COMMENT: $this->consumeToken(self::T_COMMENT); break; case self::T_DOC_COMMENT: $comment = $this->consumeToken(self::T_DOC_COMMENT)->image; $this->_packageName = $this->_parsePackageAnnotation($comment); $this->_docComment = $comment; break; case self::T_INTERFACE: $package = $this->_builder->buildPackage($this->_getNamespaceOrPackageName()); $package->addType($interface = $this->_parseInterfaceDeclaration()); $this->_builder->restoreInterface($interface); $this->_sourceFile->addChild($interface); break; case self::T_CLASS: case self::T_FINAL: case self::T_ABSTRACT: $package = $this->_builder->buildPackage($this->_getNamespaceOrPackageName()); $package->addType($class = $this->_parseClassDeclaration()); $this->_builder->restoreClass($class); $this->_sourceFile->addChild($class); break; case self::T_FUNCTION: $callable = $this->_parseFunctionOrClosureDeclaration(); $this->_sourceFile->addChild($callable); break; case self::T_USE: // Parse a use statement. This method has no return value but it // creates a new entry in the symbol map. $this->_parseUseDeclarations(); break; case self::T_NAMESPACE: $this->_parseNamespaceDeclaration(); break; default: // Consume whatever token $this->consumeToken($tokenType); $this->reset(); break; } $tokenType = $this->tokenizer->peek(); } $this->_sourceFile->setTokens($this->_tokenStack->pop()); $this->cache->store($this->_sourceFile->getUUID(), $this->_sourceFile, $hash); $this->tearDownEnvironment(); }