Scripts
Code Cleaner
Met deze class kun je je code snel opruimen, dat betekent dus: tabs toevoegen, variabelen buiten quotes halen, elk statement op een nieuwe regel, etc =) Uitleg: Deze class werkt redelijk simpel: URL: http://www.legolasweb.nl/content/view/64/29/
code-cleaner
[code]<?php
//
// CodeCleaner class v1.1.1
// author: Stas Verberkt (Legolas)
// email: [email protected]
// web: http://www.legolasweb.nl/
//
// example:
// // Create new object
// $code = new codeCleaner();
// // Set the code to be cleaned
// $code->setCode($mycode);
// // Get the cleaned code
// $cleanedcode = $code->parse();
// // Get the errors
// $errors = $code->getErrors();
//
/**
* CodeCleaner, clean your code =)
*
* @package CodeCleaner
* @version 1.1.1
* @license GNU General Public License
* @copyright Copyright (c) Stas Verberkt (Legolas), 2001 - 2006
*/
/**
* Code Cleaner class
*
* @package CodeCleaner
*/
class codeCleaner {
/**
* @var string The code
*/
var $code = null;
/**
* @var string The parsed code
*/
var $parsedcode = null;
/**
* @var array The flags
*/
var $flags = array();
/**
* @var array The counters
*/
var $counters = array();
/**
* @var array All returned errors
*/
var $errors = array();
/**
* @var bool Whether to use phpmode (code containts php-tags)
*/
var $phpmode = false;
/**
* Constructor
*
* @param bool Whether to use phpmode (code containts php-tags)
*/
function codeCleaner($phpmode = false) {
$this->phpmode = $phpmode;
$this->resetparser();
}
/**
* Resets all flags, counters, errors and information storage
*/
function resetParser() {
$this->flags = array();
$this->flags['IN_SINGLE_QUOTES'] = false;
$this->flags['IN_DOUBLE_QUOTES'] = false;
$this->flags['IN_ONE_LINE_COMMENT'] = false;
$this->flags['IN_MULTI_LINE_COMMENT'] = false;
$this->flags['IN_SWITCH_STATEMENT'] = false;
$this->counters = array();
$this->counters['LINE_NUMBER'] = 1;
$this->counters['BRACKET_LEVEL'] = 0;
$this->counters['PARENTHESIS_LEVEL'] = 0;
$this->counters['INDENT_LEVEL'] = 0;
$this->counters['SWITCH_LEVEL'] = 0;
$this->info = array();
$this->info['BRACKETS'] = array();
$this->info['PARENTHESISSES'] = array();
$this->info['LAST_QUOTE'] = 0;
$this->errors = array();
}
/**
* Sets the code to be cleaned
*
* @param string The code
*/
function setCode($newcode) {
// Linux returns
$this->code = str_replace("\r", "\n", str_replace("\r\n", "\n", $newcode));
}
/**
* Get all errors
*
* @return array An array containing all errors
*/
function getErrors() {
return $this->errors;
}
/**
* Parses the code
*
* @return string The parsed code
*/
function parse() {
$this->parsedcode = null;
$this->resetparser();
if ($this->phpmode == true) {
$in_php = false;
}
$chars = preg_split('//', $this->code, -1, PREG_SPLIT_NO_EMPTY);
for ($i = 0; $i < count($chars); $i++) {
if ($this->phpmode == true && $in_php == false) {
if ($chars[$i] == '<' && $chars[($i + 1)] == '?') {
$chars[$i] .= '?php';
$chars[($i + 1)] = '';
if ($chars[($i + 2)] == '=') {
$chars[$i] .= "\n" . 'echo ';
$chars[($i + 2)] = '';
}
elseif ($chars[($i + 2)] . $chars[($i + 3)] . $chars[($i + 4)] != 'php') {
if ($chars[($i + 2)] != "\n") {
$chars[$i] .= "\n";
}
}
else {
$chars[($i + 2)] = '';
$chars[($i + 3)] = '';
$chars[($i + 4)] = '';
if ($chars[($i + 5)] != "\n") {
$chars[$i] .= "\n";
}
}
if (substr($this->parsedcode, -1) != "\n") {
$chars[$i] = "\n" . $chars[$i];
}
$in_php = true;
}
}
else {
switch ($chars[$i]) {
case '\'':
if (!$this->flags['IN_DOUBLE_QUOTES'] && !$this->flags['IN_ONE_LINE_COMMENT'] && !$this->flags['IN_MULTI_LINE_COMMENT']) {
$this->info['LAST_QUOTE'] = $this->counters['LINE_NUMBER'];
if ($this->flags['IN_SINGLE_QUOTES']) {
$this->flags['IN_SINGLE_QUOTES'] = false;
}
else {
$this->flags['IN_SINGLE_QUOTES'] = true;
}
}
break;
case '"':
if (!$this->flags['IN_SINGLE_QUOTES'] && !$this->flags['IN_ONE_LINE_COMMENT'] && !$this->flags['IN_MULTI_LINE_COMMENT']) {
$this->info['LAST_QUOTE'] = $this->counters['LINE_NUMBER'];
if ($this->flags['IN_DOUBLE_QUOTES']) {
$this->flags['IN_DOUBLE_QUOTES'] = false;
}
else {
$this->flags['IN_DOUBLE_QUOTES'] = true;
}
}
break;
case ';':
if (!$this->flags['IN_DOUBLE_QUOTES'] && !$this->flags['IN_SINGLE_QUOTES'] && !$this->flags['IN_ONE_LINE_COMMENT'] && !$this->flags['IN_MULTI_LINE_COMMENT']) {
if ($this->counters['PARENTHESIS_LEVEL'] > 0) {
if (!in_array($chars[($i + 1)], array("\n", "\t", ' '))) {
$chars[$i] .= ' ';
}
}
else {
if ($chars[($i + 1)] != "\n") {
$count = 1;
while (in_array($chars[($i + $count)], array("\n", "\t", ' '))) {
$count++;
}
if (!($chars[($i + $count)] == '#' || ($chars[($i + $count)] == '/' && $chars[($i + $count + 1)] == '/'))) {
//$chars[$i] .= "\n";
array_splice($chars, $i + 1, 0, array("\n"));
}
}
}
}
break;
case ',':
if (!$this->flags['IN_DOUBLE_QUOTES'] && !$this->flags['IN_SINGLE_QUOTES'] && !$this->flags['IN_ONE_LINE_COMMENT'] && !$this->flags['IN_MULTI_LINE_COMMENT']) {
if (!in_array($chars[($i + 1)], array("\n", "\t", ' '))) {
$chars[$i] .= ' ';
}
}
break;
case ' ':
if (!$this->flags['IN_DOUBLE_QUOTES'] && !$this->flags['IN_SINGLE_QUOTES'] && !$this->flags['IN_ONE_LINE_COMMENT'] && !$this->flags['IN_MULTI_LINE_COMMENT']) {
$prechar = '';
//$count = 1;
//while ($prechar == '' && $i - $count >= 0) {
// $prechar = $chars[($i - $count)];
// $count++;
//}
if (strlen($this->parsedcode) > 0) {
$prechar = substr($this->parsedcode, -1);
}
if (in_array($prechar, array("\n", "\t", ' '))) {
$chars[$i] = '';
}
}
break;
case '{':
if (!$this->flags['IN_DOUBLE_QUOTES'] && !$this->flags['IN_SINGLE_QUOTES'] && !$this->flags['IN_ONE_LINE_COMMENT'] && !$this->flags['IN_MULTI_LINE_COMMENT']) {
$this->counters['BRACKET_LEVEL']++;
$this->counters['INDENT_LEVEL']++;
$this->info['BRACKETS'][] = array('LINE' => $this->counters['LINE_NUMBER'], 'TYPE' => 'OPEN');
if ($chars[($i + 1)] != "\n") {
//$chars[$i] .= "\n";
array_splice($chars, $i + 1, 0, array("\n"));
}
}
break;
case '}':
if (!$this->flags['IN_DOUBLE_QUOTES'] && !$this->flags['IN_SINGLE_QUOTES'] && !$this->flags['IN_ONE_LINE_COMMENT'] && !$this->flags['IN_MULTI_LINE_COMMENT']) {
$this->counters['BRACKET_LEVEL']--;
$this->info['BRACKETS'][] = array('LINE' => $this->counters['LINE_NUMBER'], 'TYPE' => 'CLOSE');
if ($this->counters['BRACKET_LEVEL'] < 0) {
$this->counters['BRACKET_LEVEL'] = 0;
$this->errors[] = '_CLOSING_BRACKET_ON_LEVEL_ZERO@' . $this->counters['LINE_NUMBER'];
}
else {
$this->parsedcode = substr($this->parsedcode, 0, -1);
$this->counters['INDENT_LEVEL']--;
if ($this->flags['IN_SWITCH_STATEMENT'] && $this->counters['SWITCH_LEVEL'] == $this->counters['INDENT_LEVEL']) {
$this->parsedcode = substr($this->parsedcode, 0, -1);
$this->counters['INDENT_LEVEL']--;
}
}
if ($chars[($i + 1)] != "\n") {
//$chars[$i] .= "\n";
array_splice($chars, $i + 1, 0, array("\n"));
}
}
break;
case '(':
if (!$this->flags['IN_DOUBLE_QUOTES'] && !$this->flags['IN_SINGLE_QUOTES'] && !$this->flags['IN_ONE_LINE_COMMENT'] && !$this->flags['IN_MULTI_LINE_COMMENT']) {
$this->counters['PARENTHESIS_LEVEL']++;
$this->counters['INDENT_LEVEL']++;
$this->info['PARENTHESISSES'][] = array('LINE' => $this->counters['LINE_NUMBER'], 'TYPE' => 'OPEN');
}
break;
case ')':
if (!$this->flags['IN_DOUBLE_QUOTES'] && !$this->flags['IN_SINGLE_QUOTES'] && !$this->flags['IN_ONE_LINE_COMMENT'] && !$this->flags['IN_MULTI_LINE_COMMENT']) {
$this->counters['PARENTHESIS_LEVEL']--;
$this->info['PARENTHESISSES'][] = array('LINE' => $this->counters['LINE_NUMBER'], 'TYPE' => 'CLOSE');
if ($this->counters['PARENTHESIS_LEVEL'] < 0) {
$this->counters['PARENTHESIS_LEVEL'] = 0;
$this->errors[] = '_CLOSING_PARENTHESIS_ON_LEVEL_ZERO@' . $this->counters['LINE_NUMBER'];
}
else {
$this->counters['INDENT_LEVEL']--;
}
}
break;
case 'c':
case 'C':
if (!$this->flags['IN_DOUBLE_QUOTES'] && !$this->flags['IN_SINGLE_QUOTES'] && !$this->flags['IN_ONE_LINE_COMMENT'] && !$this->flags['IN_MULTI_LINE_COMMENT']) {
$try = strtolower($chars[$i] . $chars[($i + 1)] . $chars[($i + 2)] . $chars[($i + 3)]);
if ($try == 'case' && in_array(substr($this->parsedcode, -1), array("\t", "\n", ' ')) && in_array($chars[($i + 4)], array("\t", "\n", ' '))) {
if (!$this->flags['IN_SWITCH_STATEMENT']) {
$this->flags['IN_SWITCH_STATEMENT'] = true;
$this->counters['SWITCH_LEVEL'] = $this->counters['INDENT_LEVEL'];
$this->counters['INDENT_LEVEL']++;
}
else {
$this->parsedcode = substr($this->parsedcode, 0, -1);
}
}
}
break;
case "\n":
if (!$this->flags['IN_DOUBLE_QUOTES'] && !$this->flags['IN_SINGLE_QUOTES']) {
if ($this->flags['IN_ONE_LINE_COMMENT']) {
$this->flags['IN_ONE_LINE_COMMENT'] = false;
}
$tabs = 0;
while ($chars[($i + 1 + $tabs)] == "\t") {
$tabs++;
}
if ($tabs < $this->counters['INDENT_LEVEL']) {
for ($j = 0; $j < $this->counters['INDENT_LEVEL'] - $tabs; $j++) {
$chars[$i] .= "\t";
}
}
else if ($tabs > $this->counters['INDENT_LEVEL']) {
for ($j = 1; $j < $tabs - $this->counters['INDENT_LEVEL'] + 1; $j++) {
$chars[($i + $j)] = '';
}
}
}
else if ($this->flags['IN_DOUBLE_QUOTES']) {
$indent = null;
for ($j = 0; $j < $this->counters['INDENT_LEVEL']; $j++) {
$indent .= "\t";
}
$chars[$i] = '\n"' . "\n" . $indent . '. "';
}
else if ($this->flags['IN_SINGLE_QUOTES']) {
$indent = null;
for ($j = 0; $j < $this->counters['INDENT_LEVEL']; $j++) {
$indent .= "\t";
}
$chars[$i] = '\' . "\n"' . "\n" . $indent . '. \'';
}
$this->counters['LINE_NUMBER']++;
break;
case '\\':
if ($this->flags['IN_DOUBLE_QUOTES'] || $this->flags['IN_SINGLE_QUOTES']) {
$chars[$i] .= $chars[($i + 1)];
$chars[($i + 1)] = '';
}
break;
case '$':
if ($this->flags['IN_DOUBLE_QUOTES']) {
if ($i > 0 && $chars[($i - 1)] != '{') {
$var = null;
$charnum = 1;
$matches = array();
while (isset($chars[($i + $charnum)]) && preg_match("/([a-zA-Z_\x7f-\xff])([a-zA-Z0-9_\x7f-\xff]*)/", $var . $chars[($i + $charnum)], $matches) && strlen($matches[0]) == strlen($var . $chars[($i + $charnum)])) {
$var .= $chars[($i + $charnum)];
$charnum++;
}
for ($j = 1; $j < $charnum; $j++) {
$chars[($i + $j)] = '';
}
$chars[$i] = '" . $' . $var . ' . "';
}
else {
$var = null;
$charnum = 1;
while (isset($chars[($i + $charnum)]) && $chars[($i + $charnum)] != '}') {
$var .= $chars[($i + $charnum)];
$charnum++;
}
for ($j = 1; $j < $charnum + 1; $j++) {
$chars[($i + $j)] = '';
}
$this->parsedcode = substr($this->parsedcode, 0, -1);
$chars[$i] = '" . $' . $var . ' . "';
}
}
break;
case '#':
if (!$this->flags['IN_DOUBLE_QUOTES'] && !$this->flags['IN_SINGLE_QUOTES'] && !$this->flags['IN_ONE_LINE_COMMENT'] && !$this->flags['IN_MULTI_LINE_COMMENT']) {
$this->flags['IN_ONE_LINE_COMMENT'] = true;
$chars[$i] = '//';
}
case '/':
if (!$this->flags['IN_DOUBLE_QUOTES'] && !$this->flags['IN_SINGLE_QUOTES'] && !$this->flags['IN_ONE_LINE_COMMENT'] && !$this->flags['IN_MULTI_LINE_COMMENT']) {
if (count($chars) > $i + 1 && $chars[($i + 1)] == '/') {
$this->flags['IN_ONE_LINE_COMMENT'] = true;
}
else if (count($chars) > $i + 1 && $chars[($i + 1)] == '*') {
$this->flags['IN_MULTI_LINE_COMMENT'] = true;
}
}
case '*':
if ($this->flags['IN_MULTI_LINE_COMMENT']) {
if (count($chars) > $i + 1 && $chars[($i + 1)] == '/') {
$this->flags['IN_MULTI_LINE_COMMENT'] = false;
}
}
break;
}
if ($this->phpmode == true && count($chars) > $i + 1 && $chars[$i] . $chars[($i + 1)] == '?>') {
$chars[($i + 1)] = '';
$chars[$i] .= '>';
if (substr($this->parsedcode, -1) != "\n") {
$chars[$i] = "\n" . $chars[$i];
}
if (count($chars) > $i + 2 && $chars[($i + 2)] != "\n") {
$chars[$i] .= "\n";
}
$in_php = false;
}
}
$this->parsedcode .= $chars[$i];
}
if ($this->counters['BRACKET_LEVEL'] != 0) {
$level = 0;
for ($i = count($this->info['BRACKETS']) - 1; $i >= 0; $i--) {
if ($this->info['BRACKETS'][$i]['TYPE'] == 'OPEN') {
$level--;
}
else if ($this->info['BRACKETS'][$i]['TYPE'] == 'CLOSE') {
$level++;
}
if ($level < 0) {
$level = 0;
$this->errors[] = '_UNCLOSED_BRACKET@' .$this->info['BRACKETS'][$i]['LINE'];
}
}
}
if ($this->counters['PARENTHESIS_LEVEL'] != 0) {
$level = 0;
for ($i = count($this->info['PARENTHESISSES']) - 1; $i >= 0; $i--) {
if ($this->info['PARENTHESISSES'][$i]['TYPE'] == 'OPEN') {
$level--;
}
else if ($this->info['PARENTHESISSES'][$i]['TYPE'] == 'CLOSE') {
$level++;
}
if ($level < 0) {
$level = 0;
$this->errors[] = '_UNCLOSED_PARENTHESIS@' .$this->info['PARENTHESISSES'][$i]['LINE'];
}
}
}
if ($this->flags['IN_SINGLE_QUOTES']) {
$this->errors[] = '_UNCLOSED_SINGLE_QUOTE@' .$this->info['LAST_QUOTE'];
}
else if ($this->flags['IN_DOUBLE_QUOTES']) {
$this->errors[] = '_UNCLOSED_DOUBLE_QUOTE@' .$this->info['LAST_QUOTE'];
}
return $this->parsedcode;
}
}
?>[/code]
Reacties
0