Completed
Push — master ( df8184...e3b994 )
by Aydin
02:31
created

examples/submenu.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
use PhpSchool\CliMenu\CliMenu;
4
use PhpSchool\CliMenu\Builder\CliMenuBuilder;
5
6
require_once(__DIR__ . '/../vendor/autoload.php');
7
8
$itemCallable = function (CliMenu $menu) {
9
    echo $menu->getSelectedItem()->getText();
10
};
11
12
$menu = (new CliMenuBuilder)
0 ignored issues
show
The method setWidth does only exist in PhpSchool\CliMenu\Builder\CliMenuBuilder, but not in PhpSchool\CliMenu\Builder\SplitItemBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
13
    ->setTitle('CLI Menu')
14
    ->addItem('First Item', $itemCallable)
15
    ->addItem('Second Item', $itemCallable)
16
    ->addLineBreak('-')
17
    ->addSubMenu('sub-menu-1', 'Options')
18
        ->setTitle('CLI Menu > Options')
19
        ->addItem('First option', function (CliMenu $menu) {
20
            echo sprintf('Executing option: %s', $menu->getSelectedItem()->getText());
21
        })
22
        ->addLineBreak('-')
23
        ->end()
24
    ->setWidth(70)
25
    ->setBackgroundColour('yellow')
26
    ->build();
27
28
$menu->open();
29