为什么 VS Code 说此 PHP 代码与签名不匹配?

Why is VS Code saying this PHP code doesn't match signatures?

我在 WordPress 插件项目中使用以下代码。 VS Code 给我一个错误:

Method 'Acorn\UI\Mobile\MenuWalker::start_lvl()' is not compatible with method 'Walker_Nav_Menu::start_lvl()'.intelephense(1038)

这是生成错误的代码片段(仅函数的第一部分):

namespace Acorn\UI\Mobile;

class MenuWalker extends \Walker_Nav_Menu {

    private int      $depth = 0;

    public function start_lvl( 
        string   &$output, 
        int       $depth = 0, 
        \stdClass  $args = []
    )  {

        $this->_set_depth( $depth );

        $output .= 
            $this->_dump( '<start_lvl />' )  .
            $this->_dump( $args )
            ;
    }

更新 我听从了@Spoody 的建议并将函数更改为:

namespace Acorn\UI\Mobile;

class MenuWalker extends \Walker_Nav_Menu {

    private int      $depth = 0;

    public function start_lvl( &$output, $depth = 0, \stdClass $args = [] ) {
        $this->_set_depth( (int) $depth );

        $output .= 
            $this->_dump( '<start_lvl />' )  .
            $this->_dump( $args )
            ;
    }

...但我仍然收到同样的错误。还有其他想法吗?

更多详情 在@M。埃里克森的要求,全文如下class:

<?php

namespace Acorn\UI\Mobile;

class MenuWalker extends \Walker_Nav_Menu {

    private int $depth = 0;

    public function start_lvl( &$output, $depth = 0, \stdClass $args = null ) {
        $this->_set_depth( (int) $depth );

        $output .= 
            $this->_dump( '<start_lvl />' )  .
            $this->_dump( $args )
            ;
    }


    // public function end_lvl( 
    //     string   &$output, 
    //     int       $depth = 0, 
    //     \stdClass $args  = null
    // ) : void {

    //     $this->_set_depth( $depth );
        
    //     $output .= 
    //         $this->_dump( '<end_lvl />' )  .
    //         $this->_dump( $args )
    //         ;

    //     return;
    // }


    // public function start_el( 
    //     string   &$output, 
    //     \WP_Post  $item, 
    //     int       $depth = 0, 
    //     \stdClass $args  = $this->emptyStdClass,
    //     int       $id 
    // ) : void {

    //     $this->_set_depth( $depth );
        
    //     $output .= 
    //         $this->_dump( '<start_el />' )  .
    //         $this->_dump( "ID:", $id )      .
    //         $this->_dump( 'Post:', $item )  .
    //         $this->_dump( 'Object:', $args )
    //         ;

    //     return;
    // }


    // public function end_el( 
    //     string   &$output, 
    //     \WP_Post  $item, 
    //     int       $depth = 0, 
    //     \stdClass $args  = $this->emptyStdClass
    // ) : void {

    //     $this->_set_depth( $depth );
        
    //     $output .= 
    //         $this->_dump( '<end_el />' )    .
    //         $this->_dump( 'Post:', $item )  .
    //         $this->_dump( 'Object:', $args )
    //         ;

    //     return;
    // }


    private function _dump( mixed ...$args ) : string {
        $output = '';

        foreach ( $args as $obj ) {
            $type = gettype( $obj );

            $output .= 
                match ( $type ) {
                    'boolean'   => $this->_tab()  .  $obj ? 'true' : 'false',
                    'string',
                    'integer',
                    'double'    => $this->_tab()  .  (string) $obj,
                    'array',
                    'object'    => $this->_indent_text_block( var_export( $obj, true ) ),
                    default     => $this->_tab()  .  "Type '{$type}' is invalid for an argument to _dump()"
                }  .
                "\n"
                ;
        }

        return $output;
    }


    private function _tab( int $spaces_per_tab = 4 ) : string {
        if ( $spaces_per_tab < 0 ) {
            throw new \InvalidArgumentException( "Spaces-per-tab parameter < 0: {$spaces_per_tab}" );
        }

        return str_repeat( ' ', $spaces_per_tab * 4 );
    }


    private function _indent_text_block( string $str ) : string {
        $lines = explode( "\n", $str );

        $lines = array_map( fn( $line ) => $this->_tab . "{$line}\n", $lines );

        return implode( "\n", $lines );
    }


    private function _set_depth( int $depth ) : int {
        if ( $depth < 0 ) {
            throw new \InvalidArgumentException( "Input parameter $depth < 0: {$depth}" );
        }

        $this->depth = $depth;

        return $this->depth;
    }
}

您在 start_lvl() 的参数中使用了类型,但 \Walker_Nav_menu 不是。

来自the source code

    /**
     * Starts the list before the elements are added.
     *
     * @since 3.0.0
     *
     * @see Walker::start_lvl()
     *
     * @param string   $output Used to append additional content (passed by reference).
     * @param int      $depth  Depth of menu item. Used for padding.
     * @param stdClass $args   An object of wp_nav_menu() arguments.
     */
    public function start_lvl( &$output, $depth = 0, $args = null ) 

您需要从方法的参数中删除类型