Indenting
Posted: Wed Jul 08, 2009 2:16 am
Merely for fun and should not be taken seriously. Perhaps some cooperative improvement of this script?
Remember that this is incomplete and is based on my indenting standards.
What I am looking for is the naming of variables etc, could it be done in a "better" way if so a short description how/why. I know already that I have an awful habit of swapping naming scheme in the middle of a code.
Not indented
http://img190.imageshack.us/img190/5547/indentno.png
Indented
http://img194.imageshack.us/img194/4239/indent.png
Remember that this is incomplete and is based on my indenting standards.
What I am looking for is the naming of variables etc, could it be done in a "better" way if so a short description how/why. I know already that I have an awful habit of swapping naming scheme in the middle of a code.
Not indented
http://img190.imageshack.us/img190/5547/indentno.png
Indented
http://img194.imageshack.us/img194/4239/indent.png
Code: Select all
<?php
/*
@Author: Svante Hansson
@Usage: ?filename=thephpfile.php
@Warning; Do not use on a production server due to the ability to view any file source
*/
error_reporting();
$defaultIndent = 2; //Start with X spaces by default as seen here in the code.
$indentLength = 2; //How many spaces that is considered one 'tab'.
$currentIndent = $defaultIndent; //To be used later to tell the script how many indents to use.
$doIndent = false; //Enable it.
//Get the file contents and explode it into an array.
$text_input = file_get_contents($_GET["filename"]);
$text_array = explode("\r\n", $text_input);
if(!$doIndent)
{
highlight_string($text_input);
die();
}
//For-loop to get position in array.
for($i = 0; $i <= count($text_array)-1; $i++)
{
//Set $temporaryIndent to 2.
$temporaryIndent = 0;
//Trim the string line.
$text_array[$i] = trim($text_array[$i]);
//Get first character of the string and the last character
$firstCharacter = $text_array[$i][0];
$lastCharacter = $text_array[$i][strlen($text_array[$i])-1];
//Check for { }
switch($lastCharacter)
{
case "{":
$currentIndent += $indentLength;
$temporaryIndent = -$indentLength;
$temp1[] = $text_array[$i];
break;
case "}":
$currentIndent -= $indentLength;
$temporaryIndent = 0;
$temp2[] = $text_array[$i];
break;
}
//Check for special cases
switch($text_array[$i])
{
case "<?php":
$temporaryIndent = -$currentIndent;
$currentIndent = $defaultIndent;
break;
case chr(63).chr(62):
$temporaryIndent = -$currentIndent;
$currentIndent = $defaultIndent;
break;
}
//Causes warning if special cases is triggered, supressing it.
$text_array[$i] = @str_repeat(" ", $currentIndent+$temporaryIndent) . $text_array[$i] . "\r\n";
}
//Implode the fixed array and output it.
$text_output = implode($text_array);
echo highlight_string($text_output);
?>