Skip to content

Instantly share code, notes, and snippets.

@sanslan
Created January 1, 2022 14:00
Show Gist options
  • Save sanslan/d5bf68a8eb2f1bd0f2755c5b4916a663 to your computer and use it in GitHub Desktop.
Save sanslan/d5bf68a8eb2f1bd0f2755c5b4916a663 to your computer and use it in GitHub Desktop.
<?php
/**
There is a code supporting calculation if a car is damaged.
Now it should be extended to support calculating if a painting of car's exterior is damaged (this means, if a painting of any of car details is not OK - for example a door is scratched).
*/
abstract class CarDetail {
protected bool $isBroken;
protected bool $isPaintingDamaged;
public function __construct(bool $isBroken,bool $isPaintingDamaged=false)
{
$this->isBroken = $isBroken;
$this->isPaintingDamaged = $isPaintingDamaged;
}
public function isBroken(): bool
{
return $this->isBroken;
}
public function isPaintingDamaged(): bool
{
return $this->isPaintingDamaged;
}
}
class Door extends CarDetail
{
}
class Tyre extends CarDetail
{
}
class Car
{
/**
* @var CarDetail[]
*/
private $details;
/**
* @param CarDetail[] $details
*/
public function __construct(array $details)
{
$this->details = $details;
}
private function checkDamage(string $damage_name):bool
{
if(!method_exists($this, $damage_name)){
throw new Exception('Method not exists');
}
foreach ($this->details as $detail) {
if ($detail->$damage_name()) {
return true;
}
}
return false;
}
public function isBroken(): bool
{
return $this->checkDamage('isBroken');
}
public function isPaintingDamaged(): bool
{
return $this->checkDamage('isPaintingDamaged');
}
}
$car = new Car([new Door(true,true), new Tyre(false)]);
// we pass a list of all details
/**
Expected result: an implemented code.
Note: you are allowed (and encouraged) to change anything in the existing code in order to make an implementation SOLID compliant
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment