Browse Source

Change some if-the-exception blocks to assertions

This has only been done some parts of the code that are internal
to the parser at large.
ns
J. King 5 years ago
parent
commit
6b42f08fbc
  1. 12
      lib/ActiveFormattingElementsList.php
  2. 12
      lib/Data.php
  3. 8
      lib/Exception.php
  4. 14
      lib/OpenElementsStack.php
  5. 8
      lib/ParseError.php
  6. 12
      lib/Stack.php
  7. 9
      lib/Tokenizer.php
  8. 2
      tests/cases/TestTokenizer.php

12
lib/ActiveFormattingElementsList.php

@ -23,14 +23,12 @@ class ActiveFormattingElementsList extends Stack {
}
public function offsetSet($offset, $value) {
if ($offset < 0 || $offset > count($this->_storage) - 1) {
throw new Exception(Exception::STACK_INVALID_INDEX, $offset);
}
assert($offset >= 0 && $offset <= count($this->_storage), new Exception(Exception::STACK_INVALID_INDEX, $offset));
if (is_null($offset)) {
# When the steps below require the UA to push onto the list of active formatting
# elements an element element, the UA must perform the following steps:
if ($value instanceof DOMElement) {
if ($value instanceof \DOMElement) {
# 1. If there are already three elements in the list of active formatting
# elements after the last marker, if any, or anywhere in the list if there are
# no markers, that have the same tag name, namespace, and attributes as element,
@ -42,11 +40,11 @@ class ActiveFormattingElementsList extends Stack {
# order of the attributes does not matter).
$lastMarkerIndex = $this->lastMarker;
$start = ($lastMarkerIndex !== false) ? $lastMarkerIndex + 1 : 0;
$length = count($storage);
$length = count($this->_storage);
if ($start < $length - 3) {
$count = 0;
for ($i = $length - 1; $i > $start; $i--) {
$cur = $storage[$i];
$cur = $this->_storage[$i];
if ($cur->nodeName === $value->nodeName && $cur->namespaceURI === $value->namespaceURI && $cur->attributes->length === $value->attributes->length) {
$a = [];
for ($j = 0; $j < $cur->attributes->length; $cur++) {
@ -203,4 +201,4 @@ class ActiveFormattingElementsList extends Stack {
}
}
class ActiveFormattingElementMarker {}
class ActiveFormattingElementMarker {}

12
lib/Data.php

@ -68,9 +68,7 @@ class Data {
}
public function consume(int $length = 1): string {
if ($length <= 0) {
throw new Exception(Exception::DATA_INVALID_DATA_CONSUMPTION_LENGTH, $length);
}
assert($length > 0, new Exception(Exception::DATA_INVALID_DATA_CONSUMPTION_LENGTH, $length));
for ($i = 0, $string = ''; $i < $length; $i++) {
$char = $this->data->nextChar();
@ -100,9 +98,7 @@ class Data {
}
public function unconsume(int $length = 1) {
if ($length <= 0) {
throw new Exception(Exception::DATA_INVALID_DATA_CONSUMPTION_LENGTH, $length);
}
assert($length > 0, new Exception(Exception::DATA_INVALID_DATA_CONSUMPTION_LENGTH, $length));
$this->data->seek(0 - $length);
@ -135,9 +131,7 @@ class Data {
}
public function peek(int $length = 1): string {
if ($length <= 0) {
throw new Exception(Exception::DATA_INVALID_DATA_CONSUMPTION_LENGTH, $length);
}
assert($length > 0, new Exception(Exception::DATA_INVALID_DATA_CONSUMPTION_LENGTH, $length));
$string = $this->data->peekChar($length);

8
lib/Exception.php

@ -54,9 +54,7 @@ class Exception extends \Exception {
10701 => 'Method %1$s::%2$s has been disabled from %1$s'];
public function __construct(int $code, ...$args) {
if (!isset(static::$messages[$code])) {
throw new Exception(self::INVALID_CODE);
}
assert(isset(static::$message[$code]), new Exception(self::INVALID_CODE));
$message = static::$messages[$code];
$previous = null;
@ -75,9 +73,7 @@ class Exception extends \Exception {
$count = count(array_unique($matches[1]));
// If the number of replacements don't match the arguments then oops.
if (count($args) !== $count) {
throw new Exception(self::INCORRECT_PARAMETERS_FOR_MESSAGE, $count);
}
assert(count($args) === $count, new Exception(self::INCORRECT_PARAMETERS_FOR_MESSAGE, $count));
if ($count > 0) {
// Go through each of the arguments and run sprintf on the strings.

14
lib/OpenElementsStack.php

@ -11,10 +11,8 @@ class OpenElementsStack extends Stack {
// or element then we have a problem. Additionally, if the parser is created for
// parsing a fragment and the fragment context is null then we have a problem,
// too.
if ((!is_null($fragmentContext) && !$fragmentContext instanceof DOMDocumentFragment && !$fragmentContext instanceof DOMDocument && !$fragmentContext instanceof DOMElement) ||
(is_null($fragmentContext) && $fragmentCase)) {
throw new Exception(Exception::STACK_ELEMENT_DOCUMENT_DOCUMENTFRAG_EXPECTED);
}
assert(is_null($fragmentContext) || $fragmentContext instanceof \DOMDocumentFragment || $fragmentContext instanceof \DOMDocument || $fragmentContext instanceof \DOMElement,new Exception(Exception::STACK_ELEMENT_DOCUMENT_DOCUMENTFRAG_EXPECTED));
assert(!$fragmentCase || !is_null($fragmentContext), new Exception(Exception::STACK_ELEMENT_DOCUMENT_DOCUMENTFRAG_EXPECTED));
$this->fragmentCase = $fragmentCase;
$this->fragmentContext = $fragmentContext;
@ -43,7 +41,7 @@ class OpenElementsStack extends Stack {
return -1;
}
if ($needle instanceof DOMElement) {
if ($needle instanceof \DOMElement) {
foreach (array_reverse($this->_storage) as $key => $value) {
if ($value->isSameNode($needle)) {
return $key;
@ -87,9 +85,7 @@ class OpenElementsStack extends Stack {
$exclude = [$exclude];
}
if (!is_array($exclude)) {
throw new Exception(Exception::STACK_STRING_ARRAY_EXPECTED);
}
assert(is_array($exclude), new Exception(Exception::STACK_STRING_ARRAY_EXPECTED));
if (count($exclude) > 0) {
$modified = false;
@ -319,7 +315,7 @@ class OpenElementsStack extends Stack {
// Handled by loop.
foreach (array_reverse($this->_storage) as $node) {
# 2. If node is the target node, terminate in a match state.
if ($target instanceof DOMElement) {
if ($target instanceof \DOMElement) {
if ($node->isSameNode($target)) {
return true;
}

8
lib/ParseError.php

@ -49,17 +49,13 @@ class ParseError {
}
protected function prepareMessage(string $file, int $line, int $column, int $code, ...$arg): string {
if (!isset(static::$messages[$code])) {
throw new Exception(Exception::INVALID_CODE);
}
assert(isset(static::$messages[$code]), new Exception(Exception::INVALID_CODE));
$message = static::$messages[$code];
// Count the number of replacements needed in the message.
$count = substr_count($message, '%s');
// If the number of replacements don't match the arguments then oops.
if (count($arg) !== $count) {
throw new Exception(Exception::INCORRECT_PARAMETERS_FOR_MESSAGE, $count);
}
assert(count($arg) === $count, new Exception(Exception::INCORRECT_PARAMETERS_FOR_MESSAGE, $count));
if ($count > 0) {
// Convert newlines and tabs in the arguments to words to better express what they

12
lib/Stack.php

@ -8,9 +8,7 @@ abstract class Stack implements \ArrayAccess {
protected $fragmentContext;
public function offsetSet($offset, $value) {
if ($offset < 0) {
throw new Exception(Exception::STACK_INVALID_INDEX, $offset);
}
assert($offset >= 0, new Exception(Exception::STACK_INVALID_INDEX, $offset));
if (is_null($offset)) {
$this->_storage[] = $value;
@ -24,9 +22,7 @@ abstract class Stack implements \ArrayAccess {
}
public function offsetUnset($offset) {
if ($offset < 0 || $offset > count($this->_storage) - 1) {
throw new Exception(Exception::STACK_INVALID_INDEX, $offset);
}
assert($offset >= 0 && $offset < count($this->_storage), new Exception(Exception::STACK_INVALID_INDEX, $offset));
unset($this->_storage[$offset]);
// Reindex the array.
@ -34,9 +30,7 @@ abstract class Stack implements \ArrayAccess {
}
public function offsetGet($offset) {
if ($offset < 0 || $offset > count($this->_storage) - 1) {
throw new Exception(Exception::STACK_INVALID_INDEX, $offset);
}
assert($offset >= 0 && $offset < count($this->_storage), new Exception(Exception::STACK_INVALID_INDEX, $offset));
return $this->_storage[$offset];
}

9
lib/Tokenizer.php

@ -161,12 +161,9 @@ class Tokenizer {
while (true) {
if (self::$debug) {
$state = self::STATE_NAMES[$this->state] ?? "";
if ($state) {
echo "State: $state\n";
unset($state);
} else {
throw new Exception(Exception::UNKNOWN_ERROR);
}
assert($state, new Exception(Exception::UNKNOWN_ERROR));
echo "State: $state\n";
unset($state);
}
# 12.2.4.1 Data state

2
tests/cases/TestTokenizer.php

@ -45,7 +45,7 @@ class TestTokenizer extends \dW\HTML5\Test\StandardTest {
public function provideStandardTokenizerTests() {
$tests = [];
$blacklist = ["pendingSpecChanges.test", "xmlViolation.test"];
foreach (new \GlobIterator(\dW\HTML5\BASE."tests/html5lib-tests/tokenizer/*.test", \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::CURRENT_AS_PATHNAME) as $file) {
foreach (new \GlobIterator(\dW\HTML5\BASE."tests/html5lib-tests/tokenizer/test*.test", \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::CURRENT_AS_PATHNAME) as $file) {
if (!in_array(basename($file), $blacklist)) {
$tests[] = $file;
}

Loading…
Cancel
Save