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
|
|||
13 | ->setTitle('CLI Menu') |
||
14 | ->addItem('First Item', $itemCallable) |
||
15 | ->addItem('Second Item', $itemCallable) |
||
16 | ->addLineBreak('-') |
||
17 | ->addSubMenu('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 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: