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 66db70f commit d9dae54
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 58 deletions.
2 changes: 1 addition & 1 deletion src/FluentDOM.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

abstract class FluentDOM {

private static ?Loadable $_loader;
private static ?Loadable $_loader = NULL;

private static array $_xpathTransformers = [];

Expand Down
5 changes: 1 addition & 4 deletions src/FluentDOM/DOM/Node/Xpath.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ public function evaluate(

/**
* Allow to call evaluate() by using the node as object
*
* @param string $expression
* @return string|float|\DOMNodeList|Node[]
*/
public function __invoke(string $expression) {
public function __invoke(string $expression): string|float|bool|\DOMNodeList {
return $this->evaluate(
$expression, $this instanceof \DOMNode ? $this : NULL
);
Expand Down
25 changes: 6 additions & 19 deletions src/FluentDOM/Loader/Lazy.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,16 @@ public function addClasses(array $classes, string $namespace = ''): void {
$this->add($type, $callback);
}
} else {
/** @noinspection PhpCastIsUnnecessaryInspection */
$this->add((string)$types, $callback);
}
}
}

/**
* @throws \UnexpectedValueException
* @param string $contentType
* @return bool|Loadable
*/
public function get(string $contentType) {
public function get(string $contentType): ?Loadable {
$contentType = $this->normalizeContentType($contentType);
if (isset($this->_list[$contentType])) {
if (!($this->_list[$contentType] instanceof Loadable)) {
Expand All @@ -99,24 +98,14 @@ public function get(string $contentType) {
}
return $this->_list[$contentType];
}
return FALSE;
return NULL;
}

/**
* @param string $contentType
* @return bool
*/
public function supports(string $contentType): bool {
$contentType = $this->normalizeContentType($contentType);
return isset($this->_list[$contentType]);
}

/**
* @param mixed $source
* @param string $contentType
* @param array|\Traversable|Options $options
* @return Result|NULL
*/
public function load($source, string $contentType, $options = []): ?Result {
$contentType = $this->normalizeContentType($contentType);
if ($loader = $this->get($contentType)) {
Expand All @@ -126,13 +115,11 @@ public function load($source, string $contentType, $options = []): ?Result {
}

/**
* @param mixed $source
* @param string $contentType
* @param array|\Traversable|Options $options
* @return DocumentFragment|NULL
* @throws \UnexpectedValueException
*/
public function loadFragment($source, string $contentType, $options = []): ?DocumentFragment {
public function loadFragment(
mixed $source, string $contentType, iterable $options = []
): ?DocumentFragment {
$contentType = $this->normalizeContentType($contentType);
if ($loader = $this->get($contentType)) {
return $loader->loadFragment($source, $contentType, $options);
Expand Down
31 changes: 8 additions & 23 deletions src/FluentDOM/Loaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,15 @@ public function __construct(iterable $list = NULL) {

/**
* Add a loader to the list
*
* @param Loadable $loader
*/
public function add(Loadable $loader) {
public function add(Loadable $loader): void {
$this->_list[spl_object_hash($loader)] = $loader;
}

/**
* Remove a loader to the list
*
* @param Loadable $loader
*/
public function remove(Loadable $loader) {
public function remove(Loadable $loader): void {
$key = spl_object_hash($loader);
if (isset($this->_list[$key])) {
unset($this->_list[$key]);
Expand All @@ -61,18 +57,13 @@ public function remove(Loadable $loader) {

/**
* Allow to iterate all added loaders
*
* @return \Iterator
*/
public function getIterator(): \Iterator {
return new \ArrayIterator(array_values($this->_list));
}

/**
* Validate if the list contains a loader that supports the given content type
*
* @param string $contentType
* @return bool
*/
public function supports(string $contentType): bool {
foreach ($this as $loader) {
Expand All @@ -89,13 +80,10 @@ public function supports(string $contentType): bool {
/**
* Load a data source, the content type allows the loader to decide if it supports
* the data source
*
* @param mixed $source
* @param string $contentType
* @param array|\Traversable|Options $options
* @return Result|NULL
*/
public function load($source, string $contentType, $options = []): ?Result {
public function load(
mixed $source, string $contentType, iterable $options = []
): ?Result {
$result = NULL;
foreach ($this as $loader) {
/**
Expand All @@ -111,13 +99,10 @@ public function load($source, string $contentType, $options = []): ?Result {
/**
* Load a data source as a fragment, the content type allows the loader to decide if it supports
* the data source
*
* @param mixed $source
* @param string $contentType
* @param array|\Traversable|Options $options
* @return DocumentFragment|NULL
*/
public function loadFragment($source, string $contentType, $options = []): ?DocumentFragment {
public function loadFragment(
mixed $source, string $contentType, iterable $options = []
): ?DocumentFragment {
$fragment = NULL;
foreach ($this as $loader) {
/**
Expand Down
7 changes: 1 addition & 6 deletions src/FluentDOM/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,8 @@ public function first(): static {
* Retrieve the matched DOM elements in an array. A negative position will be counted from the end.
*
* @param int|NULL $position optional offset of a single element to get.
* @return array|\DOMNode|NULL
*/
public function get(int $position = NULL) {
public function get(int $position = NULL): array|\DOMNode|NULL {
if (NULL === $position) {
return $this->_nodes;
}
Expand All @@ -428,8 +427,6 @@ public function get(int $position = NULL) {
* Reduce the set of matched elements to those that have
* a descendant that matches the selector or DOM element.
*
* @param string|\DOMNode $selector selector or DOMNode
* @return self
* @throws \OutOfBoundsException
* @throws \InvalidArgumentException
*/
Expand All @@ -455,8 +452,6 @@ public function has(string|\DOMNode $selector): static {
* Checks the current selection against an expression and returns TRUE,
* if at least one element of the selection fits the given expression.
*
* @param string $selector selector
* @return bool
* @example ../examples/Query/is.php Usage Example: FluentDOM\Query::is()
*/
public function is(string $selector): bool {
Expand Down
10 changes: 5 additions & 5 deletions src/FluentDOM/XMLWriter/NamespaceStack.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,28 @@ public function __construct() {
$this->clear();
}

public function clear() {
public function clear(): void {
$this->_stack = [];
$this->_current = new NamespaceDefinition();
}

public function push() {
public function push(): void {
$this->_current->increaseDepth();
}

public function pop() {
public function pop(): void {
if ($this->_current->getDepth() < 1) {
$this->_current = end($this->_stack);
} else {
$this->_current->decreaseDepth();
}
}

public function isDefined($prefix, $namespaceURI): bool {
public function isDefined(?string $prefix, ?string $namespaceURI): bool {
return ($this->_current->resolveNamespace((string)$prefix) === $namespaceURI);
}

public function add($prefix, $namespaceURI) {
public function add(?string $prefix, ?string $namespaceURI): void {
if ($this->_current->getDepth() > 0) {
$this->_stack[] = $this->_current;
$this->_current = new NamespaceDefinition($this->_current);
Expand Down

0 comments on commit d9dae54

Please sign in to comment.