Indenting

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
SvanteH
Forum Commoner
Posts: 50
Joined: Wed Jul 08, 2009 12:25 am

Indenting

Post by SvanteH »

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. :P

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);
?>
User avatar
DaveTheAve
Forum Contributor
Posts: 385
Joined: Tue Oct 03, 2006 2:25 pm
Location: 127.0.0.1
Contact:

Re: Indenting

Post by DaveTheAve »

Loving the idea because I'm always needing to indent other people code they throw at me and it's almost never indented. I also love the simplicity but as im sure you realize it's not 100% there yet.

One issue that comes to mind is actually my favorite short hand for if conditions:

Code: Select all

 
if ($do='la')
   $stephenLynch = $theBest;
else
   $Linux = $theBest;
 
See what I mean? Typically though if I do the if conditions without the brackets I do it without the else statement just the first if statement.
Post Reply