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

LSP ambiguous example #119

Open
Sinuide opened this issue Sep 21, 2017 · 2 comments
Open

LSP ambiguous example #119

Sinuide opened this issue Sep 21, 2017 · 2 comments

Comments

@Sinuide
Copy link

Sinuide commented Sep 21, 2017

In the comment for Liskov Substitution Principle bad example :

function renderLargeRectangles(Rectangle $rectangles): void
{
    foreach ($rectangles as $rectangle) {
        $rectangle->setWidth(4);
        $rectangle->setHeight(5);
        $area = $rectangle->getArea(); // BAD: Will return 25 for Square. Should be 20.
        $rectangle->render($area);
    }
}

$rectangles = [new Rectangle(), new Rectangle(), new Square()];
renderLargeRectangles($rectangles);

For a square the math is right as getArea() should return 25, not 20.
I understand that it implies 20 for 4x5, but this is only for not square and Square is explicitely used.
The example and comment "Should be 20" are confusing.

Edit :
Additionnaly, if the height/width/length are in constructor as seen in acf92a1 and not defined in a function.
You then have :

function renderLargeRectangles(Rectangle $rectangles): void
{
    foreach ($rectangles as $rectangle) {
        $area = $rectangle->getArea(); // GOOD: Will return 25 for Square.
        $rectangle->render($area);
    }
}

$rectangles = [new Rectangle(4, 5), new Rectangle(4, 5), new Square(5)];
renderLargeRectangles($rectangles);

And the example becomes correct but I'm being fussy.

@peter-gribanov
Copy link
Contributor

@Sinuide This is old problem and @jupeter unfortunately doesn't want to solve it #37, #46, #66, #101, #106

@peter-gribanov
Copy link
Contributor

fixed in #106 and #101

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