From d1856a4ed3caa148ae09118e728dfd2d47642057 Mon Sep 17 00:00:00 2001 From: fzerorubigd Date: Fri, 8 Nov 2013 15:00:49 +0330 Subject: [PATCH] Cleanup. --- src/Handlebars/Autoloader.php | 4 +- src/Handlebars/Cache.php | 2 +- src/Handlebars/Context.php | 8 ++-- src/Handlebars/Handlebars.php | 5 +- src/Handlebars/Helpers.php | 89 ++++++++++++++++++----------------- src/Handlebars/Parser.php | 55 +++++++++++----------- src/Handlebars/Template.php | 18 +++---- 7 files changed, 94 insertions(+), 87 deletions(-) diff --git a/src/Handlebars/Autoloader.php b/src/Handlebars/Autoloader.php index b4d5fd2..49b640c 100755 --- a/src/Handlebars/Autoloader.php +++ b/src/Handlebars/Autoloader.php @@ -43,7 +43,7 @@ class Autoloader * @param string $baseDir Handlebars library base directory default is * __DIR__.'/..' */ - public function __construct($baseDir = null) + protected function __construct($baseDir = null) { if ($baseDir === null) { $this->_baseDir = realpath(__DIR__.'/..'); @@ -58,7 +58,7 @@ class Autoloader * @param string $baseDir Handlebars library base directory, default is * __DIR__.'/..' * - * @return Handlebars\Autoloader Registered Autoloader instance + * @return \Handlebars\Autoloader Registered Autoloader instance */ public static function register($baseDir = null) { diff --git a/src/Handlebars/Cache.php b/src/Handlebars/Cache.php index 476f31e..40785ae 100755 --- a/src/Handlebars/Cache.php +++ b/src/Handlebars/Cache.php @@ -40,7 +40,7 @@ interface Cache * * @param string $name Cache id * - * @return data on hit, boolean false on cache not found + * @return mixed data on hit, boolean false on cache not found */ public function get($name); diff --git a/src/Handlebars/Context.php b/src/Handlebars/Context.php index 85af35b..309c525 100755 --- a/src/Handlebars/Context.php +++ b/src/Handlebars/Context.php @@ -41,12 +41,12 @@ class Context protected $stack = array(); /** - * @var index stack for sections + * @var array index stack for sections */ protected $index = array(); /** - * @var key stack for objects + * @var array key stack for objects */ protected $key = array(); @@ -180,8 +180,8 @@ class Context * @param string $variableName variavle name to get from current context * @param boolean $strict strict search? if not found then throw exception * + * @throws \InvalidArgumentException in strict mode and variable not found * @return mixed - * @throws InvalidArgumentException in strict mode and variable not found */ public function get($variableName, $strict = false) { @@ -234,8 +234,8 @@ class Context * @param string $inside property/method to check * @param boolean $strict strict search? if not found then throw exception * + * @throws \InvalidArgumentException in strict mode and variable not found * @return boolean true if exist - * @throws InvalidArgumentException in strict mode and variable not found */ private function _findVariableInContext($variable, $inside, $strict = false) { diff --git a/src/Handlebars/Handlebars.php b/src/Handlebars/Handlebars.php index d75e82d..1162693 100755 --- a/src/Handlebars/Handlebars.php +++ b/src/Handlebars/Handlebars.php @@ -108,6 +108,8 @@ class Handlebars * cache => Cache object * * @param array $options array of options to set + * + * @throws \InvalidArgumentException */ public function __construct(array $options = array()) { @@ -332,6 +334,7 @@ class Handlebars * * @param callable $escape function * + * @throws \InvalidArgumentException * @return void */ public function setEscape($escape) @@ -347,7 +350,7 @@ class Handlebars /** * Get current escape function * - * @return callable + * @return array */ public function getEscapeArgs() { diff --git a/src/Handlebars/Helpers.php b/src/Handlebars/Helpers.php index f36df44..f3b2310 100755 --- a/src/Handlebars/Helpers.php +++ b/src/Handlebars/Helpers.php @@ -46,10 +46,10 @@ class Helpers /** * Create new helper container class * - * @param array $helpers array of name=>$value helpers - * @param array $defaults add defaults helper (if, unless, each,with) + * @param array $helpers array of name=>$value helpers + * @param array|bool $defaults add defaults helper (if, unless, each,with) * - * @throws InvalidArgumentException when $helpers is not an array + * @throws \InvalidArgumentException when $helpers is not an array * (or traversable) or helper is not a callable */ public function __construct($helpers = null, $defaults = true) @@ -58,7 +58,7 @@ class Helpers $this->addDefaultHelpers(); } if ($helpers != null) { - if (!is_array($helpers) && !$helpers instanceof Traversable) { + if (!is_array($helpers) && !$helpers instanceof \Traversable) { throw new \InvalidArgumentException( 'HelperCollection constructor expects an array of helpers' ); @@ -75,18 +75,17 @@ class Helpers * Needed for compatibility with PHP 5.2 since it doesn't support anonymous * functions. * - * @param Handlebars\Template $template template that is being rendered - * @param Handlebars\Context $context context object - * @param array $args passed arguments to helper - * @param string $source part of template that is wrapped - * within helper + * @param \Handlebars\Template $template template that is being rendered + * @param \Handlebars\Context $context context object + * @param array $args passed arguments to helper + * @param string $source part of template that is wrapped + * within helper * * @return mixed */ public static function helperIf($template, $context, $args, $source) { $tmp = $context->get($args); - $buffer = ''; if ($tmp) { $template->setStopToken('else'); @@ -99,6 +98,7 @@ class Helpers $template->setStopToken(false); $buffer = $template->render($context); } + return $buffer; } @@ -108,11 +108,11 @@ class Helpers * Needed for compatibility with PHP 5.2 since it doesn't support anonymous * functions. * - * @param Handlebars\Template $template template that is being rendered - * @param Handlebars\Context $context context object - * @param array $args passed arguments to helper - * @param string $source part of template that is wrapped - * within helper + * @param \Handlebars\Template $template template that is being rendered + * @param \Handlebars\Context $context context object + * @param array $args passed arguments to helper + * @param string $source part of template that is wrapped + * within helper * * @return mixed */ @@ -120,11 +120,11 @@ class Helpers { $tmp = $context->get($args); $buffer = ''; - if (is_array($tmp) || $tmp instanceof Traversable) { - $islist = ( array_keys($tmp) == range(0, count($tmp) - 1) ); + if (is_array($tmp) || $tmp instanceof \Traversable) { + $islist = (array_keys($tmp) == range(0, count($tmp) - 1)); foreach ($tmp as $key => $var) { - if ( $islist ) { + if ($islist) { $context->pushIndex($key); } else { $context->pushKey($key); @@ -132,13 +132,14 @@ class Helpers $context->push($var); $buffer .= $template->render($context); $context->pop(); - if ( $islist ) { + if ($islist) { $context->popIndex(); } else { $context->popKey(); } } } + return $buffer; } @@ -147,11 +148,11 @@ class Helpers * Needed for compatibility with PHP 5.2 since it doesn't support anonymous * functions. * - * @param Handlebars\Template $template template that is being rendered - * @param Handlebars\Context $context context object - * @param array $args passed arguments to helper - * @param string $source part of template that is wrapped - * within helper + * @param \Handlebars\Template $template template that is being rendered + * @param \Handlebars\Context $context context object + * @param array $args passed arguments to helper + * @param string $source part of template that is wrapped + * within helper * * @return mixed */ @@ -162,6 +163,7 @@ class Helpers if (!$tmp) { $buffer = $template->render($context); } + return $buffer; } @@ -170,11 +172,11 @@ class Helpers * Needed for compatibility with PHP 5.2 since it doesn't support anonymous * functions. * - * @param Handlebars\Template $template template that is being rendered - * @param Handlebars\Context $context context object - * @param array $args passed arguments to helper - * @param string $source part of template that is wrapped - * within helper + * @param \Handlebars\Template $template template that is being rendered + * @param \Handlebars\Context $context context object + * @param array $args passed arguments to helper + * @param string $source part of template that is wrapped + * within helper * * @return mixed */ @@ -184,6 +186,7 @@ class Helpers $context->push($tmp); $buffer = $template->render($context); $context->pop(); + return $buffer; } @@ -192,11 +195,11 @@ class Helpers * Needed for compatibility with PHP 5.2 since it doesn't support anonymous * functions. * - * @param Handlebars\Template $template template that is being rendered - * @param Handlebars\Context $context context object - * @param array $args passed arguments to helper - * @param string $source part of template that is wrapped - * within helper + * @param \Handlebars\Template $template template that is being rendered + * @param \Handlebars\Context $context context object + * @param array $args passed arguments to helper + * @param string $source part of template that is wrapped + * within helper * * @return mixed */ @@ -245,10 +248,10 @@ class Helpers * @param string $name helper name * @param callable $helper a function as a helper * + * @throws \InvalidArgumentException if $helper is not a callable * @return void - * @throws InvalidArgumentException if $helper is not a callable */ - public function add($name ,$helper) + public function add($name, $helper) { if (!is_callable($helper)) { throw new \InvalidArgumentException("$name Helper is not a callable."); @@ -273,14 +276,15 @@ class Helpers * * @param string $name helper name * + * @throws \InvalidArgumentException if $name is not available * @return callable helper function - * @throws InvalidArgumentException if $name is not available */ public function __get($name) { if (!$this->has($name)) { - throw new \InvalidArgumentException('Unknow helper :' . $name); + throw new \InvalidArgumentException('Unknown helper :' . $name); } + return $this->helpers[$name]; } @@ -304,18 +308,17 @@ class Helpers * @param callable $helper a function as a helper * * @return void - * @throws InvalidArgumentException if $helper is not a callable */ - public function __set($name ,$helper) + public function __set($name, $helper) { - $this->add($name, $helpers); + $this->add($name, $helper); } /** * Unset a helper * - * @param string $name helpername to remove + * @param string $name helper name to remove * * @return void */ @@ -329,8 +332,8 @@ class Helpers * * @param string $name helper name * + * @throws \InvalidArgumentException if the requested helper is not present. * @return void - * @throws InvalidArgumentException if the requested helper is not present. */ public function remove($name) { diff --git a/src/Handlebars/Parser.php b/src/Handlebars/Parser.php index 99ccc6a..62ced09 100755 --- a/src/Handlebars/Parser.php +++ b/src/Handlebars/Parser.php @@ -51,12 +51,12 @@ class Parser /** * Helper method for recursively building a parse tree. * - * @param ArrayIterator $tokens Stream of tokens + * @param \ArrayIterator $tokens Stream of tokens * + * @throws \LogicException when nesting errors or mismatched section tags + * are encountered. * @return array Token parse tree * - * @throws LogicException when nesting errors or mismatched section tags - * are encountered. */ private function _buildTree(\ArrayIterator $tokens) { @@ -70,32 +70,31 @@ class Parser continue; } else { switch ($token[Tokenizer::TYPE]) { - case Tokenizer::T_END_SECTION: - $newNodes = array (); - $continue = true; - do { - $result = array_pop($stack); - if ($result === null) { - throw new \LogicException( - 'Unexpected closing tag: /'. $token[Tokenizer::NAME] - ); - } + case Tokenizer::T_END_SECTION: + $newNodes = array(); + do { + $result = array_pop($stack); + if ($result === null) { + throw new \LogicException( + 'Unexpected closing tag: /' . $token[Tokenizer::NAME] + ); + } - if (!array_key_exists(Tokenizer::NODES, $result) - && isset($result[Tokenizer::NAME]) - && $result[Tokenizer::NAME] == $token[Tokenizer::NAME] - ) { - $result[Tokenizer::NODES] = $newNodes; - $result[Tokenizer::END] = $token[Tokenizer::INDEX]; - array_push($stack, $result); - break 2; - } else { - array_unshift($newNodes, $result); - } - } while (true); - break; - default: - array_push($stack, $token); + if (!array_key_exists(Tokenizer::NODES, $result) + && isset($result[Tokenizer::NAME]) + && $result[Tokenizer::NAME] == $token[Tokenizer::NAME] + ) { + $result[Tokenizer::NODES] = $newNodes; + $result[Tokenizer::END] = $token[Tokenizer::INDEX]; + array_push($stack, $result); + break 2; + } else { + array_unshift($newNodes, $result); + } + } while (true); + break; + default: + array_push($stack, $token); } } diff --git a/src/Handlebars/Template.php b/src/Handlebars/Template.php index 75d5755..c7c2cf5 100755 --- a/src/Handlebars/Template.php +++ b/src/Handlebars/Template.php @@ -111,7 +111,7 @@ class Template /** * get current stop token * - * @return string|false + * @return string|bool */ public function getStopToken() @@ -119,11 +119,13 @@ class Template $topStack = end($this->_stack); return $topStack[2]; } + /** * Render top tree * * @param mixed $context current context * + * @throws \RuntimeException * @return string */ public function render($context) @@ -234,6 +236,7 @@ class Template * @param Context $context current context * @param array $current section node data * + * @throws \RuntimeException * @return string the result */ private function _section(Context $context, $current) @@ -252,7 +255,7 @@ class Template } $params = array( $this, //First argument is this template - $context, //Secound is current context + $context, //Second is current context $current[Tokenizer::ARGS], //Arguments $source ); @@ -268,13 +271,13 @@ class Template // no argument at all. try { $sectionVar = $context->get($sectionName, true); - } catch (InvalidArgumentException $e) { + } catch (\InvalidArgumentException $e) { throw new \RuntimeException( $sectionName . ' is not registered as a helper' ); } $buffer = ''; - if (is_array($sectionVar) || $sectionVar instanceof Traversable) { + if (is_array($sectionVar) || $sectionVar instanceof \Traversable) { foreach ($sectionVar as $index => $d) { $context->pushIndex($index); $context->push($d); @@ -313,7 +316,7 @@ class Template if (!$data) { return $this->render($context); } else { - //No need to disacard here, since itshas no else + //No need to discard here, since it has no else return ''; } } @@ -326,7 +329,7 @@ class Template * * @return string the result */ - private function _partial($context, $current) + private function _partial(Context $context, $current) { $partial = $this->handlebars->loadPartial($current[Tokenizer::NAME]); @@ -346,7 +349,7 @@ class Template * * @return string the result */ - private function _variables($context, $current, $escaped) + private function _variables(Context $context, $current, $escaped) { $name = $current[Tokenizer::NAME]; $value = $context->get($name); @@ -366,5 +369,4 @@ class Template } return $value; } - }