Skip to content

Commit

Permalink
#110 adding parameter and return types
Browse files Browse the repository at this point in the history
  • Loading branch information
tbela99 committed Sep 24, 2022
1 parent 8e0126a commit 93d8a5e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
37 changes: 20 additions & 17 deletions src/Element/RuleList.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ public function __get($name) {

return $this->ast->children ?? [];
}

return null;
}

/**
* @inheritDoc
*/
public function addComment($value)
public function addComment(string $value): Comment
{

$comment = new Comment();
Expand All @@ -57,10 +59,11 @@ public function addComment($value)
return $this->append($comment);
}

/**
* @inheritDoc
*/
public function computeShortHand() {
/**
* @inheritDoc
* @throws \Exception
*/
public function computeShortHand(): RuleListInterface {

foreach (new NodeWalker($this) as $event => $node) {
// ->
Expand Down Expand Up @@ -108,7 +111,7 @@ public function computeShortHand() {
/**
* @inheritDoc
*/
public function hasChildren()
public function hasChildren(): bool
{

return isset($this->ast->children) && count($this->ast->children) > 0;
Expand All @@ -117,7 +120,7 @@ public function hasChildren()
/**
* @inheritDoc
*/
public function removeChildren()
public function removeChildren(): ElementInterface
{

if (!empty($this->ast->isLeaf)) {
Expand All @@ -143,13 +146,13 @@ public function removeChildren()
/**
* @inheritDoc
*/
public function getChildren()
public function getChildren(): array
{

return $this->ast->children ?? [];
}

public function setChildren(array $elements) {
public function setChildren(array $elements): RuleListInterface {

if (!empty($this->ast->children)) {

Expand Down Expand Up @@ -194,14 +197,14 @@ public function support(ElementInterface $child): bool
/**
* @inheritDoc
*/
public function append(ElementInterface ...$elements)
public function append(ElementInterface ...$elements): ElementInterface|array
{

foreach ($elements as $element) {

if (!$this->support($element)) {

throw new InvalidArgumentException(sprintf('%s: invalid child of type %s', $this->type, $element->type), 400);
throw new InvalidArgumentException(sprintf('%s: invalid child of type %s', $this->ast->type, $element->ast->type), 400);
}

if ($element instanceof Stylesheet) {
Expand Down Expand Up @@ -229,7 +232,7 @@ public function append(ElementInterface ...$elements)
/**
* @inheritDoc
*/
public function appendCss($css) {
public function appendCss(string $css): RuleListInterface {

$this->append((new Parser($css))->parse());

Expand All @@ -239,7 +242,7 @@ public function appendCss($css) {
/**
* @inheritDoc
*/
public function insert(ElementInterface $element, $position)
public function insert(ElementInterface $element, int $position): ElementInterface
{
if (!$this->support($element) || $position < 0) {

Expand Down Expand Up @@ -268,8 +271,8 @@ public function insert(ElementInterface $element, $position)
/**
* @inheritDoc
*/
public function remove(ElementInterface $element)
{
public function remove(ElementInterface $element): ElementInterface
{

if ($element->getParent() === $this) {

Expand All @@ -289,8 +292,8 @@ public function remove(ElementInterface $element)
* return an iterator of child nodes
* @return ArrayIterator|Traversable
*/
public function getIterator()
{
public function getIterator(): Traversable|ArrayIterator
{

return new ArrayIterator($this->ast->children ?? []);
}
Expand Down
25 changes: 13 additions & 12 deletions src/Interfaces/RuleListInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use IteratorAggregate;
use TBela\CSS\Element\Comment;

/**
* Interface implemented by rules containers
Expand All @@ -18,28 +19,28 @@ interface RuleListInterface extends ElementInterface, IteratorAggregate {
* return true if the node has children
* @return bool
*/
public function hasChildren();
public function hasChildren(): bool;
/**
* return child nodes
* @return array
*/
public function getChildren();
public function getChildren(): array;

/**
* append child node
* @param ElementInterface[] $elements
* @return ElementInterface
* @throws Exception
*/
public function setChildren(array $elements);
public function setChildren(array $elements): ElementInterface;

/**
* Add a comment node
* @param string $value
* @return Element\Comment
* @return Comment
* @throws Exception
*/
public function addComment($value);
public function addComment(string $value): Comment;

/**
* check if this node accepts element as a child
Expand All @@ -51,18 +52,18 @@ public function support(ElementInterface $child): bool;
/**
* append child nodes
* @param ElementInterface|ElementInterface[] $elements
* @return ElementInterface
* @return ElementInterface|ElementInterface[]
* @throws Exception
*/
public function append(ElementInterface ...$elements);
public function append(ElementInterface ...$elements): ElementInterface|array;

/**
* append css text to this node
* @param string $css
* @return ElementInterface
* @throws Exception
*/
public function appendCss($css);
public function appendCss(string $css): ElementInterface;

/**
* insert a child node at the specified position
Expand All @@ -71,23 +72,23 @@ public function appendCss($css);
* @return ElementInterface
* @throws Exception
*/
public function insert(ElementInterface $element, $position);
public function insert(ElementInterface $element, int $position): ElementInterface;

/**
* remove a child node from its parent
* @param ElementInterface $element
* @return ElementInterface
*/
public function remove(ElementInterface $element);
public function remove(ElementInterface $element): ElementInterface;

/**
* Remove all children
* @return ElementInterface
*/
public function removeChildren();
public function removeChildren(): ElementInterface;

/**
* @return RuleListInterface
*/
public function computeShortHand();
public function computeShortHand(): RuleListInterface;
}

0 comments on commit 93d8a5e

Please sign in to comment.