Make WordPress Core


Ignore:
Timestamp:
07/19/2024 11:42:14 PM (10 months ago)
Author:
dmsnell
Message:

HTML API: Add PHP type annotations.

This patch adds type annotations to internal and private methods of the HTML
API and the supporting WP_Token_Map. Annotations have not been added to the
public interfaces where it would likely crash a site if called wrong.

These annotations should help avoid unnecessary type-related bugs (as have
been uncovered in earlier work adding such annotations) and provide additional
guidance to developers when interacting with these classes in an IDE.

Developed in https://github.com/WordPress/wordpress-develop/pull/6753
Discussed in https://core.trac.wordpress.org/ticket/61399

Props dmsnell, jonsurrell.
See #61399.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-token-map.php

    r58742 r58769  
    281281     * @return WP_Token_Map|null Token map, unless unable to create it.
    282282     */
    283     public static function from_array( $mappings, $key_length = 2 ) {
     283    public static function from_array( array $mappings, int $key_length = 2 ): ?WP_Token_Map {
    284284        $map             = new WP_Token_Map();
    285285        $map->key_length = $key_length;
     
    329329            usort(
    330330                $groups[ $group_key ],
    331                 static function ( $a, $b ) {
     331                static function ( array $a, array $b ): int {
    332332                    return self::longest_first_then_alphabetical( $a[0], $b[0] );
    333333                }
     
    386386     * @return WP_Token_Map Map with precomputed data loaded.
    387387     */
    388     public static function from_precomputed_table( $state ) {
     388    public static function from_precomputed_table( $state ): ?WP_Token_Map {
    389389        $has_necessary_state = isset(
    390390            $state['storage_version'],
     
    440440     * @return bool Whether there's an entry for the given word in the map.
    441441     */
    442     public function contains( $word, $case_sensitivity = 'case-sensitive' ) {
     442    public function contains( string $word, string $case_sensitivity = 'case-sensitive' ): bool {
    443443        $ignore_case = 'ascii-case-insensitive' === $case_sensitivity;
    444444
     
    528528     * @return string|null Mapped value of lookup key if found, otherwise `null`.
    529529     */
    530     public function read_token( $text, $offset = 0, &$matched_token_byte_length = null, $case_sensitivity = 'case-sensitive' ) {
     530    public function read_token( string $text, int $offset = 0, &$matched_token_byte_length = null, $case_sensitivity = 'case-sensitive' ): ?string {
    531531        $ignore_case = 'ascii-case-insensitive' === $case_sensitivity;
    532532        $text_length = strlen( $text );
     
    572572     * Finds a match for a short word at the index.
    573573     *
    574      * @since 6.6.0.
    575      *
    576      * @param string $text                       String in which to search for a lookup key.
    577      * @param int    $offset                     Optional. How many bytes into the string where the lookup key ought to start. Default 0.
    578      * @param ?int   &$matched_token_byte_length Optional. Holds byte-length of found lookup key if matched, otherwise not set. Default null.
    579      * @param string $case_sensitivity           Optional. Pass 'ascii-case-insensitive' to ignore ASCII case when matching. Default 'case-sensitive'.
     574     * @since 6.6.0
     575     *
     576     * @param string   $text                       String in which to search for a lookup key.
     577     * @param int      $offset                     Optional. How many bytes into the string where the lookup key ought to start. Default 0.
     578     * @param int|null &$matched_token_byte_length Optional. Holds byte-length of found lookup key if matched, otherwise not set. Default null.
     579     * @param string   $case_sensitivity           Optional. Pass 'ascii-case-insensitive' to ignore ASCII case when matching. Default 'case-sensitive'.
     580     *
    580581     * @return string|null Mapped value of lookup key if found, otherwise `null`.
    581582     */
    582     private function read_small_token( $text, $offset, &$matched_token_byte_length, $case_sensitivity = 'case-sensitive' ) {
     583    private function read_small_token( string $text, int $offset = 0, &$matched_token_byte_length = null, $case_sensitivity = 'case-sensitive' ): ?string {
    583584        $ignore_case  = 'ascii-case-insensitive' === $case_sensitivity;
    584585        $small_length = strlen( $this->small_words );
     
    635636     * @return array The lookup key/substitution values as an associate array.
    636637     */
    637     public function to_array() {
     638    public function to_array(): array {
    638639        $tokens = array();
    639640
     
    697698     * @return string Value which can be pasted into a PHP source file for quick loading of table.
    698699     */
    699     public function precomputed_php_source_table( $indent = "\t" ) {
     700    public function precomputed_php_source_table( string $indent = "\t" ): string {
    700701        $i1 = $indent;
    701702        $i2 = $i1 . $indent;
     
    802803     * @return int -1 or lower if `$a` is less than `$b`; 1 or greater if `$a` is greater than `$b`, and 0 if they are equal.
    803804     */
    804     private static function longest_first_then_alphabetical( $a, $b ) {
     805    private static function longest_first_then_alphabetical( string $a, string $b ): int {
    805806        if ( $a === $b ) {
    806807            return 0;
Note: See TracChangeset for help on using the changeset viewer.