Skip to content

Commit

Permalink
[REFACTOR] PHP 8 Syntax, Add Types
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasWeinert committed Oct 16, 2023
1 parent 23f02ff commit d51617a
Show file tree
Hide file tree
Showing 28 changed files with 110 additions and 280 deletions.
74 changes: 11 additions & 63 deletions src/FluentDOM/Creator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,14 @@
*/
class Creator {

/**
* @var DOM\Document
*/
private $_document;
private DOM\Document $_document;

/**
* @var bool
*/
private $_optimizeNamespaces = TRUE;
private bool $_optimizeNamespaces = TRUE;

/**
* @param string $version
* @param string $encoding
*/
public function __construct(string $version = '1.0', string $encoding = 'UTF-8') {
$this->_document = new DOM\Document($version, $encoding);
}

/**
* @param string $name
* @return bool
*/
public function __isset(string $name): bool {
return match ($name) {
'document' => true,
Expand All @@ -51,10 +37,6 @@ public function __isset(string $name): bool {
};
}

/**
* @param string $name
* @return mixed
*/
public function __get(string $name): mixed {
return match ($name) {
'document' => $this->_document,
Expand All @@ -65,41 +47,28 @@ public function __get(string $name): mixed {
}

public function __set(string $name, mixed $value): void {
switch ($name) {
case 'formatOutput' :
$this->_document->{$name} = $value;
return;
case 'optimizeNamespaces' :
$this->_optimizeNamespaces = (bool)$value;
return;
}
throw new UndeclaredPropertyError($this, $name);
match ($name) {
'formatOutput' => $this->_document->{$name} = $value,
'optimizeNamespaces' => $this->_optimizeNamespaces = (bool)$value,
default => throw new UndeclaredPropertyError($this, $name)
};
}

/**
* If the creator is cloned, a clone of the dom document is needed, too.
*
*/
public function __clone(): void {
$this->_document = clone $this->_document;
}

/**
* @param string $prefix
* @param string $namespaceURI
* @throws \LogicException
*/
public function registerNamespace(string $prefix, string $namespaceURI): void {
$this->_document->registerNamespace($prefix, $namespaceURI);
}

/**
* @param string $name
* @param mixed ...$parameters
* @return Creator\Node
* @throws \LogicException
* @throws \LogicException|Exceptions\UnattachedNode|\DOMException
*/
public function __invoke(string $name, ...$parameters): Creator\Node {
public function __invoke(string $name, mixed ...$parameters): Creator\Node {
return new Creator\Node(
$this,
$this->_document,
Expand All @@ -118,48 +87,27 @@ public function __invoke(string $name, ...$parameters): Creator\Node {
* - FluentDOM\Appendable instances are appended
* - Strings or objects castable to string are appended as text nodes
*
* @param string $name
* @param mixed ...$parameters
* @return DOM\Element
* @throws \LogicException
* @throws Exceptions\UnattachedNode
* @throws Exceptions\UnattachedNode|\DOMException
*/
public function element(string $name, ...$parameters): DOM\Element {
public function element(string $name, mixed ...$parameters): DOM\Element {
$node = $this->_document->createElement($name);
$node->append(...$parameters);
return $node;
}

/**
* @param string $content
* @return DOM\CdataSection
*/
public function cdata(string $content): DOM\CdataSection {
return $this->_document->createCdataSection($content);
}

/**
* @param string $content
* @return DOM\Comment
*/
public function comment(string $content): DOM\Comment {
return $this->_document->createComment($content);
}

/**
* @param string $target
* @param string $content
* @return DOM\ProcessingInstruction
*/
public function pi(string $target, string $content): DOM\ProcessingInstruction {
return $this->_document->createProcessingInstruction($target, $content);
}

/**
* @param iterable $traversable
* @param callable|NULL $map
* @return Appendable
*/
public function each(iterable $traversable, callable $map = NULL): Appendable {
return new Creator\Nodes($traversable, $map);
}
Expand Down
4 changes: 2 additions & 2 deletions src/FluentDOM/Creator/Nodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Nodes implements Appendable, \OuterIterator {

private iterable $_iterable;

private ?\Closure $_map = NULL;
private ?\Closure $_map;

private ?\Iterator $_iterator = NULL;

Expand Down Expand Up @@ -78,7 +78,7 @@ public function valid(): bool {
public function appendTo(Element $parentNode): void {
try {
$parentNode->append(...$this);
} catch (UnattachedNode $e) {
} catch (UnattachedNode) {
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/FluentDOM/DOM/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,7 @@ public function getElementsByTagName(string $qualifiedName): \DOMNodeList {
}

/**
* @param string|null $qualifiedName
* @param string|null $publicId
* @param string|null $systemId
* @return \DOMDocumentType
* @throws \DOMException
*/
public function createDocumentType(
string $qualifiedName = NULL, string $publicId = NULL, string $systemId = NULL
Expand Down
2 changes: 1 addition & 1 deletion src/FluentDOM/DOM/DocumentFragment.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function appendXml(string $data, iterable|\DOMElement $namespaces = NULL)
/**
* Append an child element
*
* @throws \LogicException
* @throws \LogicException|\DOMException
*/
public function appendElement(
string $name, string|array $content = '', array $attributes = NULL
Expand Down
7 changes: 1 addition & 6 deletions src/FluentDOM/DOM/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,9 @@ public function removeAttribute(string $qualifiedName): bool {
if ($namespaceURI !== '') {
return $this->removeAttributeNS($namespaceURI, $localName);
}
return (bool)parent::removeAttribute($qualifiedName);
return parent::removeAttribute($qualifiedName);
}

/**
* @param string $namespace
* @param string $localName
* @return bool
*/
#[\ReturnTypeWillChange]
public function removeAttributeNS(?string $namespace, string $localName): bool {
if ($namespace === self::NAMESPACE_XMLNS) {
Expand Down
6 changes: 1 addition & 5 deletions src/FluentDOM/DOM/Node/ParentNode/Properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ public function __isset(string $name): bool {
};
}

/**
* @param string $name
* @return mixed
*/
public function __get(string $name) {
public function __get(string $name): mixed {
switch ($name) {
case 'firstElementChild' :
return $this->getFirstElementChild();
Expand Down
8 changes: 2 additions & 6 deletions src/FluentDOM/DOM/Node/WholeText.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* FluentDOM
*
* @link https://thomas.weinert.info/FluentDOM/
* @copyright Copyright 2009-2021 FluentDOM Contributors
* @copyright Copyright 2009-2023 FluentDOM Contributors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*
*/
Expand Down Expand Up @@ -33,11 +33,7 @@
*/
trait WholeText {

/**
* @param string $content
* @return $this|Text|CdataSection|NULL
*/
public function replaceWholeText($content) {
public function replaceWholeText($content): static|NULL|Text|CdataSection {
$canReplaceEntity = static function(\DOMEntityReference $reference) use (&$canReplaceEntity) {
foreach ($reference->firstChild->childNodes as $childNode) {
$canReplace = FALSE;
Expand Down
12 changes: 5 additions & 7 deletions src/FluentDOM/DOM/Node/Xpath.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
/**
/*
* FluentDOM
*
* @link https://thomas.weinert.info/FluentDOM/
* @copyright Copyright 2009-2019 FluentDOM Contributors
* @copyright Copyright 2009-2023 FluentDOM Contributors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*
*/
Expand All @@ -26,12 +26,10 @@ trait Xpath {
/**
* Evaluate an xpath expression in the context of this
* element.
*
* @param string $expression
* @param Node|\DOMNode|NULL $context
* @return string|float|\DOMNodeList|Node[]
*/
public function evaluate(string $expression, Node $context = NULL) {
public function evaluate(
string $expression, Node $context = NULL
): string|float|bool|\DOMNodeList {
$document = $this instanceof Document
? $this
: $this->ownerDocument;
Expand Down
23 changes: 6 additions & 17 deletions src/FluentDOM/DOM/Xpath.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,14 @@ public function firstOf(string $expression, \DOMNode $contextNode = NULL, bool $
*/
public static function quote(string $string): string {
$string = str_replace("\x00", '', $string);
$hasSingleQuote = FALSE !== strpos($string, "'");
$hasSingleQuote = str_contains($string, "'");
if ($hasSingleQuote) {
$hasDoubleQuote = FALSE !== strpos($string, '"');
$hasDoubleQuote = str_contains($string, '"');
if ($hasDoubleQuote) {
$result = '';
preg_match_all('("[^\']*|[^"]+)', $string, $matches);
foreach ($matches[0] as $part) {
$quoteChar = 0 === strpos($part, '"') ? "'" : '"';
$quoteChar = str_starts_with($part, '"') ? "'" : '"';
$result .= ', '.$quoteChar.$part.$quoteChar;
}
return 'concat('.substr($result, 2).')';
Expand All @@ -165,33 +165,22 @@ public function __isset(string $name): bool {
return FALSE;
}

/**
* @param string $name
* @return mixed
*/
public function __get(string $name) {
public function __get(string $name): mixed {
if ($name === 'registerNodeNamespaces') {
return $this->_registerNodeNamespaces;
}
throw new UndeclaredPropertyError($this, $name);
}

/**
* @param string $name
* @param mixed $value
*/
public function __set(string $name, $value) {
public function __set(string $name, mixed $value): void {
if ($name === 'registerNodeNamespaces') {
$this->_registerNodeNamespaces = (bool)$value;
return;
}
throw new UndeclaredPropertyError($this, $name);
}

/**
* @param string $name
*/
public function __unset(string $name) {
public function __unset(string $name): void {
if ($name === 'registerNodeNamespaces') {
$this->registerNodeNamespaces = FALSE;
return;
Expand Down
12 changes: 3 additions & 9 deletions src/FluentDOM/Loader/JSONx.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,11 @@ public function loadFragment($source, string $contentType, $options = []): ?Docu
}

/**
* @param \DOMNode|Element $node
* @param \DOMNode|Document|Element $target
* @throws \Throwable
*/
private function transferNode(\DOMNode $node, \DOMNode $target): void {
Constraints::assertNodeClass(
$node, Element::class
);
Constraints::assertNodeClass(
$node, [Element::class, DocumentFragment::class, Document::class]
);
private function transferNode(
Element $node, Document|Element|DocumentFragment $target
): void {
if ($node->namespaceURI === self::XMLNS_JSONX) {
if ($target instanceof Document) {
$normalizedName = $name = 'json:json';
Expand Down
8 changes: 2 additions & 6 deletions src/FluentDOM/Loader/Json/SimpleXML.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,9 @@ class SimpleXML implements Loadable {
/**
* Load the json string into an DOMDocument
*
* @param mixed $source
* @param string $contentType
* @param array|\Traversable|Options $options
* @return Result|NULL
* @throws InvalidSource
* @throws InvalidSource|\DOMException
*/
public function load($source, string $contentType, $options = []): ?Result {
public function load($source, string $contentType, iterable $options = []): ?Result {
if (FALSE !== ($json = $this->getJson($source, $contentType, $options))) {
$document = new Document('1.0', 'UTF-8');
$document->appendChild(
Expand Down
2 changes: 1 addition & 1 deletion src/FluentDOM/Loader/PHP/PDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class PDO extends JsonDOM {
public const CONTENT_TYPES = ['php/pdo', 'pdo'];

/**
* @throws \LogicException|\DOMException
* @see Loadable::load
* @throws \LogicException
*/
public function load(mixed $source, string $contentType, iterable $options = []): ?Result {
if ($source instanceof \PDOStatement) {
Expand Down
Loading

0 comments on commit d51617a

Please sign in to comment.