Skip to content

Instantly share code, notes, and snippets.

@yinian1992
Created October 2, 2012 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yinian1992/3820432 to your computer and use it in GitHub Desktop.
Save yinian1992/3820432 to your computer and use it in GitHub Desktop.
A php line number counter just for pratice.
<?php
/**
* Code Calculator
*
* count line number of php project
* @author yinian1992
*
*/
// define('BASEPATH', 'absolute path of your php project');
define('BASEPATH', str_replace('\\', '/', dirname(__FILE__) . '/../' ));
function timer_start() {
static $t;
return $t = microtime();
}
function timer_stop($precision = 4) {
static $t;
return round(microtime()-$t, 4) ;
}
class CodeCalculator {
private $extension = 'php';
private $each_file = true;
private $skip_line = array('//', '/*', '*', '#');
private $skip_file = array('.', '..','dev', '.git', '.settings', '.buildpath', '.htaccess', '.gitignore', '.project');
public $max_level = 0;
public function __construct($ext, $no_skip_line = false) {
if ( $no_skip_line == true ) {
$this->skip_line = array();
}
$this->set_extension($ext);
}
private function set_extension($ext, $dir = BASEPATH) {
$this->extension = $ext;
$this->calc_dir($dir);
echo '<div id="total"><p>Total lines: '.$this->total_calc.'</p></div>';
}
public function add_rule($type, $rule) {
if ( $type == 'line' ) {
$this->skip_line[] = $rule;
} else if ( $type == 'directory' ) {
$this->skip_directory[] = $rule;
} else {
return false;
}
}
public function calc_dir($dir = BASEPATH, $level = 0) {
if ( $this->max_level <= $level ) {
$this->max_level = $level;
}
if ( is_dir($dir) ) {
$dir_counter = 0;
$sub_counter = 0;
$handle = opendir($dir);
while ( false !== ( $file = readdir($handle) ) ) {
// Whether skip the file or directory
if ( in_array($file, $this->skip_file) ) {
continue;
}
// Recursion for sub folder
if ( is_dir($dir.'/'.$file) ) {
$margin = $level * 20;
$d_margin = $margin + 8;
echo '<p class="folder" '. "style=\"margin-left: {$margin}px\">".$file.'</p>';
if ( ! $subdir_counter = $this->calc_dir($dir.'/'.$file, $level+1) ) {
$e_margin = ( $level + 1 ) * 20;
echo '<p class="file" '. "style=\"margin-left: {$e_margin}px;\">- ". '(empty)</p>';
}
$dir_counter += $subdir_counter;
echo '<p class="folder_counter" '. "style=\"margin-left: {$d_margin}px\">There're " . $subdir_counter . ' lines in "' .$file. '" folder.</p>';
} else {
if ( $counter = $this->calc_file($dir.'/'.$file) ) {
$margin = $level * 20;
echo '<p class="file" '. "style=\"margin-left: {$margin}px;\">- ". $file .' - '. $counter . ' lines </p>';
}
$this->total_calc += $counter;
$dir_counter += $counter;
}
}
closedir($handle);
return $dir_counter;
} else {
return;
}
}
public function calc_file($filename) {
$tmp = explode('.', $filename);
if ( end($tmp) != $this->extension ) {
return false;
}
$counter = 0;
if ( is_file($filename) ) {
$handle = fopen($filename, 'rb');
while ( !feof($handle) ) {
$line = fgets($handle);
$line = trim($line);
if ( empty($line) ) {
continue;
}
foreach ( $this->skip_line as $tag) {
if ( strpos(substr(ltrim($line), 0, 2), $tag) !== false ) {
continue 2;
}
}
$counter++;
}
}
return $counter;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CodeCalculator</title>
<link rel="shortcut icon" type="image/ico" href="favicon.ico" />
<style type="text/css">
body {
background-color: #fff;
margin: 40px;
font-family: Lucida Grande, Verdana, Sans-serif;
font-size: 12px;
color: #000;
}
#content {
border: #999 1px solid;
background-color: #fff;
padding: 20px 20px 12px 20px;
}
h1 {
font-weight: normal;
font-size: 18px;
color: #990000;
margin: 0 0 14px 0;
padding: 8px;
border-bottom: 1px solid #ccc;
}
#wrap {
padding: 0 0 0 18px;
}
#total {
font-size: 20px;
color: #990000;
margin: 8px 0;
}
.folder {
font-weight: bold;
font-size: 14px;
color: #551C1C;
margin: 4px;
}
.folder_counter {
font-size: 12px;
color: #990000;
margin: 6px 4px 4px 2px;
padding: 0 0 3px 0;
border-bottom: 1px solid #ccc;
}
.file {
font-size: 12px;
color: #333;
margin: 2px;
}
footer {
margin: 0 0 0 18px;
}
</style>
</head>
<body>
<header>
<h1>CodeCalculator</h1>
</header>
<div id="wrap">
<?php
timer_start();
if ( ! isset($_GET['noskip']) ) {
echo '<a href="'.$_SERVER['PHP_SELF'].'?noskip=1">No skip</a>'."\n";
$calc = new CodeCalculator('php');
} elseif ( $_GET['noskip'] == '1' ) {
echo '<a href="'.$_SERVER['PHP_SELF'].'">Skip comments</a>'."\n";
$calc = new CodeCalculator('php', true);
}
?>
</div>
<footer>
<div id="info"><?php printf("Loaded in ".timer_stop().'s. CodeCalculator Calculate Codes.<br/>'."{$_SERVER['SERVER_SOFTWARE']}")?></div>
</footer>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment