Permalink
Browse files

Refactored duplicate docblock code to common base class

  • Loading branch information...
1 parent 52e4767 commit 63fa1c4e8107847edbfe457de4fa98bf5a924cf9 @mvriel mvriel committed Aug 17, 2010
@@ -208,35 +208,6 @@ protected function findStatic(DocBlox_TokenIterator $tokens)
return $tokens->findPreviousByType(T_STATIC, 5, array('{', ';'));
}
- /**
- * Returns the first docblock preceding the active token within 10 tokens.
- *
- * Please note that the iterator cursor does not change due to this method
- *
- * @param DocBlox_TokenIterator $tokens
- * @return Zend_Reflection_DocBlock|null
- */
- protected function findDocBlock(DocBlox_TokenIterator $tokens)
- {
- $result = null;
- $docblock = $tokens->findPreviousByType(T_DOC_COMMENT, 10, array('{'. '}', ';'));
- try
- {
- $result = $docblock ? new Zend_Reflection_Docblock($docblock->getContent()) : null;
- }
- catch (Exception $e)
- {
- $this->log($e->getMessage(), Zend_Log::CRIT);
- }
-
- if (!$result)
- {
- $this->log('No DocBlock was found for '.substr(get_class($this), strrpos(get_class($this), '_')+1).' '.$this->getName().' on line '.$this->getLineNumber(), Zend_Log::ERR);
- }
-
- return $result;
- }
-
protected function findVisibility(DocBlox_TokenIterator $tokens)
{
$result = 'public';
@@ -316,34 +287,6 @@ public function getEndTokenId()
return $this->token_end;
}
- protected function addDocblockToSimpleXmlElement(SimpleXMLElement $xml)
- {
- if ($this->getDocBlock())
- {
- if (!isset($xml->docblock))
- {
- $xml->addChild('docblock');
- }
- $xml->docblock->description = utf8_encode(str_replace(PHP_EOL, '<br/>', $this->getDocBlock()->getShortDescription()));
- $xml->docblock->{'long-description'} = utf8_encode(str_replace(PHP_EOL, '<br/>', $this->getDocBlock()->getLongDescription()));
-
- /** @var Zend_Reflection_Docblock_Tag $tag */
- foreach ($this->getDocBlock()->getTags() as $tag)
- {
- $tag_object = $xml->docblock->addChild('tag', utf8_encode(htmlspecialchars($tag->getDescription())));
- $tag_object['name'] = trim($tag->getName(), '@');
- if (method_exists($tag, 'getType'))
- {
- $tag_object['type'] = $tag->getType();
- }
- if (method_exists($tag, 'getVariableName'))
- {
- $tag_object['variable'] = $tag->getVariableName();
- }
- }
- }
- }
-
protected function mergeXmlToDomDocument(DOMDocument $origin, $xml)
{
$dom_arguments = new DOMDocument();
@@ -364,9 +307,14 @@ protected function mergeDomDocuments(DOMDocument $origin, DOMDocument $document)
abstract public function __toXml();
- public function getDocBlock()
+ /**
+ * Default behavior of the toString method is to return the name of this reflection.
+ *
+ * @return string
+ */
+ public function __toString()
{
- return false;
+ return $this->getName();
}
}
@@ -26,7 +26,7 @@ class DocBlox_Reflection_Argument extends DocBlox_Reflection_Abstract
* @param DocBlox_TokenIterator $tokens
* @return void
*/
- public function processGenericInformation(DocBlox_TokenIterator $tokens)
+ protected function processGenericInformation(DocBlox_TokenIterator $tokens)
{
$this->setName($tokens->current()->getContent());
$this->type = $this->findType($tokens);
@@ -1,5 +1,5 @@
<?php
-abstract class DocBlox_Reflection_BracesAbstract extends DocBlox_Reflection_Abstract
+abstract class DocBlox_Reflection_BracesAbstract extends DocBlox_Reflection_DocBlockedAbstract
{
/**
* Generic method which iterates through all tokens between the braces following the current position in the token
@@ -4,7 +4,7 @@ class DocBlox_Reflection_Class extends DocBlox_Reflection_Interface
protected $abstract = false;
protected $final = false;
- public function processGenericInformation(DocBlox_TokenIterator $tokens)
+ protected function processGenericInformation(DocBlox_TokenIterator $tokens)
{
// retrieve generic information about the class
$this->abstract = $this->findAbstract($tokens) ? true : false;
@@ -1,30 +1,24 @@
<?php
-class DocBlox_Reflection_Constant extends DocBlox_Reflection_Abstract
+class DocBlox_Reflection_Constant extends DocBlox_Reflection_DocBlockedAbstract
{
- protected $doc_block = null;
protected $value = '';
- public function processGenericInformation(DocBlox_TokenIterator $tokens)
+ protected function processGenericInformation(DocBlox_TokenIterator $tokens)
{
- $this->name = $tokens->gotoNextByType(T_STRING, 5, array('='))->getContent();
+ $this->setName($tokens->gotoNextByType(T_STRING, 5, array('='))->getContent());
+ $this->setValue($this->findDefault($tokens));
- $this->value = $this->findDefault($tokens);
- $this->doc_block = $this->findDocBlock($tokens);
+ parent::processGenericInformation($tokens);
}
- public function getValue()
- {
- return $this->value;
- }
-
- public function getDocBlock()
+ public function setValue($value)
{
- return $this->doc_block;
+ $this->value = $value;
}
- public function __toString()
+ public function getValue()
{
- return $this->getName();
+ return $this->value;
}
public function __toXml()
@@ -37,4 +31,5 @@ public function __toXml()
return $xml->asXML();
}
+
}
@@ -0,0 +1,85 @@
+<?php
+/**
+ * @author mvriel
+ * @copyright
+ */
+
+/**
+ * Abstract base class for all Reflection entities which have a docblock.
+ *
+ * @author Mike van Riel <mike.vanriel@naenius.com>
+ * @package docblox
+ * @subpackage reflection
+ */
+abstract class DocBlox_Reflection_DocBlockedAbstract extends DocBlox_Reflection_Abstract
+{
+ protected $doc_block = null;
+
+ protected function processGenericInformation(DocBlox_TokenIterator $tokens)
+ {
+ $this->doc_block = $this->findDocBlock($tokens);
+ }
+
+ public function getDocBlock()
+ {
+ return $this->doc_block;
+ }
+
+ /**
+ * Returns the first docblock preceding the active token within 10 tokens.
+ *
+ * Please note that the iterator cursor does not change due to this method
+ *
+ * @param DocBlox_TokenIterator $tokens
+ * @return Zend_Reflection_DocBlock|null
+ */
+ protected function findDocBlock(DocBlox_TokenIterator $tokens)
+ {
+ $result = null;
+ $docblock = $tokens->findPreviousByType(T_DOC_COMMENT, 10, array('{'. '}', ';'));
+ try
+ {
+ $result = $docblock ? new Zend_Reflection_Docblock($docblock->getContent()) : null;
+ }
+ catch (Exception $e)
+ {
+ $this->log($e->getMessage(), Zend_Log::CRIT);
+ }
+
+ if (!$result)
+ {
+ $this->log('No DocBlock was found for '.substr(get_class($this), strrpos(get_class($this), '_')+1).' '.$this->getName().' on line '.$this->getLineNumber(), Zend_Log::ERR);
+ }
+
+ return $result;
+ }
+
+ protected function addDocblockToSimpleXmlElement(SimpleXMLElement $xml)
+ {
+ if ($this->getDocBlock())
+ {
+ if (!isset($xml->docblock))
+ {
+ $xml->addChild('docblock');
+ }
+ $xml->docblock->description = utf8_encode(str_replace(PHP_EOL, '<br/>', $this->getDocBlock()->getShortDescription()));
+ $xml->docblock->{'long-description'} = utf8_encode(str_replace(PHP_EOL, '<br/>', $this->getDocBlock()->getLongDescription()));
+
+ /** @var Zend_Reflection_Docblock_Tag $tag */
+ foreach ($this->getDocBlock()->getTags() as $tag)
+ {
+ $tag_object = $xml->docblock->addChild('tag', utf8_encode(htmlspecialchars($tag->getDescription())));
+ $tag_object['name'] = trim($tag->getName(), '@');
+ if (method_exists($tag, 'getType'))
+ {
+ $tag_object['type'] = $tag->getType();
+ }
+ if (method_exists($tag, 'getVariableName'))
+ {
+ $tag_object['variable'] = $tag->getVariableName();
+ }
+ }
+ }
+ }
+
+}
@@ -1,5 +1,5 @@
<?php
-class DocBlox_Reflection_File extends DocBlox_Reflection_Abstract
+class DocBlox_Reflection_File extends DocBlox_Reflection_DocBlockedAbstract
{
protected $filename = '';
protected $hash = null;
@@ -10,7 +10,6 @@ class DocBlox_Reflection_File extends DocBlox_Reflection_Abstract
protected $functions = array();
protected $constants = array();
protected $includes = array();
- protected $doc_block = null;
protected $active_namespace = 'default';
protected $markers = array();
protected $marker_terms = array('TODO', 'FIXME');
@@ -60,11 +59,6 @@ public function setHash($hash)
$this->hash = $hash;
}
- public function getDocBlock()
- {
- return $this->doc_block;
- }
-
public function initializeTokens()
{
if ($this->tokens instanceof DocBlox_TokenIterator)
@@ -86,7 +80,7 @@ public function process()
unset($this->tokens);
}
- public function processGenericInformation(DocBlox_TokenIterator $tokens)
+ protected function processGenericInformation(DocBlox_TokenIterator $tokens)
{
// find file docblock; standard function does not suffice as this scans backwards and we have to make sure it isn't
// the docblock of another element
@@ -1,7 +1,6 @@
<?php
class DocBlox_Reflection_Function extends DocBlox_Reflection_BracesAbstract
{
- protected $doc_block = null;
protected $arguments_token_start = 0;
protected $arguments_token_end = 0;
@@ -12,7 +11,7 @@ protected function processGenericInformation(DocBlox_TokenIterator $tokens)
$this->setName($this->findName($tokens));
$this->resetTimer();
- $this->doc_block = $this->findDocBlock($tokens);
+ parent::processGenericInformation($tokens);
list($start_index, $end_index) = $tokens->getTokenIdsOfParenthesisPair();
$this->arguments_token_start = $start_index;
@@ -40,11 +39,6 @@ protected function findName(DocBlox_TokenIterator $tokens)
return $tokens->findNextByType(T_STRING, 5, array('{', ';'))->getContent();
}
- public function getDocBlock()
- {
- return $this->doc_block;
- }
-
public function __toXml()
{
$xml = new SimpleXMLElement('<function></function>');
@@ -63,9 +57,4 @@ public function __toXml()
return trim($dom->saveXML());
}
- public function __toString()
- {
- return $this->getName();
- }
-
}
@@ -3,7 +3,7 @@ class DocBlox_Reflection_Include extends DocBlox_Reflection_Abstract
{
protected $type = '';
- public function processGenericInformation(DocBlox_TokenIterator $tokens)
+ protected function processGenericInformation(DocBlox_TokenIterator $tokens)
{
$this->type = ucwords(strtolower(str_replace('_', ' ', substr($tokens->current()->getName(), 2))));
@@ -13,7 +13,6 @@
*/
class DocBlox_Reflection_Interface extends DocBlox_Reflection_BracesAbstract
{
- protected $doc_block = null;
protected $extends = false;
protected $extendsFrom = null;
protected $implements = false;
@@ -23,7 +22,7 @@ class DocBlox_Reflection_Interface extends DocBlox_Reflection_BracesAbstract
protected $properties = array();
protected $methods = array();
- public function processGenericInformation(DocBlox_TokenIterator $tokens)
+ protected function processGenericInformation(DocBlox_TokenIterator $tokens)
{
// retrieve generic information about the class
$this->setName($tokens->findNextByType(T_STRING, 5, array('{'))->getContent());
@@ -83,16 +82,6 @@ protected function processFunction($tokens)
$this->debugTimer('>> Processed method '.$method->getName(), 'method');
}
- /**
- * Returns the docblock associated with this class; if any
- *
- * @return Zend_Reflection_Docblock|null
- */
- public function getDocBlock()
- {
- return $this->doc_block;
- }
-
public function getParentClass()
{
return $this->extends ? $this->extendsFrom : null;
@@ -139,9 +128,4 @@ public function __toXml(SimpleXMLElement $xml = null)
return trim($dom->saveXML());
}
- public function __toString()
- {
- return $this->getName();
- }
-
}
Oops, something went wrong.

0 comments on commit 63fa1c4

Please sign in to comment.