Skip to content

Commit

Permalink
LSP
Browse files Browse the repository at this point in the history
  • Loading branch information
charlie-goldenowl committed Oct 29, 2022
1 parent 45f8ac3 commit b44fe4e
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/Solid/LSP/Bad/BeefItem.php
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);
}
}
15 changes: 15 additions & 0 deletions app/Solid/LSP/Bad/BeverageItem.php
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);
}
}
32 changes: 32 additions & 0 deletions app/Solid/LSP/Bad/LiskovExample.php
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);
}
}
23 changes: 23 additions & 0 deletions app/Solid/LSP/Bad/MenuItem.php
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;
}
}
11 changes: 11 additions & 0 deletions app/Solid/LSP/Good/BeefItem.php
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);
}
}
21 changes: 21 additions & 0 deletions app/Solid/LSP/Good/BeverageItem.php
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);
}
}
31 changes: 31 additions & 0 deletions app/Solid/LSP/Good/LiskovExample.php
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);
}
}
28 changes: 28 additions & 0 deletions app/Solid/LSP/Good/MenuItem.php
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;
}
}

0 comments on commit b44fe4e

Please # to comment.