Skip to content

Commit

Permalink
organize exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
renanbr committed Oct 16, 2017
1 parent 49adfac commit 48c7048
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
* file that was distributed with this source code.
*/

namespace RenanBr\BibTexParser;
namespace RenanBr\BibTexParser\Exception;

class ParseException extends \RuntimeException
/**
* Interface for package exceptions.
*/
interface ExceptionInterface
{
}
30 changes: 30 additions & 0 deletions src/Exception/ParserException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the BibTex Parser.
*
* (c) Renan de Lima Barbosa <renandelima@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace RenanBr\BibTexParser\Exception;

use Exception;

class ParserException extends Exception implements ExceptionInterface
{
public function unexpectedCharacter(string $character, int $line, int $column): ParserException
{
// Avoid var_export() weird treatment for \0
$character = "\0" == $character ? "'\\0'" : var_export($character, true);

return new self(sprintf(
"Unexpected character %s at line %d column %d",
$character,
$line,
$column
));
}
}
18 changes: 18 additions & 0 deletions src/Exception/ProcessorException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the BibTex Parser.
*
* (c) Renan de Lima Barbosa <renandelima@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace RenanBr\BibTexParser\Exception;

use Exception;

class ProcessorException extends Exception implements ExceptionInterface
{
}
41 changes: 15 additions & 26 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace RenanBr\BibTexParser;

use RenanBr\BibTexParser\Exception\ParserException;

class Parser
{
public const TYPE = 'type';
Expand Down Expand Up @@ -68,9 +70,9 @@ public function addListener(ListenerInterface $listener)
}

/**
* @param string $file
* @throws RenanBr\BibTexParser\ParseException If $file given is not a valid BibTeX.
* @throws ErrorException If $file given is not readable.
* @param string $file
* @throws ParserException If $file given is not a valid BibTeX.
* @throws \ErrorException If $file given is not readable.
*/
public function parseFile($file)
{
Expand All @@ -88,8 +90,8 @@ public function parseFile($file)
}

/**
* @param string $string
* @throws RenanBr\BibTexParser\ParseException If $string given is not a valid BibTeX.
* @param string $string
* @throws ParserException If $string given is not a valid BibTeX.
*/
public function parseString($string)
{
Expand Down Expand Up @@ -119,7 +121,7 @@ private function checkFinalStatus()
// it's called when parsing has been done
// so it checks whether the status is ok or not
if (self::NONE != $this->state && self::COMMENT != $this->state) {
$this->throwException("\0");
throw ParserException::unexpectedCharacter("\0", $this->line, $this->column);
}
}

Expand Down Expand Up @@ -212,7 +214,7 @@ private function readPostType($char)
if ('{' == $char) {
$this->state = self::TAG_NAME;
} elseif (!$this->isWhitespace($char)) {
$this->throwException($char);
throw ParserException::unexpectedCharacter($char, $this->line, $this->column);
}
}

Expand Down Expand Up @@ -244,7 +246,7 @@ private function readPostTagName($char)
} elseif (',' == $char) {
$this->state = self::TAG_NAME;
} elseif (!$this->isWhitespace($char)) {
$this->throwException($char);
throw ParserException::unexpectedCharacter($char, $this->line, $this->column);
}
}

Expand All @@ -255,28 +257,28 @@ private function readPreTagContent($char)
// value defined before it, so a concatenator char is expected (or
// a comment as well)
if ($this->mayConcatenateTagContent) {
$this->throwException($char);
throw ParserException::unexpectedCharacter($char, $this->line, $this->column);
}
$this->state = self::RAW_TAG_CONTENT;
$this->readRawTagContent($char);
} elseif ('"' == $char) {
// this verification is here for the same reason of the first case
if ($this->mayConcatenateTagContent) {
$this->throwException($char);
throw ParserException::unexpectedCharacter($char, $this->line, $this->column);
}
$this->valueDelimiter = '"';
$this->state = self::QUOTED_TAG_CONTENT;
} elseif ('{' == $char) {
// this verification is here for the same reason of the first case
if ($this->mayConcatenateTagContent) {
$this->throwException($char);
throw ParserException::unexpectedCharacter($char, $this->line, $this->column);
}
$this->valueDelimiter = '}';
$this->state = self::BRACED_TAG_CONTENT;
} elseif ('#' == $char || ',' == $char || '}' == $char) {
if (!$this->mayConcatenateTagContent) {
// it expects some value
$this->throwException($char);
throw ParserException::unexpectedCharacter($char, $this->line, $this->column);
}
$this->mayConcatenateTagContent = false;
if (',' == $char) {
Expand Down Expand Up @@ -361,23 +363,10 @@ private function readOriginalEntry($char, $previousState)
private function throwExceptionIfBufferIsEmpty($char)
{
if (empty($this->buffer)) {
$this->throwException($char);
throw ParserException::unexpectedCharacter($char, $this->line, $this->column);
}
}

private function throwException($char)
{
// avoid var_export() weird treatment for \0
$char = "\0" == $char ? "'\\0'" : var_export($char, true);

throw new ParseException(sprintf(
"Unexpected character %s at line %d column %d",
$char,
$this->line,
$this->column
));
}

private function appendToBuffer($char)
{
if (empty($this->buffer)) {
Expand Down
14 changes: 7 additions & 7 deletions src/Processor/NamesProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace RenanBr\BibTexParser\Processor;

use RenanBr\BibTexParser\ParseException;
use RenanBr\BibTexParser\ProcessorException;

/**
* This class includes source code adapted from the Structures_BibTex package,
Expand Down Expand Up @@ -93,7 +93,7 @@ private function extractAuthors($entry)
if (0 == $futurecase) {
$islast = false;
}
} catch (ParseException $sbe) {
} catch (ProcessorException $sbe) {
// Ignore
}
}
Expand All @@ -110,7 +110,7 @@ private function extractAuthors($entry)
} else {
$von .= ' '.$tmparray[$j];
}
} catch (ParseException $sbe) {
} catch (ProcessorException $sbe) {
// Ignore
}
} else {
Expand All @@ -122,7 +122,7 @@ private function extractAuthors($entry)
} else {
$first .= ' '.$tmparray[$j];
}
} catch (ParseException $sbe) {
} catch (ProcessorException $sbe) {
// Ignore
}
}
Expand Down Expand Up @@ -153,7 +153,7 @@ private function extractAuthors($entry)
if (0 == $case) {
$islast = false;
}
} catch (ParseException $sbe) {
} catch (ProcessorException $sbe) {
// Ignore
}
}
Expand Down Expand Up @@ -193,7 +193,7 @@ private function extractAuthors($entry)
* - Caseless (return value -1)
*
* @param string $word
* @throws ParseException
* @throws ProcessorException
* @return int The Case
* @author Elmar Pitschke <elmar.pitschke@gmx.de>
*/
Expand Down Expand Up @@ -227,7 +227,7 @@ private function determineCase($word)
}
}
} else {
throw new ParseException('Could not determine case on word: '.(string) $word);
throw new ProcessorException('Could not determine case on word: '.(string) $word);
}

return $ret;
Expand Down
4 changes: 2 additions & 2 deletions tests/Parser/InvalidFilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use PHPUnit\Framework\TestCase;
use RenanBr\BibTexParser\Parser;
use RenanBr\BibTexParser\ParseException;
use RenanBr\BibTexParser\Exception\ParserException;

class InvalidFilesTest extends TestCase
{
Expand All @@ -35,7 +35,7 @@ public function testInvalidInputMustCauseException($file, $message)
{
$parser = new Parser();

$this->expectException(ParseException::class);
$this->expectException(ParserException::class);
$this->expectExceptionMessage($message);
$parser->parseFile($file);
}
Expand Down

0 comments on commit 48c7048

Please sign in to comment.