Skip to content

Instantly share code, notes, and snippets.

@twright
Forked from eleclerc/zfish.filter.php
Created September 25, 2012 22:35
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 twright/3784880 to your computer and use it in GitHub Desktop.
Save twright/3784880 to your computer and use it in GitHub Desktop.
ZendFramework-ish filter for PHP_Beautifier
<?php
/**
* A ZendFramework-ish filter for PHP_Beautifier
*
* This file have to go in:
* /path/to/pear/php/Beautifier/Filter/
*
* Use it in conjunction with Pear() and NewLines() filters:
* php_beautifier --input "/path/to/your-file.php" --filter="Pear() zfish() NewLines(after=T_DOC_COMMENT)"
*
* of course you need PEAR and PHP_Beautifier installed
*
* */
class PHP_Beautifier_Filter_zfish extends PHP_Beautifier_Filter
{
/**
* Keep blank lines
*/
function t_whitespace($sTag)
{
$match = array();
// how many new lines can we match?
preg_match_all("/(\r\n|\r|\n)/s", $sTag, $match);
if (!empty($match[1])) {
$newLines = sizeof($match[1]);
if ($newLines == 2) {
$this->oBeaut->addNewLineIndent();
}
else if ($newLines > 2) {
$this->oBeaut->addNewLineIndent();
$this->oBeaut->addNewLineIndent();
}
}
}
/**
* Keep blankline in array definitiion and align
*/
function t_comma($sTag)
{
$this->oBeaut->removeWhitespace();
$this->oBeaut->add($sTag . ' ');
if ($this->oBeaut->getControlParenthesis() == T_ARRAY) {
$matches = array();
$aToken = $this->oBeaut->getToken($this->oBeaut->iCount + 1);
if (preg_match_all("/(\r\n|\n|\r)/s", $aToken[1], $matches)) {
$this->oBeaut->incIndent();
$this->oBeaut->addNewLineIndent();
$this->oBeaut->decIndent();
}
}
}
/**
* Keep and align multiline String Concatenation
*
*/
function t_dot($sTag)
{
//check if the previous token is withspace with newline in it
$aToken = $this->oBeaut->getToken($this->oBeaut->iCount + 1);
if ($aToken[0] == 'T_WHITESPACE' && preg_match_all("/(\r\n|\n|\r)/s", $aToken[1], $match)) {
$this->oBeaut->incIndent();
$this->oBeaut->addNewLineIndent();
$this->oBeaut->decIndent();
} else {
$this->oBeaut->removeWhitespace();
$this->oBeaut->add(' ');
}
$this->oBeaut->add($sTag . ' ');
}
/**
* Add a space before and after a T_CONCAT_EQUAL
*/
function t_concat_equal($sTag)
{
$this->oBeaut->removeWhitespace();
$this->oBeaut->add(' ' . $sTag . ' ');
}
function t_foreach($sTag)
{
$this->oBeaut->add($sTag . ' ');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment