/Classes/Parser/AbstractStructureParser.php
https://github.com/RichardDownes/PHPCodeParser · PHP · 608 lines · 236 code · 132 blank · 240 comment · 36 complexity · be0e603de3b9ecc89a03b61c2f9c60f8 MD5 · raw file
- <?php
- namespace Parser;
- /*
- * Copyright 2011 Richard Downes
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- use Meta\AbstractPHPStructure;
- use Meta\DocBlock;
- /**
- * Contains Parser\AbstractStructureParser
- *
- * @author Richard Downes <richard@sculpt.ie>
- * @copyright Copyright (c) 2011, Richard Downes
- * @package Parser
- */
- /**
- * Responsible for parsing a php class
- *
- * @author Richard Downes <richard@sculpt.ie>
- */
- abstract class AbstractStructureParser extends AbstractParser
- {
- /**
- * Responsible for storing the information about the structure being parsed
- *
- * @var \Meta\AbstractPHPStructure $structure Stores meta information about the structure
- */
- protected $structure;
- /**
- * The currently detected access level.
- * This will be applied to the next function or variable discovered and then reset to public
- *
- * @var string $accessLevel The currently access level
- */
- protected $accessLevel;
- /**
- * Flag indicating that the current token belongs to a static member
- *
- * @var bool $staticMember If true then the current token belongs to a static member
- */
- protected $staticMember;
- /**
- * Flag indicating that the current token belongs to a abstract member
- *
- * @var bool $abstractMember If true then the current token belongs to a abstract member
- */
- protected $abstractMember;
- /**
- * An associative with the alias/class name as key and fully qualified class name as value
- *
- * @var array $usedClassNames A list of class names taken from a use statement
- */
- protected $usedClassNames;
- /**
- * The name of the current constant being parsed, if any
- *
- * @var string $currentConstName The name of the current constant being parsed
- */
- protected $currentConstName;
- /**
- * The value of the current constant being parsed, if any
- *
- * @var string $currentConstValue The value of the current constant being parsed
- */
- protected $currentConstValue;
- /**
- * Constructs a new instance of ClassParser
- *
- */
- public function __construct()
- {
- $this->structure = $this->createPHPStructure();
- $this->abstractMember = false;
- $this->staticMember = false;
- $this->accessLevel = "public";
- $this->currentConstName = null;
- $this->currentConstValue = null;
-
- // this parser is never passed the token that caused it to be invoked
- // so we never know if this is a T_INTERFACE or T_CLASS token
- $this->currentTokenType = $this->defaultTokenType();
- $this->usedClassNames = array();
- $this->docBlock = new DocBlock();
- }
- /**
- * Sets the list of used class names
- *
- * @param array $usedClassNames A list of used class names
- * @return void
- */
- public function setUsedClassNames($usedClassNames)
- {
- $this->usedClassNames = $usedClassNames;
- }
- /**
- * Factory method to create the PHP structure
- *
- * @return \Meta\AbstractPHPStructure
- */
- protected abstract function createPHPStructure();
- /**
- * Factory method to create the default token type
- *
- * @return int
- */
- protected abstract function defaultTokenType();
- /**
- * Responsible for handling any text tokens
- *
- * @param string $text The text token
- * @return void
- */
- protected function textHandler($text)
- {
- if ($this->currentTokenType == $this->defaultTokenType())
- {
- $this->parseStructureName($text);
- }
- else if ($text != "}")
- {
- $this->parseStructureInheritance($text);
- }
- else
- {
- $this->stopParsing();
- }
- }
- /**
- * Responsible for handling any T_EXTENDS tokens
- *
- * @param int $id The id of the token
- * @param string $text The text of the token
- * @return void
- */
- protected function extendsHandler($id, $text)
- {
- $text = null; // not needed
- $this->currentTokenType = $id;
- }
- /**
- * Responsible for handling any T_IMPLEMENTS tokens
- *
- * @param int $id The id of the token
- * @param string $text The text of the token
- * @return void
- */
- protected function implementsHandler($id, $text)
- {
- $text = null; // not needed
- $this->currentTokenType = $id;
- }
- /**
- * Responsible for handling any T_STATIC tokens
- *
- * @param int $id The id of the token
- * @param string $text The text of the token
- * @return void
- */
- protected function staticHandler($id, $text)
- {
- $id = null;
- $text = null; // not needed
- $this->staticMember = true;
- }
- /**
- * Responsible for handling any T_ABSTRACT tokens
- *
- * @param int $id The id of the token
- * @param string $text The text of the token
- * @return void
- */
- protected function abstractHandler($id, $text)
- {
- $text = null;
- $id = null;
- $this->abstractMember = true;
- }
- /**
- * Responsible for handling and T_PROTECTED tokens
- *
- * @param int $id The id of the token
- * @param string $text The text of the token
- * @return void
- */
- protected function protectedHandler($id, $text)
- {
- $text = null; // not needed
- $this->currentTokenType = $id;
- $this->accessLevel = "protected";
- }
- /**
- * Responsible for handling and T_PUBLIC tokens
- *
- * @param int $id The id of the token
- * @param string $text The text of the token
- * @return void
- */
- protected function publicHandler($id, $text)
- {
- $text = null; // not needed
- $this->currentTokenType = $id;
- $this->accessLevel = "public";
- }
- /**
- * Responsible for handling and T_PRIVATE tokens
- *
- * @param int $id The id of the token
- * @param string $text The text of the token
- * @return void
- */
- protected function privateHandler($id, $text)
- {
- $text = null; // not needed
- $this->currentTokenType = $id;
- $this->accessLevel = "private";
- }
- /**
- * Responsible for handing and T_FUNCTION tokens
- *
- * @param int $id The id of the token
- * @param string $text The text of the token
- * @return void
- */
- protected function functionHandler($id, $text)
- {
- $id = null;
- $text = null;
- $parser = $this->createFunctionParser();
- // set the default values for the parsers function object
- $parser->phpFunction()->setAccessLevel($this->accessLevel);
- $parser->phpFunction()->setStatic($this->staticMember);
- $parser->phpFunction()->setAbstract($this->abstractMember);
- $parser->phpFunction()->setDocBlock($this->docBlock);
- // parse the function
- $parser->parse($this->tokens);
- $this->structure->addMethod($parser->phpFunction());
- // reset
- $this->reset();
- }
- /**
- * Responsible for creating a function parser
- *
- * @return FunctionParser
- */
- protected abstract function createFunctionParser();
- /**
- * Responsible for creating a variable parser
- *
- * @return VariableParser
- */
- protected abstract function createVariableParser();
- /**
- * Responsible for handing and T_VARIABLE tokens
- *
- */
- protected function variableHandler($id, $text)
- {
- $parser = $this->createVariableParser();
- $parser->variable()->setName(str_replace("\$", "", $text));
- $parser->variable()->setStatic($this->staticMember);
- $parser->variable()->setDocBlock($this->docBlock);
- $parser->variable()->setAccessLevel($this->accessLevel);
- $parser->parse($this->tokens);
- $this->structure->addVariable($parser->variable());
- $this->currentTokenType = $id;
- $this->reset();
- }
- /**
- * Responsible for handling and T_CONST tokens
- *
- */
- protected function constHandler($id, $text)
- {
- $this->currentTokenType = $id;
- $text = null;
- }
- /**
- * Responsible for handling any T_CONSTANT_ENCAPSED_STRING tokens
- *
- */
- protected function constantEncapsedStringHandler($id, $text)
- {
- $id = null;
- if ($this->currentTokenType == T_CONST)
- {
- $this->currentConstValue = $text;
- }
- }
- /**
- * Responsible for handling any T_LNUMBER tokens (integers)
- *
- */
- protected function lnumberHandler($id, $text)
- {
- $id = null;
- if ($this->currentTokenType == T_CONST)
- {
- $this->currentConstValue = $text;
- }
- }
- /**
- * Responsible for handling any T_DNUMBER tokens (decimals)
- *
- */
- protected function dnumberHandler($id, $text)
- {
- $id = null;
- if ($this->currentTokenType == T_CONST)
- {
- $this->currentConstValue = $text;
- }
- }
- /**
- * Gets a reference to the PHP class
- *
- * @return \Meta\AbstractPHPStructure
- */
- public function &structure()
- {
- return $this->structure;
- }
- /**
- * Sets the PHP structure
- *
- * @param \Meta\AbstractPHPStructure $structure The new structure
- * @return void
- */
- public function setStructure(AbstractPHPStructure $structure)
- {
- $this->structure = $structure;
- }
- /**
- * Gets the PHP structure
- *
- * @return \Meta\AbstractPHPStructure
- */
- public function getStructure()
- {
- return $this->structure;
- }
- /**
- * Responsible for parsing the structures name
- *
- * @param string $text The text to parse the name frmo
- * @return void
- */
- protected function parseStructureName($text)
- {
- if ($text == "{")
- {
- $this->currentTokenType = -1;
- }
- else
- {
- $this->structure->setName($text);
- }
- }
- /**
- * Responsible for parsing the inheritance of a structure
- *
- * @param string $text The text to parse
- * @return void
- */
- protected function parseStructureInheritance($text)
- {
- if ($this->currentTokenType == T_EXTENDS)
- {
- $this->parseExtends($text);
- }
- else if ($this->currentTokenType == T_IMPLEMENTS)
- {
- $this->parseInterfaces($text);
- }
- else if ($this->currentTokenType == T_CONST)
- {
- $this->parseConstant($text);
- }
- }
- /**
- * Responsible for parsing the parent of a structure
- *
- * @var string $text The text to parse
- * @return void
- */
- protected function parseExtends($text)
- {
- if ($text != "{")
- {
- $parent = $this->resolveClassName($text);
- $this->structure->setParent($parent);
- }
- }
- /**
- * Parses a token from the class interface list
- *
- * @param string $text The text to parse
- * @return void
- */
- protected function parseInterfaces($text)
- {
- if ($text == "{")
- {
- $this->parsingInterfaces = false;
- }
- else if ($text != ",")
- {
- $interfaceName = $this->resolveClassName($text);
- $this->structure->addInterface($interfaceName);
- }
-
- }
- /**
- * Responsible for resetting the stored member properties
- *
- * @return void
- */
- protected function reset()
- {
- $this->docBlock = new DocBlock;
- $this->abstractMember = false;
- $this->accessLevel = "";
- $this->staticMember = false;
- }
- /**
- * Given a class name it will resolve it to a fully qualified on
- *
- * @param string $className The name of the class
- * @return string
- */
- protected function resolveClassName($className)
- {
- // if the class name exists in the list of used classes then
- // its from a separate namespace, otherwise prefix it with the
- // current namespace
- if (array_key_exists($className, $this->usedClassNames))
- {
- $className = $this->usedClassNames[$className];
- }
- else if (substr($className, 0, 1) != "\\")
- {
- $className = "{$this->structure()->getNamespace()}\\{$className}";
- }
- return $className;
- }
- /**
- * Responsible for parsing a class constant
- *
- * @param string $text The text to parse
- * @return void
- */
- protected function parseConstant($text)
- {
- if ($text == ";")
- {
- $this->currentConstName = null;
- $this->currentConstValue = null;
- $this->currentTokenType = -1;
- }
- else
- {
- if ($text != "=")
- {
- $this->currentConstName = $text;
- }
- }
- }
- }