mirror of
https://github.com/wataridori/solid-php-example.git
synced 2025-01-09 09:19:50 +00:00
95 lines
1.3 KiB
PHP
95 lines
1.3 KiB
PHP
|
<?php
|
||
|
|
||
|
// Interface Segregation Principle Violation
|
||
|
interface Workable
|
||
|
{
|
||
|
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 Workable
|
||
|
{
|
||
|
public function test()
|
||
|
{
|
||
|
return 'testing in test server';
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class ProjectManagement
|
||
|
{
|
||
|
public function processCode(Codeable $member)
|
||
|
{
|
||
|
$member->code();
|
||
|
}
|
||
|
}
|