Skip to content

Commit 18b843f

Browse files
committed
Updated type hints to correct types where #[\ReturnTypeWillChange] as the software now only supports PHP 8.0+.
Added type hints where `mixed` is required. Added union type hints on arguments and return types where missing.
1 parent 091df7e commit 18b843f

File tree

4 files changed

+26
-31
lines changed

4 files changed

+26
-31
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ $ vendor/bin/phpunit
9595

9696
## Support
9797

98-
HTMLdoc supports PHP version 7.4+.
98+
HTMLdoc supports PHP version 8.0+.
9999

100100
## Contributing
101101

src/htmldoc.php

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ class htmldoc extends config implements \ArrayAccess, \Iterator {
6767
* @param string $var The name of the property to retrieve, currently 'length' and output
6868
* @return mixed The number of children in the object for length, the output config, or null if the parameter doesn't exist
6969
*/
70-
#[\ReturnTypeWillChange]
71-
public function __get(string $var) {
70+
public function __get(string $var) : mixed {
7271
if ($var === 'config') {
7372
return $this->config;
7473
} elseif ($var === 'length') {
@@ -92,7 +91,7 @@ public function toArray() : array {
9291
* @param mixed $i The key to be updated, can be a string or integer
9392
* @param mixed $value The value of the array key in the children array to be updated
9493
*/
95-
public function offsetSet($i, $value) : void {
94+
public function offsetSet(mixed $i, mixed $value) : void {
9695
$this->children[$i] = $value;
9796
}
9897

@@ -102,7 +101,7 @@ public function offsetSet($i, $value) : void {
102101
* @param mixed $i The key to be checked
103102
* @return bool Whether the key exists in the config array
104103
*/
105-
public function offsetExists($i) : bool {
104+
public function offsetExists(mixed $i) : bool {
106105
return isset($this->children[$i]);
107106
}
108107

@@ -111,7 +110,7 @@ public function offsetExists($i) : bool {
111110
*
112111
* @param mixed $i The key to be removed
113112
*/
114-
public function offsetUnset($i) : void {
113+
public function offsetUnset(mixed $i) : void {
115114
unset($this->children[$i]);
116115
}
117116

@@ -121,8 +120,7 @@ public function offsetUnset($i) : void {
121120
* @param mixed $i The key to be accessed, can be a string or integer
122121
* @return mixed An HTMLdoc object containing the child node at the requested position or null if there is no child at the requested position
123122
*/
124-
#[\ReturnTypeWillChange]
125-
public function offsetGet($i) { // return reference so you can set it like an array
123+
public function offsetGet(mixed $i) : mixed { // return reference so you can set it like an array
126124
if (isset($this->children[$i])) {
127125
$obj = new htmldoc($this->config);
128126
$obj->collection([$this->children[$i]]);
@@ -136,8 +134,7 @@ public function offsetGet($i) { // return reference so you can set it like an ar
136134
*
137135
* @return mixed An HTMLdoc object containing the child node at the current pointer position or null if there are no children
138136
*/
139-
#[\ReturnTypeWillChange]
140-
public function current() {
137+
public function current() : mixed {
141138
if (isset($this->children[$this->pointer])) {
142139
$obj = new htmldoc($this->config);
143140
$obj->collection([$this->children[$this->pointer]]);
@@ -151,8 +148,7 @@ public function current() {
151148
*
152149
* @return mixed The current pointer position
153150
*/
154-
#[\ReturnTypeWillChange]
155-
public function key() {
151+
public function key() : mixed {
156152
return $this->pointer;
157153
}
158154

@@ -191,7 +187,7 @@ public function valid() : bool {
191187
* @param ?string &$error A reference to any user error that is generated
192188
* @return string|false The loaded HTML, or false on error
193189
*/
194-
public function open(string $url, $context = null, ?string &$error = null) {
190+
public function open(string $url, $context = null, ?string &$error = null) : string|false {
195191

196192
// check resource
197193
if ($context !== null && !\is_resource($context)) {
@@ -295,9 +291,9 @@ protected function isEncodingValid(string $charset) : bool {
295291
* Parses an array of tokens into an HTML document
296292
*
297293
* @param string|htmldoc $html A string of HTML, or an htmldoc object
298-
* @return bool|array An array of node objects or false on error
294+
* @return array|false An array of node objects or false on error
299295
*/
300-
protected function parse($html) {
296+
protected function parse(string|htmldoc $html) : array|false {
301297

302298
// convert string to nodes
303299
if (\is_string($html)) {
@@ -346,9 +342,9 @@ public function cache(string $key, array $values) : void {
346342
* Retrieves the tag object at the specified index, or all children of type tag
347343
*
348344
* @param int $index The index of the child tag to retrieve
349-
* @return mixed A tag object if index is specified, or an array of tag objects, or null if the specified index doesn't exist or the object is empty
345+
* @return tag|array|null A tag object if index is specified, or an array of tag objects, or null if the specified index doesn't exist or the object is empty
350346
*/
351-
public function get(int $index = null) {
347+
public function get(int $index = null) : tag|array|null {
352348

353349
// build children that are tags
354350
$children = [];
@@ -599,7 +595,7 @@ public function html(array $options = []) : string {
599595
* @param string|htmldoc $html A string of HTML, or an htmldoc object
600596
* @return htmldoc The current htmldoc object with the nodes appended
601597
*/
602-
public function append($html) : htmldoc {
598+
public function append(string|htmldoc $html) : htmldoc {
603599
if (($nodes = $this->parse($html)) !== false) {
604600
foreach ($this->children AS $item) {
605601
if (\get_class($item) === 'hexydec\\html\\tag') {
@@ -616,7 +612,7 @@ public function append($html) : htmldoc {
616612
* @param string|htmldoc $html A string of HTML, or an htmldoc object
617613
* @return htmldoc The current htmldoc object with the nodes appended
618614
*/
619-
public function prepend($html) : htmldoc {
615+
public function prepend(string|htmldoc $html) : htmldoc {
620616
if (($nodes = $this->parse($html)) !== false) {
621617
foreach ($this->children AS $item) {
622618
if (\get_class($item) === 'hexydec\\html\\tag') {
@@ -633,7 +629,7 @@ public function prepend($html) : htmldoc {
633629
* @param string|htmldoc $html A string of HTML, or an htmldoc object
634630
* @return htmldoc The current htmldoc object with the nodes appended
635631
*/
636-
public function before($html) : htmldoc {
632+
public function before(string|htmldoc $html) : htmldoc {
637633
if (($nodes = $this->parse($html)) !== false) {
638634
foreach ($this->children AS $item) {
639635
if (\get_class($item) === 'hexydec\\html\\tag') {
@@ -650,7 +646,7 @@ public function before($html) : htmldoc {
650646
* @param string|htmldoc $html A string of HTML, or an htmldoc object
651647
* @return htmldoc The current htmldoc object with the nodes appended
652648
*/
653-
public function after($html) : htmldoc {
649+
public function after(string|htmldoc $html) : htmldoc {
654650
if (($nodes = $this->parse($html)) !== false) {
655651
foreach ($this->children AS $item) {
656652
if (\get_class($item) === 'hexydec\\html\\tag') {
@@ -664,10 +660,10 @@ public function after($html) : htmldoc {
664660
/**
665661
* Removes all top level nodes, or if $selector is specified, the nodes matched by the selector
666662
*
667-
* @param string $selector A CSS selector to refine the nodes to delete or null to delete top level nodes
663+
* @param ?string $selector A CSS selector to refine the nodes to delete or null to delete top level nodes
668664
* @return htmldoc The current htmldoc object with the requested nodes deleted
669665
*/
670-
public function remove(string $selector = null) : htmldoc {
666+
public function remove(?string $selector = null) : htmldoc {
671667
$obj = $selector ? $this->find($selector) : $this;
672668
foreach ($obj->children AS $item) {
673669
if (\get_class($item) === 'hexydec\\html\\tag') {
@@ -682,9 +678,9 @@ public function remove(string $selector = null) : htmldoc {
682678
*
683679
* @param string|null $file The file location to save the document to, or null to just return the compiled code
684680
* @param array $options An array indicating output options, this is merged with htmldoc::$output
685-
* @return string|bool The compiled HTML, or false if the file could not be saved
681+
* @return string|false The compiled HTML, or false if the file could not be saved
686682
*/
687-
public function save(string $file = null, array $options = []) {
683+
public function save(?string $file = null, array $options = []) : string|false {
688684

689685
// compile html
690686
$html = $this->html($options);

src/tokens/tag.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function __set(string $name, $value) : void {
9494
*
9595
* @return void
9696
*/
97-
public function __clone() {
97+
public function __clone() : void {
9898
foreach ($this->children AS &$item) {
9999
$item = clone $item;
100100
}
@@ -287,7 +287,7 @@ public function parseChildren(tokenise $tokens) : array {
287287
/**
288288
* Returns the parent of the current object
289289
*
290-
* @return tag The parent tag
290+
* @return ?tag The parent tag
291291
*/
292292
public function parent() : ?tag {
293293
return $this->parent;
@@ -918,8 +918,7 @@ public function children() : array {
918918
*
919919
* @return mixed The value of the requested property
920920
*/
921-
#[\ReturnTypeWillChange]
922-
public function __get(string $var) {
921+
public function __get(string $var) : mixed {
923922
return $this->$var;
924923
}
925924
}

src/tokens/text.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct(htmldoc $root, ?tag $parent = null) {
3838
* @param mixed $value The value of the property to set
3939
* @return void
4040
*/
41-
public function __set(string $name, $value) : void {
41+
public function __set(string $name, mixed $value) : void {
4242
if ($name === 'parent' && \get_class($value) === 'hexydec\\html\\tag') {
4343
$this->parent = $value;
4444
}
@@ -117,7 +117,7 @@ public function minify(array $minify) : void {
117117
}
118118
}
119119

120-
protected function getIndex($children) {
120+
protected function getIndex(array $children) : int|false {
121121
foreach ($children AS $key => $value) {
122122
if ($value === $this) {
123123
return $key;

0 commit comments

Comments
 (0)