SOLID/4-interface-segregation-principle.php
2018-01-19 15:03:24 +01:00

96 lines
1.4 KiB
PHP

<?php
// Interface Segregation Principle Violation
interface Workable
{
public function canCode();
public function code();
public function test();
}
class Programmer implements Workable
{
public function canCode()
{
return true;
}
public function code()
{
return 'coding';
}
public function test()
{
return 'testing in localhost';
}
}
class Tester implements Workable
{
public function canCode()
{
return false;
}
public function code()
{
throw new Exception('Opps! I can not code');
}
public function test()
{
return 'testing in test server';
}
}
class ProjectManagement
{
public function processCode(Workable $member)
{
if ($member->canCode()) {
$member->code();
}
}
}
// Refactored
interface Codeable
{
public function code();
}
interface Testable
{
public function test();
}
class Programmer implements Codeable, Testable
{
public function code()
{
return 'coding';
}
public function test()
{
return 'testing in localhost';
}
}
class Tester implements Testable
{
public function test()
{
return 'testing in test server';
}
}
class ProjectManagement
{
public function processCode(Codeable $member)
{
$member->code();
}
}