【问题标题】:Why is VS Code saying this PHP code doesn't match signatures?为什么 VS Code 说这个 PHP 代码与签名不匹配?
【发布时间】:2022-01-15 05:50:43
【问题描述】:

我在 WordPress 插件项目中使用下面的代码。 VS Code 给我一个错误,上面写着:

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

这是产生错误的代码 sn-p(仅限函数的第一部分):

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。 Eriksson 的请求,这是整个课程:

<?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;
    }
}

【问题讨论】:

  • 请始终发布所有代码(如原始方法)。如果我们只能看到您的版本而不是原始版本,很难知道问题出在哪里。我们需要知道您使用的确切代码(我们不知道该插件在不同版本中是否有不同的签名以及您使用的是什么版本)。不要指望我们猜测或假设。

标签: php wordpress signature wp-nav-walker


【解决方案1】:

您在 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 ) 

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

【讨论】:

  • 感谢您的回答,但它给了我相同的结果...
  • 在您更新的问题中,$args 仍然有stdClass,您的方法必须与我上面发布的源代码中的方法相同
  • 斯波迪,谢谢。这就像一个冠军。
猜你喜欢
  • 2019-12-04
  • 2023-04-08
  • 2017-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-30
  • 2019-03-06
  • 1970-01-01
相关资源
最近更新 更多