Git Product home page Git Product logo

Comments (3)

yuriyzinchenko avatar yuriyzinchenko commented on May 20, 2024

@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.

from clean-code-php.

peter-gribanov avatar peter-gribanov commented on May 20, 2024

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

from clean-code-php.

peter-gribanov avatar peter-gribanov commented on May 20, 2024

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

from clean-code-php.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.