Skip to content

Don't call $callback->bindTo() #191

@jtreminio

Description

@jtreminio

Both CliMenuBuilder::addSplitItem() and SplitItemBuilder::addSubMenu() call $callback = $callback->bindTo($builder);

This overrides $this within the context of the callee, forcing the use of $self = $this or similar:

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $self = $this;
        
        $menu = (new CliMenuBuilder)
            ->setBackgroundColour('black')
            ->setForegroundColour('green')
            ->setTitle('Create a New Project')
            ->addLineBreak()
            ->addStaticItem('Select Language')
            ->addSubMenu('PHP', function (CliMenuBuilder $b) use ($self) {
                $self->newPhpProject($b);
            })
            ->addItem('Javascript', function (CliMenu $menu) {})
            ->addItem('Ruby', function (CliMenu $menu) {})
            ->addItem('Python', function (CliMenu $menu) {})
            ->addItem('Go', function (CliMenu $menu) {})
            ->addLineBreak()
            ->build();

        $menu->open();

        return 0;
    }

    protected function newPhpProject(CliMenuBuilder $builder)
    {
        $symfonySubMenu = function (CliMenuBuilder $b) {
            $symfony = new Project\Symfony($b);
            $symfony->init();
        };

        $builder->setTitle('Choose PHP Project Type')
            ->addSubMenu('Symfony', $symfonySubMenu)
            ->addItem('Laravel', function (CliMenu $menu) {})
            ->addItem('Wordpress', function (CliMenu $menu) {})
            ->addItem('Drupal', function (CliMenu $menu) {})
            ->addItem('Pure', function (CliMenu $menu) {})
            ->build()
        ;
    }

Within the anonymous function, $this refers to the same $b:

            ->addSubMenu('PHP', function (CliMenuBuilder $b) use ($self) {
                $self->newPhpProject($b);
            })

But CliMenuBuilder::addSplitItem() and SplitItemBuilder::addSubMenu() already pass the builder object to the callback, $callback($builder); (also seen above in function (CliMenuBuilder $b)).

Removing $callback = $callback->bindTo($builder); lets us use $this as expected:

            ->addSubMenu('PHP', function (CliMenuBuilder $b) {
                $this->newPhpProject($b);
            })

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions