Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion app/Controllers/Home.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ class Home extends BaseController
{
public function index()
{
return $this->response->redirect('/sample');
return view('sample/sample', [
'title' => 'ci4Sample',
'location' => 'home',
'menu' => '',
]);
}
}
53 changes: 53 additions & 0 deletions app/Controllers/Refactoring/Basic/Exam1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
namespace App\Controllers\Refactoring\Basic;

use App\Controllers\BaseController;
use CodeIgniter\I18n\Time;

/**
* Description of Exam1
*
* @author hoksi
*/
class Exam1 extends BaseController
{
public function printOwing($invoice)
{
$outstanding = 0;

echo "*****************\n";
echo "*** 고객 채무 ***\n";
echo "*****************\n";

foreach($invoice['orders'] as $o) {
$outstanding += $o['amount'];
}

$time = Time::now();
$invoice['dueDate'] = $time->addDays(30);

echo "고객명 : {$invoice['customer']}\n";
echo "채무액 : {$outstanding}\n";
echo "마감일 : {$invoice['dueDate']}\n";
}

public function index()
{
$invoice = [
'customer' => 'customer',
'orders' => [
['amount' => 100],
['amount' => 110],
['amount' => 120],
['amount' => 130],
['amount' => 140],
],
'dueDate' => '',
];

echo '<pre>';
$this->printOwing($invoice);
echo '</pre>';
}

}
65 changes: 65 additions & 0 deletions app/Controllers/Refactoring/Basic/Exam2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
namespace App\Controllers\Refactoring\Basic;

use App\Controllers\BaseController;

/**
* Description of Exam2
*
* @author hoksi
*/
class Exam2 extends BaseController
{
protected function rating1($aDriver)
{
return ($this->moreThanFiveLateDeliveries($aDriver) ? 2 : 1);
}

protected function moreThanFiveLateDeliveries($aDriver)
{
return $aDriver['numberOfLateDeliveries'] > 5;
}

protected function rating2($aDriver)
{
return ($this->lessThanFiveLateDeliveries($aDriver) ? 2 : 1);
}

protected function lessThanFiveLateDeliveries($drv)
{
return $drv['numberOfLateDeliveries'] < 5;
}

protected function reportLines($aCustomer)
{
$lines = [];
$lines = $this->getherCustomerData($lines, $aCustomer);
return $lines;
}

protected function getherCustomerData($out, $aCustomer)
{
$out['name'] = $aCustomer['name'];
$out['location'] = $aCustomer['location'];

return $out;
}

public function index()
{
$aDriver = [
'numberOfLateDeliveries' => 3
];

$aCustomer = [
'name' => 'Name',
'location' => 'Location',
];

echo '<pre>';
echo "rating1 = {$this->rating1($aDriver)}\n";
echo "rating2 = {$this->rating2($aDriver)}\n";
echo "reportLines = " . print_r($this->reportLines($aCustomer), true);
echo '</pre>';
}
}
34 changes: 34 additions & 0 deletions app/Controllers/Refactoring/Basic/Exam3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Controllers\Refactoring\Basic;

use App\Controllers\BaseController;

/**
* Description of Exam3
*
* @author hoksi
*/
class Exam3 extends BaseController
{

protected function price($order)
{
// 가격(price) = 기본 가격 - 수량 할인 + 배송비
return $order['quantity'] * $order['itemPrice'] -
max(0, $order['quantity'] - 500) * $order['itemPrice'] * 0.05 +
min($order['quantity'] * $order['itemPrice'] * 0.1, 100);
}

public function index()
{
$order = [
'quantity' => 5,
'itemPrice' => 1000
];

echo '<pre>';
echo "price = {$this->price($order)}";
echo '</pre>';
}
}
26 changes: 26 additions & 0 deletions app/Controllers/Refactoring/Basic/Exam4.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Controllers\Refactoring\Basic;

use App\Controllers\BaseController;

/**
* Description of Exam4
*
* @author hoksi
*/
class Exam4 extends BaseController
{
public $defaultOwner = ['firstName' => 'mr', 'lastName' => 'he'];

public function index()
{
$spaceship['owner'] = $this->defaultOwner;
$this->defaultOwner = ['firstName' => 'mrs', 'lastName' => 'her'];

echo '<pre>';
echo 'spaceship = ' . print_r($spaceship, true) . "\n";
echo 'defaultOwner = ' . print_r($this->defaultOwner, true);
echo '</pre>';
}
}
39 changes: 39 additions & 0 deletions app/Controllers/Refactoring/Basic/Exam5.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
namespace App\Controllers\Refactoring\Basic;

use App\Controllers\BaseController;

/**
* Description of Exam5
*
* @author hoksi
*/
class Exam5 extends BaseController
{
protected function readingsOutsideRange($station, $min, $max)
{
return array_filter($station['readings'], function($r) use ($min, $max) {
return ($r['temp'] < $min || $r['temp'] > $max);
});

}

public function index()
{
$station = [
'name' => 'ZB!',
'readings' => [
['temp' => 47, 'time' => '2016-11-10 09:10'],
['temp' => 53, 'time' => '2016-11-10 09:20'],
['temp' => 58, 'time' => '2016-11-10 09:30'],
['temp' => 53, 'time' => '2016-11-10 09:40'],
['temp' => 51, 'time' => '2016-11-10 09:50'],
],
];

echo '<pre>';
print_r($this->readingsOutsideRange($station, 50, 55));
echo '</pre>';

}
}
73 changes: 73 additions & 0 deletions app/Controllers/Refactoring/Basic/Exam6.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
namespace App\Controllers\Refactoring\Basic;

use App\Controllers\BaseController;

function acquireReading()
{
$reading = [
'customer' => 'ivan',
'quantity' => 10,
'month' => 5,
'year' => 2017,
];

return $reading;
}

function taxThreshold($year)
{
return $year > 2015 ? 0 : 0.5;
}

function baseRate($month, $year)
{
return $year / $month;
}

function calculateBaseCharge($aReading)
{
return baseRate($aReading['month'], $aReading['year']) * $aReading['quantity'];
}

/**
* Description of Exam6
*
* @author hoksi
*/
class Exam6 extends BaseController
{
protected function client1()
{
$aReading = acquireReading();
$baseCharge = baseRate($aReading['month'], $aReading['year']) * $aReading['quantity'];

return $baseCharge;
}

protected function client2()
{
$aReading = acquireReading();
$base = (baseRate($aReading['month'], $aReading['year']) * $aReading['quantity']);
$taxableCharge = max(0, $base - taxThreshold($aReading['year']));

return $taxableCharge;
}

protected function client3()
{
$aReading = acquireReading();
$baseCharge = calculateBaseCharge($aReading);

return $baseCharge;
}

public function index()
{
echo '<pre>';
echo "client1 : {$this->client1()}\n";
echo "client2 : {$this->client2()}\n";
echo "client3 : {$this->client3()}\n";
echo '</pre>';
}
}
73 changes: 73 additions & 0 deletions app/Controllers/Refactoring/Basic/Exam7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
namespace App\Controllers\Refactoring\Basic;

use App\Controllers\BaseController;

function acquireReading()
{
$reading = [
'customer' => 'ivan',
'quantity' => 10,
'month' => 5,
'year' => 2017,
];

return $reading;
}

function taxThreshold($year)
{
return $year > 2015 ? 0 : 0.5;
}

function baseRate($month, $year)
{
return $year / $month;
}

function calculateBaseCharge($aReading)
{
return baseRate($aReading['month'], $aReading['year']) * $aReading['quantity'];
}

/**
* Description of Exam7
*
* @author hoksi
*/
class Exam7 extends BaseController
{
protected function client1()
{
$aReading = acquireReading();
$baseCharge = baseRate($aReading['month'], $aReading['year']) * $aReading['quantity'];

return $baseCharge;
}

protected function client2()
{
$aReading = acquireReading();
$base = (baseRate($aReading['month'], $aReading['year']) * $aReading['quantity']);
$taxableCharge = max(0, $base - taxThreshold($aReading['year']));

return $taxableCharge;
}

protected function client3()
{
$aReading = acquireReading();
$baseCharge = calculateBaseCharge($aReading);

return $baseCharge;
}

public function index()
{
echo '<pre>';
echo "client1 : {$this->client1()}\n";
echo "client2 : {$this->client2()}\n";
echo "client3 : {$this->client3()}\n";
echo '</pre>';
}
}
Loading