updated-packages

This commit is contained in:
RafficMohammed
2023-01-08 00:13:22 +05:30
parent 3ff7df7487
commit da241bacb6
12659 changed files with 563377 additions and 510538 deletions

View File

@@ -2,5 +2,4 @@
namespace TheSeer\Tokenizer;
class Exception extends \Exception {
}

View File

@@ -6,9 +6,6 @@ class NamespaceUri {
/** @var string */
private $value;
/**
* @param string $value
*/
public function __construct(string $value) {
$this->ensureValidUri($value);
$this->value = $value;
@@ -18,10 +15,10 @@ class NamespaceUri {
return $this->value;
}
private function ensureValidUri($value) {
if (strpos($value, ':') === false) {
private function ensureValidUri($value): void {
if (\strpos($value, ':') === false) {
throw new NamespaceUriException(
sprintf("Namespace URI '%s' must contain at least one colon", $value)
\sprintf("Namespace URI '%s' must contain at least one colon", $value)
);
}
}

View File

@@ -2,5 +2,4 @@
namespace TheSeer\Tokenizer;
class NamespaceUriException extends Exception {
}

View File

@@ -3,27 +3,17 @@ namespace TheSeer\Tokenizer;
class Token {
/**
* @var int
*/
/** @var int */
private $line;
/**
* @var string
*/
/** @var string */
private $name;
/**
* @var string
*/
/** @var string */
private $value;
/**
* Token constructor.
*
* @param int $line
* @param string $name
* @param string $value
*/
public function __construct(int $line, string $name, string $value) {
$this->line = $line;
@@ -31,25 +21,15 @@ class Token {
$this->value = $value;
}
/**
* @return int
*/
public function getLine(): int {
return $this->line;
}
/**
* @return string
*/
public function getName(): string {
return $this->name;
}
/**
* @return string
*/
public function getValue(): string {
return $this->value;
}
}

View File

@@ -3,86 +3,53 @@ namespace TheSeer\Tokenizer;
class TokenCollection implements \ArrayAccess, \Iterator, \Countable {
/**
* @var Token[]
*/
/** @var Token[] */
private $tokens = [];
/**
* @var int
*/
/** @var int */
private $pos;
/**
* @param Token $token
*/
public function addToken(Token $token) {
public function addToken(Token $token): void {
$this->tokens[] = $token;
}
/**
* @return Token
*/
public function current(): Token {
return current($this->tokens);
return \current($this->tokens);
}
/**
* @return int
*/
public function key(): int {
return key($this->tokens);
return \key($this->tokens);
}
/**
* @return void
*/
public function next() {
next($this->tokens);
public function next(): void {
\next($this->tokens);
$this->pos++;
}
/**
* @return bool
*/
public function valid(): bool {
return $this->count() > $this->pos;
}
/**
* @return void
*/
public function rewind() {
reset($this->tokens);
public function rewind(): void {
\reset($this->tokens);
$this->pos = 0;
}
/**
* @return int
*/
public function count(): int {
return count($this->tokens);
return \count($this->tokens);
}
/**
* @param mixed $offset
*
* @return bool
*/
public function offsetExists($offset): bool {
return isset($this->tokens[$offset]);
}
/**
* @param mixed $offset
*
* @return Token
* @throws TokenCollectionException
*/
public function offsetGet($offset): Token {
if (!$this->offsetExists($offset)) {
throw new TokenCollectionException(
sprintf('No Token at offest %s', $offset)
\sprintf('No Token at offest %s', $offset)
);
}
@@ -90,39 +57,37 @@ class TokenCollection implements \ArrayAccess, \Iterator, \Countable {
}
/**
* @param mixed $offset
* @param Token $value
*
* @throws TokenCollectionException
*/
public function offsetSet($offset, $value) {
if (!is_int($offset)) {
$type = gettype($offset);
public function offsetSet($offset, $value): void {
if (!\is_int($offset)) {
$type = \gettype($offset);
throw new TokenCollectionException(
sprintf(
\sprintf(
'Offset must be of type integer, %s given',
$type === 'object' ? get_class($value) : $type
$type === 'object' ? \get_class($value) : $type
)
);
}
if (!$value instanceof Token) {
$type = gettype($value);
$type = \gettype($value);
throw new TokenCollectionException(
sprintf(
\sprintf(
'Value must be of type %s, %s given',
Token::class,
$type === 'object' ? get_class($value) : $type
$type === 'object' ? \get_class($value) : $type
)
);
}
$this->tokens[$offset] = $value;
}
/**
* @param mixed $offset
*/
public function offsetUnset($offset) {
public function offsetUnset($offset): void {
unset($this->tokens[$offset]);
}
}

View File

@@ -2,5 +2,4 @@
namespace TheSeer\Tokenizer;
class TokenCollectionException extends Exception {
}

View File

@@ -41,7 +41,12 @@ class Tokenizer {
public function parse(string $source): TokenCollection {
$result = new TokenCollection();
$tokens = token_get_all($source);
if ($source === '') {
return $result;
}
$tokens = \token_get_all($source);
$lastToken = new Token(
$tokens[0][2],
@@ -50,7 +55,7 @@ class Tokenizer {
);
foreach ($tokens as $pos => $tok) {
if (is_string($tok)) {
if (\is_string($tok)) {
$token = new Token(
$lastToken->getLine(),
$this->map[$tok],
@@ -58,25 +63,80 @@ class Tokenizer {
);
$result->addToken($token);
$lastToken = $token;
continue;
}
$line = $tok[2];
$values = preg_split('/\R+/Uu', $tok[1]);
$values = \preg_split('/\R+/Uu', $tok[1]);
foreach ($values as $v) {
$token = new Token(
$line,
token_name($tok[0]),
\token_name($tok[0]),
$v
);
$result->addToken($token);
$line++;
$lastToken = $token;
$line++;
if ($v === '') {
continue;
}
$result->addToken($token);
}
}
return $result;
return $this->fillBlanks($result, $lastToken->getLine());
}
private function fillBlanks(TokenCollection $tokens, int $maxLine): TokenCollection {
$prev = new Token(
0,
'Placeholder',
''
);
$final = new TokenCollection();
foreach ($tokens as $token) {
if ($prev === null) {
$final->addToken($token);
$prev = $token;
continue;
}
$gap = $token->getLine() - $prev->getLine();
while ($gap > 1) {
$linebreak = new Token(
$prev->getLine() + 1,
'T_WHITESPACE',
''
);
$final->addToken($linebreak);
$prev = $linebreak;
$gap--;
}
$final->addToken($token);
$prev = $token;
}
$gap = $maxLine - $prev->getLine();
while ($gap > 0) {
$linebreak = new Token(
$prev->getLine() + 1,
'T_WHITESPACE',
''
);
$final->addToken($linebreak);
$prev = $linebreak;
$gap--;
}
return $final;
}
}

View File

@@ -5,19 +5,13 @@ use DOMDocument;
class XMLSerializer {
/**
* @var \XMLWriter
*/
/** @var \XMLWriter */
private $writer;
/**
* @var Token
*/
/** @var Token */
private $previousToken;
/**
* @var NamespaceUri
*/
/** @var NamespaceUri */
private $xmlns;
/**
@@ -32,24 +26,14 @@ class XMLSerializer {
$this->xmlns = $xmlns;
}
/**
* @param TokenCollection $tokens
*
* @return DOMDocument
*/
public function toDom(TokenCollection $tokens): DOMDocument {
$dom = new DOMDocument();
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->loadXML($this->toXML($tokens));
return $dom;
}
/**
* @param TokenCollection $tokens
*
* @return string
*/
public function toXML(TokenCollection $tokens): string {
$this->writer = new \XMLWriter();
$this->writer->openMemory();
@@ -57,12 +41,16 @@ class XMLSerializer {
$this->writer->startDocument();
$this->writer->startElement('source');
$this->writer->writeAttribute('xmlns', $this->xmlns->asString());
$this->writer->startElement('line');
$this->writer->writeAttribute('no', '1');
$this->previousToken = $tokens[0];
foreach ($tokens as $token) {
$this->addToken($token);
if (\count($tokens) > 0) {
$this->writer->startElement('line');
$this->writer->writeAttribute('no', '1');
$this->previousToken = $tokens[0];
foreach ($tokens as $token) {
$this->addToken($token);
}
}
$this->writer->endElement();
@@ -72,10 +60,7 @@ class XMLSerializer {
return $this->writer->outputMemory();
}
/**
* @param Token $token
*/
private function addToken(Token $token) {
private function addToken(Token $token): void {
if ($this->previousToken->getLine() < $token->getLine()) {
$this->writer->endElement();
@@ -87,7 +72,7 @@ class XMLSerializer {
if ($token->getValue() !== '') {
$this->writer->startElement('token');
$this->writer->writeAttribute('name', $token->getName());
$this->writer->writeRaw(htmlspecialchars($token->getValue(), ENT_NOQUOTES | ENT_DISALLOWED | ENT_XML1));
$this->writer->writeRaw(\htmlspecialchars($token->getValue(), \ENT_NOQUOTES | \ENT_DISALLOWED | \ENT_XML1));
$this->writer->endElement();
}
}