Skip to content

Commit b19b3a1

Browse files
author
Tomáš Votruba
authored
Merge pull request piotrplenik#101 from peter-gribanov/lsp3
Liskov Substitution Principle (LSP)
2 parents a1ac1b0 + 6c1da67 commit b19b3a1

File tree

1 file changed

+33
-41
lines changed

1 file changed

+33
-41
lines changed

README.md

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,11 +1677,6 @@ class Rectangle
16771677
protected $width = 0;
16781678
protected $height = 0;
16791679

1680-
public function render(int $area): void
1681-
{
1682-
// ...
1683-
}
1684-
16851680
public function setWidth(int $width): void
16861681
{
16871682
$this->width = $width;
@@ -1711,40 +1706,40 @@ class Square extends Rectangle
17111706
}
17121707
}
17131708

1714-
/**
1715-
* @param Rectangle[] $rectangles
1716-
*/
1717-
function renderLargeRectangles(array $rectangles): void
1709+
function printArea(Rectangle $rectangle): void
17181710
{
1719-
foreach ($rectangles as $rectangle) {
1720-
$rectangle->setWidth(4);
1721-
$rectangle->setHeight(5);
1722-
$area = $rectangle->getArea(); // BAD: Will return 25 for Square. Should be 20.
1723-
$rectangle->render($area);
1724-
}
1711+
$rectangle->setWidth(4);
1712+
$rectangle->setHeight(5);
1713+
1714+
// BAD: Will return 25 for Square. Should be 20.
1715+
echo sprintf('%s has area %d.', get_class($rectangle), $rectangle->getArea()).PHP_EOL;
17251716
}
17261717

1727-
$rectangles = [new Rectangle(), new Rectangle(), new Square()];
1728-
renderLargeRectangles($rectangles);
1718+
$rectangles = [new Rectangle(), new Square()];
1719+
1720+
foreach ($rectangles as $rectangle) {
1721+
printArea($rectangle);
1722+
}
17291723
```
17301724

17311725
**Good:**
17321726

1727+
The best way is separate the quadrangles and allocation of a more general subtype for both shapes.
1728+
1729+
Despite the apparent similarity of the square and the rectangle, they are different.
1730+
A square has much in common with a rhombus, and a rectangle with a parallelogram, but they are not subtype.
1731+
A square, a rectangle, a rhombus and a parallelogram are separate shapes with their own properties, albeit similar.
1732+
17331733
```php
1734-
abstract class Shape
1734+
interface Shape
17351735
{
1736-
abstract public function getArea(): int;
1737-
1738-
public function render(int $area): void
1739-
{
1740-
// ...
1741-
}
1736+
public function getArea(): int;
17421737
}
17431738

1744-
class Rectangle extends Shape
1739+
class Rectangle implements Shape
17451740
{
1746-
private $width;
1747-
private $height;
1741+
private $width = 0;
1742+
private $height = 0;
17481743

17491744
public function __construct(int $width, int $height)
17501745
{
@@ -1758,9 +1753,9 @@ class Rectangle extends Shape
17581753
}
17591754
}
17601755

1761-
class Square extends Shape
1756+
class Square implements Shape
17621757
{
1763-
private $length;
1758+
private $length = 0;
17641759

17651760
public function __construct(int $length)
17661761
{
@@ -1769,23 +1764,20 @@ class Square extends Shape
17691764

17701765
public function getArea(): int
17711766
{
1772-
return pow($this->length, 2);
1773-
}
1767+
       return $this->length ** 2;
1768+
   }
17741769
}
17751770

1776-
/**
1777-
* @param Rectangle[] $rectangles
1778-
*/
1779-
function renderLargeRectangles(array $rectangles): void
1771+
function printArea(Shape $shape): void
17801772
{
1781-
foreach ($rectangles as $rectangle) {
1782-
$area = $rectangle->getArea();
1783-
$rectangle->render($area);
1784-
}
1773+
echo sprintf('%s has area %d.', get_class($shape), $shape->getArea()).PHP_EOL;
17851774
}
17861775

1787-
$shapes = [new Rectangle(4, 5), new Rectangle(4, 5), new Square(5)];
1788-
renderLargeRectangles($shapes);
1776+
$shapes = [new Rectangle(4, 5), new Square(5)];
1777+
1778+
foreach ($shapes as $shape) {
1779+
printArea($shape);
1780+
}
17891781
```
17901782

17911783
**[⬆ back to top](#table-of-contents)**

0 commit comments

Comments
 (0)