Skip to content

Commit

Permalink
Add type declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
mundschenk-at committed Mar 30, 2024
1 parent bb7b363 commit 35c2f1e
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/class-dom.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ abstract class DOM {
*
* @var array<string,int>
*/
private static $block_tags;
private static array $block_tags;

/**
* A flipped array of tags that should never be modified. Checked via isset/has_key.
*
* @var array<string,int>
*/
private static $inappropriate_tags;
private static array $inappropriate_tags;

const ADDITIONAL_INAPPROPRIATE_TAGS = [
'button',
Expand Down Expand Up @@ -92,7 +92,7 @@ abstract class DOM {
* @type int $tag Only the existence of the $tag key is relevant.
* }
*/
public static function block_tags( $reset = false ) {
public static function block_tags( bool $reset = false ): array {
if ( empty( self::$block_tags ) || $reset ) {
self::$block_tags = \array_merge(
\array_flip(
Expand Down Expand Up @@ -121,7 +121,7 @@ function ( $tag ) {
* @type int $tag Only the existence of the $tag key is relevant.
* }
*/
public static function inappropriate_tags( $reset = false ) {
public static function inappropriate_tags( bool $reset = false ): array {
if ( empty( self::$inappropriate_tags ) || $reset ) {
self::$inappropriate_tags = \array_flip(
\array_merge(
Expand Down Expand Up @@ -152,7 +152,7 @@ function ( $tag ) {
*
* @phpstan-param DOMNodeList<DOMNode> $node_list
*/
public static function nodelist_to_array( DOMNodeList $node_list ) {
public static function nodelist_to_array( DOMNodeList $node_list ): array {
$out = [];

foreach ( $node_list as $node ) {
Expand All @@ -170,7 +170,7 @@ public static function nodelist_to_array( DOMNodeList $node_list ) {
*
* @return DOMNode[] An array of DOMNode.
*/
public static function get_ancestors( DOMNode $node ) {
public static function get_ancestors( DOMNode $node ): array {
$result = [];

while ( ( $node = $node->parentNode ) && ( $node instanceof DOMElement ) ) { // phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
Expand All @@ -189,7 +189,7 @@ public static function get_ancestors( DOMNode $node ) {
*
* @return bool True if the element has any of the given class(es).
*/
public static function has_class( DOMNode $tag, $classnames ) {
public static function has_class( DOMNode $tag, $classnames ): bool {
if ( $tag instanceof DOMText ) {
$tag = $tag->parentNode;
}
Expand Down Expand Up @@ -224,7 +224,7 @@ public static function has_class( DOMNode $tag, $classnames ) {
*
* @return string A single character (or the empty string).
*/
public static function get_prev_chr( DOMNode $node ) {
public static function get_prev_chr( DOMNode $node ): string {
return self::get_adjacent_character( $node, -1, 1, [ __CLASS__, 'get_previous_acceptable_node' ] );
}

Expand All @@ -235,7 +235,7 @@ public static function get_prev_chr( DOMNode $node ) {
*
* @return string A single character (or the empty string).
*/
public static function get_next_chr( DOMNode $node ) {
public static function get_next_chr( DOMNode $node ): string {
return self::get_adjacent_character( $node, 0, 1, [ __CLASS__, 'get_next_acceptable_node' ] );
}

Expand All @@ -252,7 +252,7 @@ public static function get_next_chr( DOMNode $node ) {
*
* @return string The character or an empty string.
*/
private static function get_adjacent_character( DOMNode $node, $position, $length, callable $get_node ) {
private static function get_adjacent_character( DOMNode $node, int $position, int $length, callable $get_node ): string {
$character = '';
$adjacent_node = $get_node(
// Determines if the node is a textnode or one of the acceptable elements.
Expand Down Expand Up @@ -335,7 +335,7 @@ function ( callable $is_node_acceptable, DOMNode &$another_node ): ?DOMNode {
*
* @return DOMNode|null Null if $node is a block-level element or no acceptable sibling exists.
*/
private static function get_adjacent_node( callable $is_acceptable, callable $iterate, callable $get_adjacent_parent, DOMNode $node = null ): ?DOMNode {
private static function get_adjacent_node( callable $is_acceptable, callable $iterate, callable $get_adjacent_parent, ?DOMNode $node ): ?DOMNode {
if ( ! isset( $node ) || self::is_block_tag( $node ) ) {
return null;
}
Expand Down Expand Up @@ -375,7 +375,7 @@ private static function get_adjacent_node( callable $is_acceptable, callable $it
*
* @return DOMText|null The first child of type DOMText, the element itself if it is of type DOMText or null.
*/
public static function get_first_textnode( DOMNode $node = null, $recursive = false ) {
public static function get_first_textnode( DOMNode $node = null, bool $recursive = false ): ?DOMText {
/**
* We only allow textnodes in our `is_acceptable` callable.
*
Expand Down Expand Up @@ -431,7 +431,7 @@ public static function get_last_acceptable_node( callable $is_acceptable, DOMNod
*
* @return DOMNode|null The last acceptable child, the element itself if it is acceptable or null.
*/
private static function get_edge_node( callable $is_acceptable, callable $get_acceptable_node, DOMNode $node = null, $recursive = false, $reverse = false ): ?DOMNode {
private static function get_edge_node( callable $is_acceptable, callable $get_acceptable_node, ?DOMNode $node, bool $recursive = false, bool $reverse = false ): ?DOMNode {
if ( $is_acceptable( $node ) ) {
return $node;
} elseif ( ! $node instanceof DOMElement || ( $recursive && self::is_block_tag( $node ) ) ) {
Expand Down Expand Up @@ -469,7 +469,7 @@ private static function get_edge_node( callable $is_acceptable, callable $get_ac
*
* @return DOMElement|null
*/
public static function get_block_parent( DOMNode $node ) {
public static function get_block_parent( DOMNode $node ): ?DOMElement {
$parent = $node->parentNode;
if ( ! $parent instanceof DOMElement ) {
return null;
Expand All @@ -496,7 +496,7 @@ public static function get_block_parent( DOMNode $node ) {
*
* @return bool
*/
public static function is_block_tag( DOMNode $node ) {
public static function is_block_tag( DOMNode $node ): bool {
return $node instanceof DOMElement && isset( self::$block_tags[ $node->tagName ] );
}
}
Expand Down

0 comments on commit 35c2f1e

Please sign in to comment.