-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45f8ac3
commit b44fe4e
Showing
8 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace App\Solid\LSP\Bad; | ||
|
||
class BeefItem extends MenuItem | ||
{ | ||
public function __construct(int $price, string $name, string $description) | ||
{ | ||
parent::__construct($price, $name, $description); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace App\Solid\LSP\Bad; | ||
|
||
class BeverageItem extends MenuItem | ||
{ | ||
public function __construct(int $price, string $name, string $description) | ||
{ | ||
parent::__construct($price, $name, $description); | ||
} | ||
|
||
public function getPriceWithDiscount(int $discountPercent){ | ||
return $this->price - ($this->price * $discountPercent/ 100); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace App\Solid\LSP\Bad; | ||
|
||
class LiskovExample | ||
{ | ||
/** | ||
* @throws \Exception | ||
*/ | ||
public function printItemPrice($item){ | ||
if($item instanceof BeverageItem){ | ||
echo "\nBeverage price:"; | ||
echo $item->getPriceWithDiscount(10); | ||
}else if($item instanceof BeefItem){ | ||
echo "\nBeef price:"; | ||
echo $item->getPrice(); | ||
}else{ | ||
throw new \Exception("Item not found"); | ||
} | ||
} | ||
|
||
/** | ||
* @throws \Exception | ||
*/ | ||
public function main(){ | ||
$beverageItem = new BeverageItem(200, "Orange","big"); | ||
$beefItem = new BeefItem(100, "Wagyu","small"); | ||
|
||
$this->printItemPrice($beverageItem); | ||
$this->printItemPrice($beefItem); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace App\Solid\LSP\Bad; | ||
|
||
class MenuItem | ||
{ | ||
|
||
public int $price; | ||
public string $name; | ||
public string $description; | ||
|
||
|
||
public function __construct(int $price, string $name, string $description) | ||
{ | ||
$this->price = $price; | ||
$this->name = $name; | ||
$this->description = $description; | ||
} | ||
|
||
public function getPrice(){ | ||
return $this->price; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace App\Solid\LSP\Good; | ||
|
||
class BeefItem extends MenuItem | ||
{ | ||
public function __construct(int $price, string $name, string $description) | ||
{ | ||
parent::__construct($price, $name, $description); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace App\Solid\LSP\Good; | ||
|
||
class BeverageItem extends MenuItem | ||
{ | ||
public function __construct(int $price, string $name, string $description) | ||
{ | ||
parent::__construct($price, $name, $description); | ||
} | ||
|
||
public function getPrice() | ||
{ | ||
return $this->price - $this->getDiscount(); | ||
} | ||
|
||
private function getDiscount(){ | ||
$discountPercent = 10; | ||
return ($this->price * $discountPercent/ 100); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace App\Solid\LSP\Good; | ||
|
||
class LiskovExample | ||
{ | ||
/** | ||
* @throws \Exception | ||
*/ | ||
public function printItemPrice($item){ | ||
if($item instanceof BeverageItem){ | ||
echo "\nBeverage price:"; | ||
}else if($item instanceof BeefItem){ | ||
echo "\nBeef price:"; | ||
}else{ | ||
throw new \Exception("Item not found"); | ||
} | ||
echo $item->getPrice(); | ||
} | ||
|
||
/** | ||
* @throws \Exception | ||
*/ | ||
public function main(){ | ||
$beverageItem = new BeverageItem(200, "Orange","big"); | ||
$beefItem = new BeefItem(100, "Wagyu","small"); | ||
|
||
$this->printItemPrice($beverageItem); | ||
$this->printItemPrice($beefItem); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace App\Solid\LSP\Good; | ||
|
||
class MenuItem | ||
{ | ||
|
||
public int $price; | ||
public string $name; | ||
public string $description; | ||
|
||
|
||
public function __construct(int $price, string $name, string $description) | ||
{ | ||
$this->price = $price; | ||
$this->name = $name; | ||
$this->description = $description; | ||
} | ||
|
||
public function getPrice() | ||
{ | ||
return $this->price - $this->getDiscount(); | ||
} | ||
|
||
private function getDiscount(){ | ||
return 0; | ||
} | ||
} |