Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function arguments (2 or fewer ideally) #111

Closed
peter-gribanov opened this issue Sep 14, 2017 · 3 comments
Closed

Function arguments (2 or fewer ideally) #111

peter-gribanov opened this issue Sep 14, 2017 · 3 comments

Comments

@peter-gribanov
Copy link
Contributor

Link to section.

A little strange.

In Bad example, the createMenu() function has 4 required dependencies ($title, $body, $buttonText, $cancellable).

In Good example, the createMenu() function has only 1 required dependency (MenuConfig).
But MenuConfig does not have required dependencies.
This means that we do not have guarantees that all the necessary dependencies will be in createMenu() function.

This means that the Good example does not solve the problems in the Bad example.

@yuriyzinchenko
Copy link
Contributor

@peter-gribanov It's a good point. I think that solution is to use private properties, setters and typed getters. E.g.

class MenuConfig
{
    private $title;
    private $body;
    private $buttonText;
    private $cancellable = false;

    public function getTitle(): string
    {
        return $this->title;
    }

    public function setTitle(string $title): void
    {
        $this->title = $title;
    }

    public function getBody(): string
    {
        return $this->body;
    }

    public function setBody(string $body): void
    {
        $this->body = $body;
    }

    public function getButtonText(): string
    {
        return $this->buttonText;
    }

    public function setButtonText(string $buttonText): void
    {
        $this->buttonText = $buttonText;
    }

    public function getCancellable(): bool
    {
        return $this->cancellable;
    }

    public function setCancellable(bool $cancellable): void
    {
        $this->cancellable = $cancellable;
    }

}

It means that if required properties(title, body, buttonText) are not set the TypeError exception will be thrown. But if optional property isn't set all will be ok and default value will be returned.

@peter-gribanov
Copy link
Contributor Author

@yuriyzinchenko Yes, by adding access methods we return control over the data types.
You can make a PR for fix it.

@peter-gribanov
Copy link
Contributor Author

This issue was about the problem of required properties, but i now looked more closely and realized that there was no problem. Sorry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants