Welcome Guest, Not a member yet? Register   Sign In
Filter your source code with PHP_Beautifier
#1

[eluser]dinhtrung[/eluser]
Has anyone tried PHP_Beautifier? It's a PEAR package for PHP style formatting.
Just want to share my experience working with CI on Ubuntu Linux through Bluefish editor.
First, use pear install PHP_Beautifier, then copy and paste the install line from it to install PHP_Beautifier (because this package is in beta, so you must type pear install channel://pear.php.net/PHP_Beautifier_0.14 etc, but I don't remember this line correctly. Beside, the version number can change anytime.)
In Bluefish editor, choose Edit > Preferences. Choose the branch named "External Filter" and type in PHP_Beautifier in Label and
Code:
|php_beautifier -l "ArrayNested Pear Objects ListClassFunction"|
in Command. Next is to create the Objects Filter for CodeIgniter.
By default, PHP_Beautifier always merge object methods in one line, so if we typed
Code:
$user = $this->db->where('id', 5)
                 ->get('user')
                 ->row();
The filter will produce
Code:
$user = $this->db->where('id',5)->get('user')->row();
Nothing wrong, but you'll get very very long line. So, I wrote this filter only for CodeIgniter methods chaining coding style:
Code:
<?php
/* File : /usr/share/php/PHP/Beautifier/Filters/Objects.php */
class PHP_Beautifier_Filter_Objects extends PHP_Beautifier_Filter
{
     public function t_object_operator($sTag)
    {
          $this->oBeaut->removeWhitespace();
          if ((substr($this->oBeaut->getPreviousTokenContent(), 0,1) == '$') OR (substr($this->oBeaut->getPreviousTokenContent(3), 0,1) == '$')) {
          } else {
                  $this->oBeaut->addNewlineIndent();
                  $this->oBeaut->addIndent();
          }
          $this->oBeaut->add($sTag);        
    }
}
?>
Basically, the filter will only triggered when PHP_Beautifier found an object operator (->). So I just look in the content of 3rd previous segment and 2nd previous segment for a '$' to skip process, other while just put a new line and add an indent.
Try it! It'll help alot! I swear! Smile




Theme © iAndrew 2016 - Forum software by © MyBB