Skip to content

Commit

Permalink
Refactor SymbolTable.
Browse files Browse the repository at this point in the history
* Remove reference to non-existing "UnderflowException" class.
* Introduce "NoActiveScopeException" class.
* Add phpunit tests for SymbolTable.
  • Loading branch information
Alexander Guz committed Dec 2, 2016
1 parent 8d7ab8f commit 1265259
Show file tree
Hide file tree
Showing 4 changed files with 302 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
use PDepend\Source\Builder\Builder;
use PDepend\Source\Parser\InvalidStateException;
use PDepend\Source\Parser\MissingValueException;
use PDepend\Source\Parser\NoActiveScopeException;
use PDepend\Source\Parser\TokenStreamEndException;
use PDepend\Source\Parser\UnexpectedTokenException;
use PDepend\Source\Tokenizer\Token;
Expand Down Expand Up @@ -420,6 +421,8 @@ protected function setUpEnvironment()
/**
* Restores the parser environment back.
*
* @throws NoActiveScopeException
*
* @return void
* @since 0.9.12
*/
Expand Down Expand Up @@ -5808,6 +5811,8 @@ private function parseBoundVariables(\PDepend\Source\AST\ASTClosure $closure)
* PDepend\Source\Parser::parse();
* </code>
*
* @throws NoActiveScopeException
*
* @return string
* @link http://php.net/manual/en/language.namespaces.importing.php
*/
Expand Down Expand Up @@ -5905,6 +5910,8 @@ private function parseQualifiedNameRaw()
/**
* This method parses a PHP 5.3 namespace declaration.
*
* @throws NoActiveScopeException
*
* @return void
* @since 0.9.5
*/
Expand Down Expand Up @@ -5989,6 +5996,8 @@ protected function parseUseDeclarations()
* This method parses a single use declaration and adds a mapping between
* short name and full qualified name to the use symbol table.
*
* @throws NoActiveScopeException
*
* @return void
* @since 0.9.5
*/
Expand Down
58 changes: 58 additions & 0 deletions src/main/php/PDepend/Source/Parser/NoActiveScopeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* This file is part of PDepend.
*
* PHP Version 5
*
* Copyright (c) 2008-2015, Manuel Pichler <mapi@pdepend.org>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Manuel Pichler nor the names of his
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @copyright 2008-2015 Manuel Pichler. All rights reserved.
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
*/

namespace PDepend\Source\Parser;

/**
* This type of exception is thrown when an operation is performed
* on a symbol table and there is no active scope.
*/
class NoActiveScopeException extends ParserException
{
/**
* Constructs a new no active scope exception.
*/
public function __construct()
{
parent::__construct('No active scope in symbol table.');
}
}
75 changes: 55 additions & 20 deletions src/main/php/PDepend/Source/Parser/SymbolTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,44 +76,51 @@ public function createScope()
}

/**
* This method destorys the top most scope.
* This method destroys the top most scope.
*
* @throws NoActiveScopeException
*
* @return void
*/
public function destroyScope()
{
// Remove scope from stack
array_pop($this->scopeStack);
$this->ensureActiveScopeExists();

// Destroy current active scope
$this->scope = null;

// Update current scope to latest in stack
$this->scope = end($this->scopeStack);
// Try to restore previously active scope
if (count($this->scopeStack) > 0) {
$this->scope = array_pop($this->scopeStack);
}
}

/**
* Adds a new value to the top most scope.
*
* @param string $key The key of this scope value.
* @param mixed $value A new scope value.
*
* @throws NoActiveScopeException
*
* @return void
*/
public function add($key, $value)
{
if (is_array($this->scope) === false) {
throw new UnderflowException('No active scope.');
}
$this->scope[strtolower($key)] = $value;
$this->ensureActiveScopeExists();
$this->scope[$this->normalizeKey($key)] = $value;
}

/**
* Resets the current scope
*
* @throws NoActiveScopeException
*
* @return void
*/
public function resetScope()
{
if (is_array($this->scope) === false) {
throw new UnderflowException('No active scope.');
}
$this->ensureActiveScopeExists();
$this->scope = array();
}

Expand All @@ -122,19 +129,47 @@ public function resetScope()
* exists in the current scope. The returned value will <b>null</b> if no
* value exists for the given key.
*
* @param string $key The key for a searched scope value.
* @param string $key The key for a searched scope value.
*
* @throws NoActiveScopeException
*
* @return mixed
*/
public function lookup($key)
{
if (is_array($this->scope) === false) {
throw new UnderflowException('No active scope.');
}
$this->ensureActiveScopeExists();

$normalizedKey = $this->normalizeKey($key);

return isset($this->scope[$normalizedKey])
? $this->scope[$normalizedKey]
: null;
}

$key = strtolower($key);
if (isset($this->scope[$key])) {
return $this->scope[$key];
/**
* Checks if there is an active scope.
*
* @throws NoActiveScopeException if no active scope exists.
*
* @return void
*/
private function ensureActiveScopeExists()
{
if (null === $this->scope) {
throw new NoActiveScopeException();
}
return null;
}

/**
* Normalizes the <code>$key</code>, so it's the same for
* <code>add()</code> and <code>lookup()</code> operations.
*
* @param string $key
*
* @return string normalized key
*/
private function normalizeKey($key)
{
return strtolower($key);
}
}
Loading

0 comments on commit 1265259

Please sign in to comment.