Skip to content

Commit 1950061

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. Commented out new code that is not working yet.
1 parent 4e64690 commit 1950061

6 files changed

Lines changed: 69 additions & 73 deletions

File tree

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ CSSdoc requires [\hexydec\token\tokenise](https://github.com/hexydec/tokenise) t
5252

5353
## Support
5454

55-
CSSdoc supports PHP version 7.4+.
55+
CSSdoc supports PHP version 8.0+.
5656

5757
## Contributing
5858

src/cssdoc.php

Lines changed: 52 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
class cssdoc implements \ArrayAccess, \Iterator {
77

88
/**
9-
* @var array<string> $tokens Regexp components keyed by their corresponding codename for tokenising HTML
9+
* @var array<string> $tokens Regexp components keyed by their corresponding codename for tokenising CSS
1010
*/
1111
protected static array $tokens = [
1212
'whitespace' => '\s++',
@@ -225,8 +225,7 @@ public function __construct(array $config = []) {
225225
* @param string $var The name of the property to retrieve, currently 'length' and output
226226
* @return mixed The number of children in the object for length, the output config, or null if the parameter doesn't exist
227227
*/
228-
#[\ReturnTypeWillChange]
229-
public function __get(string $var) {
228+
public function __get(string $var) : mixed {
230229
if ($var === 'length') {
231230
return \count($this->document->rules ?? []);
232231
} elseif ($var === 'config') {
@@ -250,7 +249,7 @@ public function toArray() : array {
250249
* @param mixed $i The key to be updated, can be a string or integer
251250
* @param mixed $value The value of the array key in the children array to be updated
252251
*/
253-
public function offsetSet($i, $value) : void {
252+
public function offsetSet(mixed $i, mixed $value) : void {
254253
if (\is_null($i)) {
255254
$this->document->rules[] = $value;
256255
} else {
@@ -261,30 +260,29 @@ public function offsetSet($i, $value) : void {
261260
/**
262261
* Array access method allows you to check that a key exists in the configuration array
263262
*
264-
* @param string|integer $i The key to be checked, can be a string or integer
263+
* @param mixed $i The key to be checked
265264
* @return bool Whether the key exists in the config array
266265
*/
267-
public function offsetExists($i) : bool {
266+
public function offsetExists(mixed $i) : bool {
268267
return isset($this->document->rules[$i]);
269268
}
270269

271270
/**
272271
* Removes a key from the configuration array
273272
*
274-
* @param string|integer $i The key to be removed, can be a string or integer
273+
* @param mixed $i The key to be removed
275274
*/
276-
public function offsetUnset($i) : void {
275+
public function offsetUnset(mixed $i) : void {
277276
unset($this->document->rules[$i]);
278277
}
279278

280279
/**
281280
* Retrieves a value from the configuration array with the specified key
282281
*
283-
* @param string|integer $i The key to be accessed, can be a string or integer
282+
* @param mixed $i The key to be accessed, can be a string or integer
284283
* @return mixed The requested value or null if the key doesn't exist
285284
*/
286-
#[\ReturnTypeWillChange]
287-
public function offsetGet($i) { // return reference so you can set it like an array
285+
public function offsetGet(mixed $i) : mixed { // return reference so you can set it like an array
288286
return $this->document->rules[$i] ?? null;
289287
}
290288

@@ -293,8 +291,7 @@ public function offsetGet($i) { // return reference so you can set it like an ar
293291
*
294292
* @return document|rule The child node at the current pointer position
295293
*/
296-
#[\ReturnTypeWillChange]
297-
public function current() {
294+
public function current() : mixed {
298295
return $this->document->rules[$this->pointer] ?? null;
299296
}
300297

@@ -303,7 +300,6 @@ public function current() {
303300
*
304301
* @return mixed The current pointer position
305302
*/
306-
#[\ReturnTypeWillChange]
307303
public function key() : mixed {
308304
return $this->pointer;
309305
}
@@ -336,14 +332,14 @@ public function valid() : bool {
336332
}
337333

338334
/**
339-
* Open an HTML file from a URL
335+
* Open a CSS file from a URL
340336
*
341-
* @param string $url The address of the HTML file to retrieve
337+
* @param string $url The address of the CSS file to retrieve
342338
* @param resource $context A resource object made with stream_context_create()
343339
* @param ?string &$error A reference to any user error that is generated
344-
* @return mixed The loaded HTML, or false on error
340+
* @return string|false The loaded CSS, or false on error
345341
*/
346-
public function open(string $url, mixed $context = null, ?string &$error = null) {
342+
public function open(string $url, $context = null, ?string &$error = null) : string|false {
347343

348344
// check resource
349345
if ($context !== null && !\is_resource($context)) {
@@ -354,7 +350,7 @@ public function open(string $url, mixed $context = null, ?string &$error = null)
354350
$error = 'Could not open file "'.$url.'"';
355351

356352
// retrieve the stream contents
357-
} elseif (($html = \stream_get_contents($handle)) === false) {
353+
} elseif (($css = \stream_get_contents($handle)) === false) {
358354
$error = 'Could not read file "'.$url.'"';
359355

360356
// success
@@ -372,21 +368,21 @@ public function open(string $url, mixed $context = null, ?string &$error = null)
372368
}
373369
}
374370

375-
// load html
376-
if ($this->load($html, $charset, $error)) {
377-
return $html;
371+
// load CSS
372+
if ($this->load($css, $charset, $error)) {
373+
return $css;
378374
}
379375
}
380376
return false;
381377
}
382378

383379
/**
384-
* Parse an HTML string into the object
380+
* Parse a CSS string into the object
385381
*
386382
* @param string $css A string containing valid CSS
387383
* @param string $charset The charset of the document
388384
* @param ?string &$error A reference to any user error that is generated
389-
* @return bool Whether the input HTML was parsed
385+
* @return bool Whether the input CSS was parsed
390386
*/
391387
public function load(string $css, string $charset = null, ?string &$error = null) : bool {
392388

@@ -409,7 +405,7 @@ public function load(string $css, string $charset = null, ?string &$error = null
409405
}
410406

411407
/**
412-
* Reads the charset defined in the Content-Type meta tag, or detects the charset from the HTML content
408+
* Reads the charset defined in the Content-Type meta tag, or detects the charset from the CSS content
413409
*
414410
* @param string $css A string containing valid CSS
415411
* @return ?string The defined or detected charset or null if the charset is not defined
@@ -427,9 +423,9 @@ protected function getCharsetFromCss(string $css) : ?string {
427423
* Parses an array of tokens into an CSS document
428424
*
429425
* @param string $css A string containing valid CSS
430-
* @return document|bool A document object or false if the string could not be parsed
426+
* @return document|false A document object or false if the string could not be parsed
431427
*/
432-
protected function parse(string $css) {
428+
protected function parse(string $css) : document|false {
433429

434430
// tokenise the input CSS
435431
$tokens = new tokenise(self::$tokens, $css);
@@ -475,7 +471,7 @@ public function compile(array $options = []) : string {
475471
* @param array $options An array indicating output options, this is merged with cssdoc::$output
476472
* @return string|false The compiled CSS, or false if the file could not be saved
477473
*/
478-
public function save(string $file = null, array $options = []) {
474+
public function save(string $file = null, array $options = []) : string|false {
479475
$css = $this->compile($options);
480476

481477
// save file
@@ -488,35 +484,35 @@ public function save(string $file = null, array $options = []) {
488484
return $css;
489485
}
490486

491-
public function collection(array $rules) {
492-
$this->document = new document($this, $rules);
493-
}
494-
495-
/**
496-
* Find rules in the document that match the specified criteria
497-
*
498-
* @param string $selector A string specifying the selectors to match, comma separate multiple selectors
499-
* @param array|string $hasProp A string or array specifying the properties that any rules must contain
500-
* @param array $media An array specifying how any media queries should be match, where the key is the property and the key the value. 'max-width' will match any rules where the value is lower that that specified, 'min-width' the value must be higher. Use 'media' to specify the media type
501-
* @param bool $exact Denotes whether to match selectors exactly, if false, selectors will be matched from the left
502-
* @return cssdoc A CSSdoc object
503-
*/
504-
public function find(?string $selector, $hasProp = null, array $media = [], bool $exact = true) : cssdoc {
505-
506-
// normalise selectors
507-
$selector = $selector === null ? null : \array_map('\\trim', \explode(',', $selector));
508-
if (!\is_array($hasProp)) {
509-
$hasProp = [$hasProp];
510-
}
511-
512-
// find rules
513-
$rules = $this->document->find($selector, $hasProp, $media);
487+
// public function collection(array $rules) {
488+
// $this->document = new document($this, $rules);
489+
// }
514490

515-
// attach to a new document
516-
$obj = new cssdoc($this->config);
517-
$obj->collection($rules);
518-
return $obj;
519-
}
491+
// /**
492+
// * Find rules in the document that match the specified criteria
493+
// *
494+
// * @param string $selector A string specifying the selectors to match, comma separate multiple selectors
495+
// * @param array|string $hasProp A string or array specifying the properties that any rules must contain
496+
// * @param array $media An array specifying how any media queries should be match, where the key is the property and the key the value. 'max-width' will match any rules where the value is lower that that specified, 'min-width' the value must be higher. Use 'media' to specify the media type
497+
// * @param bool $exact Denotes whether to match selectors exactly, if false, selectors will be matched from the left
498+
// * @return cssdoc A CSSdoc object
499+
// */
500+
// public function find(?string $selector, $hasProp = null, array $media = [], bool $exact = true) : cssdoc {
501+
502+
// // normalise selectors
503+
// $selector = $selector === null ? null : \array_map('\\trim', \explode(',', $selector));
504+
// if (!\is_array($hasProp)) {
505+
// $hasProp = [$hasProp];
506+
// }
507+
508+
// // find rules
509+
// $rules = $this->document->find($selector, $hasProp, $media);
510+
511+
// // attach to a new document
512+
// $obj = new cssdoc($this->config);
513+
// $obj->collection($rules);
514+
// return $obj;
515+
// }
520516

521517
// public function prop(string $prop, ?string $func = null) {
522518
//

src/tokens/document.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class document {
1818
/**
1919
* Constructs the comment object
2020
*
21-
* @param cssdoc $root The parent htmldoc object
21+
* @param cssdoc $root The parent cssdoc object
2222
*/
2323
public function __construct(cssdoc $root, array $rules = []) {
2424
$this->root = $root;
@@ -101,15 +101,15 @@ public function compile(array $options) : string {
101101
* @param array|string $hasProp A string or array specifying the properties that any rules must contain
102102
* @param array $media An array specifying how any media queries should be match, where the key is the property and the key the value. 'max-width' will match any rules where the value is lower that that specified, 'min-width' the value must be higher. Use 'media' to specify the media type
103103
* @param bool $exact Denotes whether to match selectors exactly, if false, selectors will be matched from the left
104-
* @return array A CSSdoc object
104+
* @return array An array of rule objects
105105
*/
106-
public function find(?array $selectors, $hasProp = null, array $media = [], bool $exact = true) {
107-
$rules = [];
108-
foreach ($this->rules AS $item) {
109-
if (\get_class($item) === '\\hexydec\\css\\cssdoc' && $item->matches($selectors, $hasProp, $exact)) {
110-
$rules[] = $item;
111-
}
112-
}
113-
return $rules;
114-
}
106+
// public function find(?array $selectors, array|string $hasProp = null, array $media = [], bool $exact = true) {
107+
// $rules = [];
108+
// foreach ($this->rules AS $item) {
109+
// if (\get_class($item) === '\\hexydec\\css\\cssdoc' && $item->matches($selectors, $hasProp, $exact)) {
110+
// $rules[] = $item;
111+
// }
112+
// }
113+
// return $rules;
114+
// }
115115
}

src/tokens/property.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public function __construct(cssdoc $root) {
4848
* Retrieves read only properties
4949
*
5050
* @param string $var The name of the property to retrieve
51-
* @return ?string The value of the requested property, or null if the porperty doesn't exist
51+
* @return mixed The value of the requested property, or null if the property doesn't exist
5252
*/
53-
public function __get(string $var) {
53+
public function __get(string $var) : mixed {
5454
if ($var === 'name') {
5555
return $this->name;
5656
}

src/tokens/rule.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class rule {
2828
/**
2929
* Constructs the comment object
3030
*
31-
* @param cssdoc $root The parent htmldoc object
31+
* @param cssdoc $root The parent cssdoc object
3232
*/
3333
public function __construct(cssdoc $root) {
3434
$this->root = $root;
@@ -102,7 +102,7 @@ public function minify(array $minify) : void {
102102
}
103103
}
104104

105-
public function isEmpty() {
105+
public function isEmpty() : bool {
106106
return !$this->properties;
107107
}
108108

src/tokens/selector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class selector {
1818
/**
1919
* Constructs the comment object
2020
*
21-
* @param cssdoc $root The parent htmldoc object
21+
* @param cssdoc $root The parent cssdoc object
2222
*/
2323
public function __construct(cssdoc $root) {
2424
$this->root = $root;

0 commit comments

Comments
 (0)