21CTO学院导读:不重复自己(Do not Repeat Yourself)是软件开发之要义,它的目标就是避免代码重复。
class Report{
public function show(array $data){
echo "Report: " . ucwords(strtolower($data["name"])) . " ";
echo "Product: " . ucwords(strtolower($data["product"])) . " ";
echo "Start date: " . date("Y/m/d", $data["startDate"]) . " ";
echo "End date: " . date("Y/m/d", $data["endDate"]) . " ";
echo "Total: " . $data["total"] . " ";
echo "Average x day: " . floor($data["total"] / 365) . " ";
echo "Average x week: " . floor($data["total"] / 52) . " ";
}
}
class Report {
public function show(array $data) {
echo "Report: " . ucwords(strtolower($data["name"])) . " ";
echo "Product: " . ucwords(strtolower($data["product"])) . " ";
echo "Start date: " . date("Y/m/d", $data["startDate"]) . " ";
echo "End date: " . date("Y/m/d", $data["endDate"]) . " ";
echo "Total: " . $data["total"] . " ";
echo "Average x day: " . floor($data["total"] / 365) . " ";
echo "Average x week: " . floor($data["total"] / 52) . " ";
echo "Average x month: " . floor($data["total"] / 12) . " ";
}
public function saveToFile(array $data) {
$report = ';
$report .= "Report: " . ucwords(strtolower($data["name"])) . " ";
$report .= "Product: " . ucwords(strtolower($data["product"])) . " ";
$report .= "Start date: " . date("Y/m/d", $data["startDate"]) . " ";
$report .= "End date: " . date("Y/m/d", $data["endDate"]) . " ";
$report .= "Total: " . $data["total"] . " ";
$report .= "Average x day: " . floor($data["total"] / 365) . " ";
$report .= "Average x week: " . floor($data["total"] / 52) . " ";
$report .= "Average x month: " . floor($data["total"] / 12) . " ";
file_put_contents("./report.txt", $report);
}
}
class Report
{
public function show(array $data)
{
echo $this->createReport($data);
}
public function saveToFile(array $data)
{
file_put_contents("./report.txt", $this->createReport($data));
}
private function createReport(array $data): string
{
$report = ';
$report .= "Report: " . ucwords(strtolower($data["name"])) . " ";
$report .= "Product: " . ucwords(strtolower($data["product"])) . " ";
$report .= "Start date: " . date("Y/m/d", $data["startDate"]) . " ";
$report .= "End date: " . date("Y/m/d", $data["endDate"]) . " ";
$report .= "Total: " . $data["total"] . " ";
$report .= "Average x day: " . floor($data["total"] / 365) . " ";
$report .= "Average x week: " . floor($data["total"] / 52) . " ";
$report .= "Average x month: " . floor($data["total"] / 12) . " ";
return $report;
}
}
$report .= "Report: " . ucwords(strtolower($data["name"])) . " ";
$report .= "Product: " . ucwords(strtolower($data["product"])) . " ";
private function normalizeName($name): string
{
return ucwords(strtolower($name));
}
$report .= "Start date: " . date("Y/m/d", $data["startDate"]) . " ";
$report .= "End date: " . date("Y/m/d", $data["endDate"]) . " ";
private function formatDate($date): string
{
return date("Y/m/d", $date);
}
$report .= "Average x day: " . floor($data["total"] / 365) . " ";
$report .= "Average x week: " . floor($data["total"] / 52) . " ";
$report .= "Average x month: " . floor($data["total"] / 12) . " ";
private function calculateAverage(array $data, $period): string
{
return floor($data["total"] / $period);
}
class Report
{
public function show(array $data)
{
echo $this->createReport($data);
}
public function saveToFile(array $data)
{
file_put_contents("./report.txt", $this->createReport($data));
}
private function createReport(array $data)
{
$report = ';
$report .= "Report: " . $this->normalizeName($data["name"]) . " ";
$report .= "Product: " . $this->normalizeName($data["product"]) . " ";
$report .= "Start date: " . $this->formatDate($data["startDate"]) . " ";
$report .= "End date: " . $this->formatDate($data["endDate"]) . " ";
$report .= "Total: " . $data["total"] . " ";
$report .= "Average x day: " . $this->calculateAverage($data, 365) . " ";
$report .= "Average x week: " . $this->calculateAverage($data, 52) . " ";
$report .= "Average x month: " . $this->calculateAverage($data, 12) . " ";
return $report;
}
private function formatDate($date): string
{
return date("Y/m/d", $date);
}
private function calculateAverage(array $data, $period): string
{
return floor($data["total"] / $period);
}
private function normalizeName($name): string
{
return ucwords(strtolower($name));
}
}
作者:正明
来源:21CTO学院
本文为 @ 21CTO 创作并授权 21CTO 发布,未经许可,请勿转载。
内容授权事宜请您联系 webmaster@21cto.com或关注 21CTO 公众号。
该文观点仅代表作者本人,21CTO 平台仅提供信息存储空间服务。