phpwordpresssignaturewp-nav-walker

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


I'm using the code below in a WordPress plugin project. VS Code is giving me an error that says:

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

Here's the code snippet that's generating the error (first part of function only):

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

UPDATE I followed @Spoody's advice and changed the function to this:

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

...but I still receive the same error. Any other ideas?

MORE DETAILS At @M. Eriksson's request, here's the entire 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;
    }
}

Solution

  • You are using types in your start_lvl()'s parameters but \Walker_Nav_menu is not.

    From 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 ) 
    

    You need to remove types from your method's parameters