Skip to content

Commit fd878a3

Browse files
author
Tomáš Votruba
authored
Merge pull request #58 from peter-gribanov/lsp
Liskov Substitution Principle (LSP)
2 parents 67e68e5 + a71ad82 commit fd878a3

1 file changed

Lines changed: 67 additions & 48 deletions

File tree

README.md

Lines changed: 67 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,48 +1145,56 @@ if you model it using the "is-a" relationship via inheritance, you quickly
11451145
get into trouble.
11461146

11471147
**Bad:**
1148+
11481149
```php
1149-
class Rectangle {
1150-
private $width, $height;
1151-
1152-
public function __construct() {
1150+
class Rectangle
1151+
{
1152+
protected $width;
1153+
protected $height;
1154+
1155+
public function __construct()
1156+
{
11531157
$this->width = 0;
11541158
$this->height = 0;
11551159
}
1156-
1157-
public function setColor($color) {
1158-
// ...
1159-
}
1160-
1161-
public function render($area) {
1160+
1161+
public function render($area)
1162+
{
11621163
// ...
11631164
}
1164-
1165-
public function setWidth($width) {
1165+
1166+
public function setWidth($width)
1167+
{
11661168
$this->width = $width;
11671169
}
1168-
1169-
public function setHeight($height) {
1170+
1171+
public function setHeight($height)
1172+
{
11701173
$this->height = $height;
11711174
}
1172-
1173-
public function getArea() {
1175+
1176+
public function getArea()
1177+
{
11741178
return $this->width * $this->height;
11751179
}
11761180
}
11771181

1178-
class Square extends Rectangle {
1179-
public function setWidth($width) {
1182+
class Square extends Rectangle
1183+
{
1184+
public function setWidth($width)
1185+
{
11801186
$this->width = $this->height = $width;
11811187
}
1182-
1183-
public function setHeight(height) {
1188+
1189+
public function setHeight(height)
1190+
{
11841191
$this->width = $this->height = $height;
11851192
}
11861193
}
11871194

1188-
function renderLargeRectangles($rectangles) {
1189-
foreach($rectangles as $rectangle) {
1195+
function renderLargeRectangles($rectangles)
1196+
{
1197+
foreach ($rectangles as $rectangle) {
11901198
$rectangle->setWidth(4);
11911199
$rectangle->setHeight(5);
11921200
$area = $rectangle->getArea(); // BAD: Will return 25 for Square. Should be 20.
@@ -1199,61 +1207,71 @@ renderLargeRectangles($rectangles);
11991207
```
12001208

12011209
**Good:**
1210+
12021211
```php
1203-
abstract class Shape {
1204-
protected $width, $height;
1205-
1212+
abstract class Shape
1213+
{
1214+
protected $width;
1215+
protected $height;
1216+
12061217
abstract public function getArea();
1207-
1208-
public function setColor($color) {
1209-
// ...
1210-
}
1211-
1212-
public function render($area) {
1218+
1219+
public function render($area)
1220+
{
12131221
// ...
12141222
}
12151223
}
12161224

1217-
class Rectangle extends Shape {
1218-
public function __construct() {
1225+
class Rectangle extends Shape
1226+
{
1227+
public function __construct()
1228+
{
12191229
parent::__construct();
12201230
$this->width = 0;
12211231
$this->height = 0;
12221232
}
1223-
1224-
public function setWidth($width) {
1233+
1234+
public function setWidth($width)
1235+
{
12251236
$this->width = $width;
12261237
}
1227-
1228-
public function setHeight($height) {
1238+
1239+
public function setHeight($height)
1240+
{
12291241
$this->height = $height;
12301242
}
1231-
1232-
public function getArea() {
1243+
1244+
public function getArea()
1245+
{
12331246
return $this->width * $this->height;
12341247
}
12351248
}
12361249

1237-
class Square extends Shape {
1238-
public function __construct() {
1250+
class Square extends Shape
1251+
{
1252+
public function __construct()
1253+
{
12391254
parent::__construct();
12401255
$this->length = 0;
12411256
}
1242-
1243-
public function setLength($length) {
1257+
1258+
public function setLength($length)
1259+
{
12441260
$this->length = $length;
12451261
}
1246-
1247-
public function getArea() {
1262+
1263+
public function getArea()
1264+
{
12481265
return pow($this->length, 2);
12491266
}
12501267
}
12511268

1252-
function renderLargeRectangles($rectangles) {
1253-
foreach($rectangles as $rectangle) {
1269+
function renderLargeRectangles($rectangles)
1270+
{
1271+
foreach ($rectangles as $rectangle) {
12541272
if ($rectangle instanceof Square) {
12551273
$rectangle->setLength(5);
1256-
} else if ($rectangle instanceof Rectangle) {
1274+
} elseif ($rectangle instanceof Rectangle) {
12571275
$rectangle->setWidth(4);
12581276
$rectangle->setHeight(5);
12591277
}
@@ -1266,6 +1284,7 @@ function renderLargeRectangles($rectangles) {
12661284
$shapes = [new Rectangle(), new Rectangle(), new Square()];
12671285
renderLargeRectangles($shapes);
12681286
```
1287+
12691288
**[⬆ back to top](#table-of-contents)**
12701289

12711290
### Interface Segregation Principle (ISP)

0 commit comments

Comments
 (0)