Skip to content

Instantly share code, notes, and snippets.

@inetfuture
Created August 1, 2016 13:41
Show Gist options
  • Save inetfuture/61a40d1cf5ec9b623afe7ab95b201191 to your computer and use it in GitHub Desktop.
Save inetfuture/61a40d1cf5ec9b623afe7ab95b201191 to your computer and use it in GitHub Desktop.
clean-functions-examples
class Employee {
public Money calculatePay() {
switch (this.type) {
case COMMISSIONED:
return this.calculateCommissionedPay();
case HOURLY:
return this.calculateHourlyPay();
case SALARIED:
return this.calculateSalariedPay();
default:
throw new InvalidEmployeeType(this.type);
}
}
}
<?php
class Logger {
public static function log($message, $context, $isDebug) {
$logFile = Yii::$app->getRuntimePath() . DIRECTORY_SEPARATOR . 'logs' . DIRECTORY_SEPARATOR . 'test.log';
$logPath = dirname($logFile);
if (!is_dir($logPath)) {
FileHelper::createDirectory($logPath, 0775, true);
}
if (is_string($message)) {
if (is_array($context)) {
$time = new DateTime()->format('Y-m-d H:i:s');
$level = $isDebug ? 'debug' : 'info';
$logText = "[$time] [$level] [$message] $context";
$file = @fopen($logFile, 'a');
clearstatcache();
if (filesize($logFile) > 1024 * 1024) {
$logPath = dirname($logFile);
$filenames = scandir($logPath);
$maxIndex = 0;
foreach ($filenames as $filename) {
if (is_file($logPath . DIRECTORY_SEPARATOR . $filename)) {
if (strpos($filename, self::FILE_NAME . '.') !== false) {
$newIndex = substr($filename, strlen(self::FILE_NAME . '.'));
$maxIndex = max($maxIndex, $newIndex);
}
}
}
while ($maxIndex >= 0) {
$rotateFile = $logFile . ($maxIndex == 0 ? '' : '.' . $maxIndex);
@copy($rotateFile, $logFile . '.' . ($maxIndex + 1));
if ($file = @fopen($rotateFile, 'a')) {
@ftruncate($file, 0);
@fclose($file);
}
$maxIndex--;
}
fclose($file);
file_put_contents($logFile, $text, FILE_APPEND);
} else {
fwrite($file, $text);
fclose($file);
}
chmod($logFile, 0775);
return true;
} else {
return false;
}
} else {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment