From 944cdd0cf21655ceb1bd3262dee9b33602020c09 Mon Sep 17 00:00:00 2001 From: Thomas Weinert Date: Sun, 25 Apr 2021 16:14:13 +0200 Subject: [PATCH] [REFACTOR] Add Types --- src/PhpCss.php | 28 ++++++++------- src/PhpCss/Ast/Selector/Group.php | 12 ++++--- src/PhpCss/Ast/Selector/Sequence.php | 7 ++-- .../Ast/Selector/Simple/PseudoClass.php | 2 +- src/PhpCss/Ast/Visitor/Explain.php | 26 ++++++-------- .../Exception/InvalidCharacterException.php | 24 +++++++------ .../Exception/NotConvertibleException.php | 7 ++-- src/PhpCss/Exception/ParserException.php | 35 ++++++++++--------- src/PhpCss/Exception/TokenException.php | 4 +-- .../Exception/TokenMismatchException.php | 2 +- src/PhpCss/Parser.php | 5 +-- src/PhpCss/Scanner/Status/Selector.php | 2 +- .../Scanner/Status/Selector/Attribute.php | 2 +- src/PhpCss/Scanner/Status/Text/Double.php | 2 +- 14 files changed, 79 insertions(+), 79 deletions(-) diff --git a/src/PhpCss.php b/src/PhpCss.php index 893a585..8ecab5b 100644 --- a/src/PhpCss.php +++ b/src/PhpCss.php @@ -14,12 +14,13 @@ abstract class PhpCss { /** - * Parses a css selector and compiles it into an css selector again - * - * @param string $cssSelector - * @return string - */ - public static function reformat($cssSelector) { + * Parses a css selector and compiles it into an css selector again + * + * @param string $cssSelector + * @return string + * @throws ParserException + */ + public static function reformat(string $cssSelector): string { $ast = self::getAst($cssSelector); $visitor = new PhpCss\Ast\Visitor\Css(); $ast->accept($visitor); @@ -27,13 +28,14 @@ public static function reformat($cssSelector) { } /** - * Parses a css selector and transforms it into an xpath expression - * - * @param string $cssSelector - * @param int $options - * @return string - */ - public static function toXpath($cssSelector, $options = 0) { + * Parses a css selector and transforms it into an xpath expression + * + * @param string $cssSelector + * @param int $options + * @return string + * @throws ParserException + */ + public static function toXpath(string $cssSelector, int $options = 0): string { $ast = self::getAst($cssSelector); $visitor = new PhpCss\Ast\Visitor\Xpath($options); $ast->accept($visitor); diff --git a/src/PhpCss/Ast/Selector/Group.php b/src/PhpCss/Ast/Selector/Group.php index ca42f80..fa14434 100644 --- a/src/PhpCss/Ast/Selector/Group.php +++ b/src/PhpCss/Ast/Selector/Group.php @@ -8,7 +8,11 @@ namespace PhpCss\Ast\Selector { + use ArrayAccess; + use ArrayIterator; + use Countable; use InvalidArgumentException; + use IteratorAggregate; use PhpCss\Ast; /** @@ -25,7 +29,7 @@ */ class Group extends Ast\Selector - implements \ArrayAccess, \Countable, \IteratorAggregate { + implements ArrayAccess, Countable, IteratorAggregate { private $_sequences = []; @@ -113,11 +117,11 @@ public function count(): int { /** * Return an iterator for the sequences * - * @return \Traversable + * @return ArrayIterator * @see IteratorAggregate::getIterator() */ - public function getIterator() { - return new \ArrayIterator($this->_sequences); + public function getIterator(): ArrayIterator { + return new ArrayIterator($this->_sequences); } /** diff --git a/src/PhpCss/Ast/Selector/Sequence.php b/src/PhpCss/Ast/Selector/Sequence.php index bcc997e..d502114 100644 --- a/src/PhpCss/Ast/Selector/Sequence.php +++ b/src/PhpCss/Ast/Selector/Sequence.php @@ -7,10 +7,10 @@ class Sequence extends Ast\Selector { /** - * @var array(Simple) + * @var Simple[] */ public $simples = []; - public $combinator = NULL; + public $combinator; /** * @param Simple[] $simples @@ -30,9 +30,6 @@ public function __construct(array $simples = [], Combinator $combinator = NULL) public function accept(Ast\Visitor $visitor): void { if ($visitor->visitEnter($this)) { foreach ($this->simples as $simple) { - /** - * @var Simple $simple - */ $simple->accept($visitor); } if (isset($this->combinator)) { diff --git a/src/PhpCss/Ast/Selector/Simple/PseudoClass.php b/src/PhpCss/Ast/Selector/Simple/PseudoClass.php index 4e00b6d..dd4ec1c 100644 --- a/src/PhpCss/Ast/Selector/Simple/PseudoClass.php +++ b/src/PhpCss/Ast/Selector/Simple/PseudoClass.php @@ -7,7 +7,7 @@ class PseudoClass extends Ast\Selector\Simple { public $name = ''; - public $parameter = NULL; + public $parameter; public function __construct(string $name, Ast\Node $parameter = NULL) { $this->name = $name; diff --git a/src/PhpCss/Ast/Visitor/Explain.php b/src/PhpCss/Ast/Visitor/Explain.php index c458c3c..e14e237 100644 --- a/src/PhpCss/Ast/Visitor/Explain.php +++ b/src/PhpCss/Ast/Visitor/Explain.php @@ -11,8 +11,6 @@ use DOMDocument; use DOMElement; - use DOMNode; - use LogicException; use PhpCss\Ast; /** @@ -25,12 +23,12 @@ class Explain extends Overload { /** * @var DOMDocument */ - private $_document = NULL; + private $_document; /** - * @var DOMElement + * @var DOMElement|DOMDocument */ - private $_current = NULL; + private $_current; public function __construct() { $this->clear(); @@ -51,15 +49,16 @@ public function __toString() { } /** - * @param $name + * @param string $name * @param string $content * @param array $attributes - * @return DOMNode + * @param string $contentType + * @return DOMElement */ private function appendElement( - $name, $content = '', array $attributes = [], $contentType = 'text' - ): DOMNode { - $result = $this->_document->createElementNs($this->_xmlns, $name); + string $name, string $content = '', array $attributes = [], string $contentType = 'text' + ): DOMElement { + $result = $this->_document->createElementNS($this->_xmlns, $name); if (!empty($content)) { $text = $result->appendChild( $this->_document->createElementNs($this->_xmlns, $contentType) @@ -82,10 +81,9 @@ private function appendElement( } /** - * @param $content - * @return DOMNode + * @param string $content */ - private function appendText($content): DOMNode { + private function appendText(string $content): void { $text = $this->_current->appendChild( $this->_document->createElementNs($this->_xmlns, 'text') ); @@ -98,7 +96,6 @@ private function appendText($content): DOMNode { $this->_document->createTextNode($content) ); } - return $text; } /** @@ -128,7 +125,6 @@ private function end(): bool { * If the buffer already contains data, throw an exception. * * @return boolean - * @throws LogicException */ public function visitEnterSelectorGroup(): bool { $this->start($this->appendElement('selector-group')); diff --git a/src/PhpCss/Exception/InvalidCharacterException.php b/src/PhpCss/Exception/InvalidCharacterException.php index 4a759dc..0d7bad9 100644 --- a/src/PhpCss/Exception/InvalidCharacterException.php +++ b/src/PhpCss/Exception/InvalidCharacterException.php @@ -11,28 +11,30 @@ namespace PhpCss\Exception { use PhpCss; + use PhpCss\Scanner\Status; + use UnexpectedValueException; /** * Exception thrown if a scanner status finds does not * find a valid character. */ class InvalidCharacterException - extends \UnexpectedValueException + extends UnexpectedValueException implements PhpCssException { /** @var int $offset byte offset */ - private $_offset = 0; + private $_offset; /** @var string $buffer string buffer */ - private $_buffer = ''; - /** @var PhpCss\Scanner\Status $status scanner status */ - private $_status = ''; + private $_buffer; + /** @var Status $status scanner status */ + private $_status; /** * @param string $buffer * @param int $offset - * @param \PhpCss\Scanner\Status $status + * @param Status $status */ - public function __construct($buffer, $offset, $status) { + public function __construct(string $buffer, int $offset, Status $status) { $this->_buffer = $buffer; $this->_offset = $offset; $this->_status = $status; @@ -52,22 +54,22 @@ public function __construct($buffer, $offset, $status) { * * @return string */ - public function getChar() { + public function getChar(): string { if (preg_match('(.)suS', $this->_buffer, $match, 0, $this->_offset)) { return $match[0]; } return ''; } - public function getOffset() { + public function getOffset(): int { return $this->_offset; } - public function getBuffer() { + public function getBuffer(): string { return $this->_buffer; } - public function getStatus() { + public function getStatus(): Status { return $this->_status; } } diff --git a/src/PhpCss/Exception/NotConvertibleException.php b/src/PhpCss/Exception/NotConvertibleException.php index 6aae754..b87adf9 100644 --- a/src/PhpCss/Exception/NotConvertibleException.php +++ b/src/PhpCss/Exception/NotConvertibleException.php @@ -8,19 +8,20 @@ namespace PhpCss\Exception { + use Exception; use PhpCss; /** * Exception thrown if a visitor finds something that it can not use. */ class NotConvertibleException - extends \Exception + extends Exception implements PhpCssException { - public function __construct($token, $target) { + public function __construct(string $source, string $target) { parent::__construct( sprintf( - 'Can not convert %s to %s.', (string)$token, (string)$target + 'Can not convert %s to %s.', $source, $target ) ); } diff --git a/src/PhpCss/Exception/ParserException.php b/src/PhpCss/Exception/ParserException.php index 59a7d53..649ec34 100644 --- a/src/PhpCss/Exception/ParserException.php +++ b/src/PhpCss/Exception/ParserException.php @@ -1,31 +1,32 @@ _expectedTokens; } } diff --git a/src/PhpCss/Exception/TokenException.php b/src/PhpCss/Exception/TokenException.php index f2d181c..ae32eed 100644 --- a/src/PhpCss/Exception/TokenException.php +++ b/src/PhpCss/Exception/TokenException.php @@ -28,7 +28,7 @@ abstract class TokenException extends ParserException { */ protected $_encounteredToken; - public function __construct(PhpCss\Scanner\Token $token, $message) { + public function __construct(PhpCss\Scanner\Token $token, string $message) { $this->_encounteredToken = $token; parent::__construct($message); } @@ -38,7 +38,7 @@ public function __construct(PhpCss\Scanner\Token $token, $message) { * * @return PhpCss\Scanner\Token */ - public function getToken() { + public function getToken(): PhpCss\Scanner\Token { return $this->_encounteredToken; } } diff --git a/src/PhpCss/Exception/TokenMismatchException.php b/src/PhpCss/Exception/TokenMismatchException.php index a9b18e4..03e4763 100644 --- a/src/PhpCss/Exception/TokenMismatchException.php +++ b/src/PhpCss/Exception/TokenMismatchException.php @@ -29,7 +29,7 @@ public function __construct(PhpCss\Scanner\Token $encounteredToken, array $expec parent::__construct( $encounteredToken, - 'Parse error: Found '.(string)$encounteredToken . + 'Parse error: Found '. $encounteredToken . ' while one of '.implode(", ", $expectedTokenStrings).' was expected.' ); } diff --git a/src/PhpCss/Parser.php b/src/PhpCss/Parser.php index 53188d6..fdf2cdf 100644 --- a/src/PhpCss/Parser.php +++ b/src/PhpCss/Parser.php @@ -10,7 +10,6 @@ namespace PhpCss { use PhpCss\Exception\ParserException; - use PhpCss\Exception\PhpCssException; /** * Abstract class implementing functionality to ease parsing in extending @@ -195,7 +194,6 @@ protected function ignore($expectedTokens): bool { foreach ($expectedTokens as $token) { if ($found = $this->matchToken($position, $token)) { ++$position; - continue; } } if ($found) { @@ -208,9 +206,8 @@ protected function ignore($expectedTokens): bool { if ($position > 0) { array_splice($this->_tokens, 0, $position); return TRUE; - } else { - return FALSE; } + return FALSE; } /** diff --git a/src/PhpCss/Scanner/Status/Selector.php b/src/PhpCss/Scanner/Status/Selector.php index c5169d3..255b751 100644 --- a/src/PhpCss/Scanner/Status/Selector.php +++ b/src/PhpCss/Scanner/Status/Selector.php @@ -51,7 +51,7 @@ class Selector extends Scanner\Status { * @param integer $offset * @return Scanner\Token */ - public function getToken($buffer, $offset): ?Scanner\Token { + public function getToken(string $buffer, int $offset): ?Scanner\Token { if ($token = $this->matchCharacters($buffer, $offset, $this->_tokenChars)) { return $token; } diff --git a/src/PhpCss/Scanner/Status/Selector/Attribute.php b/src/PhpCss/Scanner/Status/Selector/Attribute.php index 668bbae..647afe4 100644 --- a/src/PhpCss/Scanner/Status/Selector/Attribute.php +++ b/src/PhpCss/Scanner/Status/Selector/Attribute.php @@ -43,7 +43,7 @@ class Attribute extends Scanner\Status { * @param integer $offset * @return Scanner\Token */ - public function getToken($buffer, $offset): ?Scanner\Token { + public function getToken(string $buffer, int $offset): ?Scanner\Token { if ($token = $this->matchCharacters($buffer, $offset, $this->_tokenChars)) { return $token; } diff --git a/src/PhpCss/Scanner/Status/Text/Double.php b/src/PhpCss/Scanner/Status/Text/Double.php index 0a7435c..224f17c 100644 --- a/src/PhpCss/Scanner/Status/Text/Double.php +++ b/src/PhpCss/Scanner/Status/Text/Double.php @@ -22,7 +22,7 @@ class Double extends Scanner\Status { * @param integer $offset * @return Scanner\Token */ - public function getToken($buffer, $offset): ?Scanner\Token { + public function getToken(string $buffer, int $offset): ?Scanner\Token { if ('"' === substr($buffer, $offset, 1)) { return new Scanner\Token( Scanner\Token::DOUBLEQUOTE_STRING_END, '"', $offset