Skip to content

Instantly share code, notes, and snippets.

@combatpoodle
Last active August 29, 2015 14:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save combatpoodle/408f2656100196e73367 to your computer and use it in GitHub Desktop.
Save combatpoodle/408f2656100196e73367 to your computer and use it in GitHub Desktop.
Quick hack to doctor phpdoc tags. I recommend turning off your default Beautifier settings as well, as they mess with whitespace. You'll want to tweak the indentation and styling in order to use this effectively; I followed up with simple regex batches when using this in order to conform to CakePHP's sniffer standard. Based on solution at http:/…
<?php
// This is intended to govern fixing of stuff and clean up some weird whitespace stuff. Regexes are obviously not the best way to do that (especially given we're interfacing with beautifier...), but works OK for a quick-and-dirty solution.
// I also did a quick-and-dirty modification to the Beautifier's default whitespace filter to prevent it from executing.
require_once 'PHP/Beautifier.php';
$fileName = $argv[1];
if (!file_exists($fileName)) {
print "File $fileName does not exist";
exit(1);
}
echo "Processing file $fileName\n";
// Load the content of my source-file, with missing docblocks
$sourcecode = file_get_contents($fileName);
$oToken = new PHP_Beautifier();
// The phpDoc.filter.php file is not in the default directory,
// but in the "current" one => we need to add it to the list of
// directories that PHP_Beautifier will search in for filters
$oToken->addFilterDirectory(dirname(__FILE__));
// Adding some nice filters, to format the code
// $oToken->addFilter('ArrayNested');
// $oToken->addFilter('Lowercase');
// $oToken->addFilter('IndentStyles', array('style'=>'k&r'));
// $oToken->addFilter('DocBlock');
// Adding the phpDoc filter, asking it to add a license
// at the beginning of the file
$oToken->addFilter('phpDoc', array('license'=>'php'));
// The code is in $sourceCode
// We could also have used the setInputFile method,
// instead of having the code in a variable
$oToken->setInputString($sourcecode);
$oToken->process();
$content = $oToken->get();
$content = str_replace("\n\t\n", "\n\n", $content);
$content = preg_replace('#([;\{\}])\n+(\t*(public|private|protected|static))#s', "$1\n\n$2", $content);
$content = str_replace("\n\t\n", "\n\n", $content);
$content = preg_replace('#([;\{\}])\n+/\*#s', "$1\n\n/*", $content);
$content = str_replace("\n\t\n", "\n\n", $content);
$content = preg_replace('#\{\n\tconst#s', "{\n\n\tconst", $content);
$content = str_replace("\n\t\n", "\n\n", $content);
for ($i = 0; $i < 5; $i++) {
$content = str_replace("\n}\n\n", "\n}\n", $content);
$content = str_replace("\n\n\n", "\n\n", $content);
}
$content = str_replace("***/\n/**", "***/\n\n/**", $content);
// And here we get the result, all clean !
file_put_contents($fileName, $content);
<?php
/**
* Add automatically phpDoc tags before class, functions and interfaces declarations.
* It also adds licence (PHP 3) at the beginniing of php files.
*
* PHP version 5
*
* LICENSE: This source file is subject to version 3.0 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
*
* @category PHP
* @package PHP_Beautifier
* @subpackage Filter
* @author Fx NION <fxnion@free.fr>, Israel Shirk <israelshirk@gmail.com> (adapted to doctor partially-complete docblocks)
* @copyright 2009 Fx NION
* @link http://fxnion.free.fr
* @license http://www.php.net/license/3_0.txt PHP License 3.0
*/
class PHP_Beautifier_Filter_phpDoc extends PHP_Beautifier_Filter {
protected $aFilterTokenFunctions = array(
T_CLASS => 't_class',
T_FUNCTION => 't_function',
T_INTERFACE => 't_interface',
T_DOC_COMMENT => 't_doc_comment'
);
protected $sDescription = 'A mundane phpDoc filter';
protected $aSettings = array(
'license' => 'none'
);
public $aAllowedLicenses = array(
"php" => "php",
"bsd" => "bsd",
"gpl" => "gpl",
"pear" => "pear",
"apache" => "apache"
);
public function __construct(PHP_Beautifier $oBeaut, $aSettings = array())
{
parent::__construct($oBeaut, $aSettings);
$this->addSettingDefinition('license', 'text', 'Top php files license');
}
/**
* Add a standard php 3 licence at the begining of a php script.
* @param
* @return
*/
function addPhpdoc_license_php() {
$this->oBeaut->add("/**");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" *");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * PHP version 5");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" *");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * LICENSE: This source file is subject to version 3.0 of the PHP license");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * that is available through the world-wide-web at the following URI:");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * http://www.php.net/license/3_0.txt. If you did not receive a copy of");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * the PHP License and are unable to obtain it through the web, please");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * send a note to license@php.net so we can mail you a copy immediately.");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * @category PHP");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * @package");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * @subpackage Filter");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * @author FirstName LastName <mail>");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * @copyright 2009 FirstName LastName");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * @link");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * @license http://www.php.net/license/3_0.txt PHP License 3.0");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * @version CVS: $Id:$");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" */");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
}
/**
* Add a standard bsd licence at the begining of a php script.
* @param
* @return
*/
function addPhpdoc_license_bsd() {
$this->oBeaut->add("/** ====================================================================");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * BSD License");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * Copyright (c) <YEAR>, <OWNER>");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * All rights reserved.");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" *");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * Redistribution and use in source and binary forms, with or without ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * modification, are permitted provided that the following conditions are met:");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * - Redistributions of source code must retain the above copyright notice, ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * this list of conditions and the following disclaimer. ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * - Redistributions in binary form must reproduce the above copyright notice, ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * this list of conditions and the following disclaimer in the documentation ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * and/or other materials provided with the distribution. ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * - Neither the name of the <ORGANIZATION> nor the names of its contributors ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * may be used to endorse or promote products derived from this software ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * without specific prior written permission. ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * $Id:$");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" */");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
}
/**
* Add a standard gpl licence at the begining of a php script.
* @param
* @return
*/
function addPhpdoc_license_gpl() {
$this->oBeaut->add("/* ====================================================================");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * GNU Lesser General Public License");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * Version 2.1, February 1999");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * <one line to give the library's name and a brief idea of what it does.>");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * Copyright (C) <year> <name of author>");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" *");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * This library is free software; you can redistribute it and/or");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * modify it under the terms of the GNU Lesser General Public");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * License as published by the Free Software Foundation; either");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * version 2.1 of the License, or (at your option) any later version.");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" *");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * This library is distributed in the hope that it will be useful,");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * but WITHOUT ANY WARRANTY; without even the implied warranty of");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * Lesser General Public License for more details.");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" *");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * You should have received a copy of the GNU Lesser General Public");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * License along with this library; if not, write to the Free Software");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * $Id:$");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" */");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
}
/**
* Add a standard pear licence at the begining of a php script.
* @param
* @return
*/
function addPhpdoc_license_pear() {
$this->oBeaut->add("/**");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * Short description for file");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" *");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * Long description for file (if any)...");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" *");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * PHP versions 4 and 5");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" *");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * LICENSE: This source file is subject to version 3.0 of the PHP license");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * that is available through the world-wide-web at the following URI:");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * http://www.php.net/license/3_0.txt. If you did not receive a copy of");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * the PHP License and are unable to obtain it through the web, please");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * send a note to license@php.net so we can mail you a copy immediately.");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" *");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * @category CategoryName");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * @package PackageName");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * @author Original Author <author@example.com>");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * @author Another Author <another@example.com>");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * @copyright 1997-2005 The PHP Group");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * @license http://www.php.net/license/3_0.txt PHP License 3.0");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * @version CVS: $Id:$");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * @link http://pear.php.net/package/PackageName");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * @see NetOther, Net_Sample::Net_Sample()");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * @since File available since Release 1.2.0");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * @deprecated File deprecated in Release 2.0.0");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" */");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
}
/**
* Add a standard apache licence at the begining of a php script.
* @param
* @return
*/
function addPhpdoc_license_apache() {
$this->oBeaut->add("/* ====================================================================");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * The Apache Software License, Version 1.1");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" *");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * Copyright (c) 2000-2004 The Apache Software Foundation. All rights");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * reserved.");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" *");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * Redistribution and use in source and binary forms, with or without");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * modification, are permitted provided that the following conditions");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * are met:");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" *");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * 1. Redistributions of source code must retain the above copyright");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * notice, this list of conditions and the following disclaimer.");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" *");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * 2. Redistributions in binary form must reproduce the above copyright");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * notice, this list of conditions and the following disclaimer in");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * the documentation and/or other materials provided with the");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * distribution.");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" *");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * 3. The end-user documentation included with the redistribution,");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * if any, must include the following acknowledgment:");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * \"This product includes software developed by the");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * Apache Software Foundation (http://www.apache.org/)\".");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * Alternately, this acknowledgment may appear in the software itself,");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * if and wherever such third-party acknowledgments normally appear.");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" *");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * 4. The names \"Apache\" and \"Apache Software Foundation\" must");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * not be used to endorse or promote products derived from this");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * software without prior written permission. For written");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * permission, please contact apache@apache.org.");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" *");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * 5. Products derived from this software may not be called \"Apache\",");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * nor may \"Apache\" appear in their name, without prior written");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * permission of the Apache Software Foundation.");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" *");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * SUCH DAMAGE.");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * ====================================================================");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" *");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * This software consists of voluntary contributions made by many");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * individuals on behalf of the Apache Software Foundation. For more");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * information on the Apache Software Foundation, please see");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * <http://www.apache.org/>.");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" *");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * Portions of this software are based upon public domain software");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * originally written at the National Center for Supercomputing Applications,");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * University of Illinois, Urbana-Champaign.");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * \$Id:");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" */");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
}
/**
* Add a standard phpDoc Tag before a function declaration.
* It checks the parameters of the function and wether they have
* a default value.
*
* @param $iNext[optional] default value : 2
* @return
*/
function addPhpdoc_Header_function($iNext = 2) {
$this->oBeaut->removeWhitespace();
$sName = $this->oBeaut->getNextTokenContent($iNext);
if (!preg_match('#\w+#', $sName)) {
return;
}
$params = "";
$pt = array();
$begin = intval($iNext + 2);
while ($this->oBeaut->getNextTokenContent($begin) != ')') {
$params = $params . ' ' . $this->oBeaut->getNextTokenContent($begin);
$begin++;
}
$pt = explode(",", $params);
$pt = array_filter($pt);
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add("/**");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * [placeholder description]");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" *");
foreach($this->_getFunctionParams($iNext - 1) as $param) {
$param = $param[0];
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$line = " * @param [type] " . $param . " [placeholder]";
$this->oBeaut->add($line);
}
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * @return [type]");
// $this->oBeaut->addNewLine();
// $this->oBeaut->addIndent();
// $this->oBeaut->add(" * ");
// $this->oBeaut->addNewLine();
// $this->oBeaut->addIndent();
// $this->oBeaut->add(" * @todo Description of function " . $sName);
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" */");
}
/**
* Add a standard phpDoc Tag before a class declaration.
* @param
* @return
*/
function addPhpdoc_Header_class() {
$sName = $this->oBeaut->getNextTokenContent(1);
$this->oBeaut->addNewLine();
$this->oBeaut->add("/**");
$this->oBeaut->addNewLine();
$this->oBeaut->add(" * [$sName description]");
$this->oBeaut->addNewLine();
$this->oBeaut->add(" *");
$this->oBeaut->addNewLine();
$this->oBeaut->add(" * @author");
$this->oBeaut->addNewLine();
$this->oBeaut->add(" * @version");
$this->oBeaut->addNewLine();
$this->oBeaut->add(" * @package");
$this->oBeaut->addNewLine();
$this->oBeaut->add(" * @subpackage");
$this->oBeaut->addNewLine();
$this->oBeaut->add(" * @category");
$this->oBeaut->addNewLine();
$this->oBeaut->add(" * @link");
$this->oBeaut->addNewLine();
$this->oBeaut->add(" */");
}
/**
* Add a standard phpDoc Tag before an interface declaration.
* @param none
* @return none
*/
function addPhpdoc_Header_interface() {
$sName = $this->oBeaut->getNextTokenContent(1);
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add("/**");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * [$sName description]");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" *");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * @author ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * @version ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * @package ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * @subpackage ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * @category ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" * @link ");
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add(" */");
}
/**
* Determine if the next($iNext) token is one of the T_ACCESS family.
* @param $iNext[optional] default value : 1
* @return boolean
*/
function isNextTokenAccess($iNext = 1) {
return ($this->oBeaut->isNextTokenConstant(T_PUBLIC, $iNext) || $this->oBeaut->isNextTokenConstant(T_PRIVATE, $iNext) || $this->oBeaut->isNextTokenConstant(T_PROTECTED, $iNext) || $this->oBeaut->isNextTokenConstant(T_STATIC, $iNext) || $this->oBeaut->isNextTokenConstant(T_ABSTRACT, $iNext) || $this->oBeaut->isNextTokenConstant(T_FINAL, $iNext));
}
/**
* Determine if the previous($iPrev) token is one of the T_ACCESS family.
* @param $iPrev[optional] default value : 1
* @return boolean
*/
function isPreviousTokenAccess($iPrev = 1) {
return ($this->oBeaut->isPreviousTokenConstant(T_PUBLIC, $iPrev) || $this->oBeaut->isPreviousTokenConstant(T_PRIVATE, $iPrev) || $this->oBeaut->isPreviousTokenConstant(T_PROTECTED, $iPrev) || $this->oBeaut->isPreviousTokenConstant(T_STATIC, $iPrev) || $this->oBeaut->isPreviousTokenConstant(T_ABSTRACT, $iPrev) || $this->oBeaut->isPreviousTokenConstant(T_FINAL, $iPrev));
}
/**
* Determine if when encountering a T_OPEN_TAG token a phpDoc licence is
* to be inserted before. We check wether we are at the beginning of
* the file, and if true, we insert a phpDoc licence.
*
* @param $sTag
* @return
*/
function t_open_tag($sTag){
$this->oBeaut->add($sTag);
return;
$sLicense = strtolower($this->getSetting('license'));
if (strtolower($sLicense) == 'none') {
return PHP_Beautifier_Filter::BYPASS;
}
$this->oBeaut->add($sTag);
if (!$this->oBeaut->isPreviousTokenConstant(T_DOC_COMMENT) &&
!$this->oBeaut->isNextTokenConstant(T_DOC_COMMENT) &&
!$this->oBeaut->isNextTokenConstant(T_COMMENT) &&
$this->oBeaut->getPreviousTokenContent() == ""
)
{
if (!array_key_exists($sLicense, $this->aAllowedLicenses)) {
throw (new Exception("License " . $sLicense . " doesn't exists"));
}
$sMethod = "addPhpdoc_license_" . $sLicense;
call_user_func_array(array(
$this,
$sMethod
),null);
}
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
}
/**
* Determine if when encountering a T_ACCESS token a phpDoc Tag is
* to be inserted before. For this, we want to be on the first T_ACCESS
* without previeous phpDoc Tag before.
*
* @param $sTag
* @return
*/
function t_access($sTag) {
if (!$this->oBeaut->isPreviousTokenConstant(T_DOC_COMMENT) && !$this->isPreviousTokenAccess() && !$this->isNextTokenAccess()) {
$indent = $this->oBeaut->iIndent;
$this->oBeaut->iIndent = 0;
$this->oBeaut->sIndentChar = "\t";
if ($this->oBeaut->isNextTokenConstant(T_FUNCTION)) {
$this->addPhpdoc_Header_function();
$indent = true;
} elseif ($this->oBeaut->isNextTokenConstant(T_CLASS)) {
$this->oBeaut->iIndent = 0;
$this->addPhpdoc_Header_class();
$indent = false;
} elseif ($this->oBeaut->isNextTokenConstant(T_INTERFACE)) {
$this->oBeaut->iIndent = 0;
$this->addPhpdoc_Header_interface();
$indent = false;
} else {
$this->add($sTag);
$indent = true;
}
// $this->oBeaut->aOut = sIndentChar;
$this->oBeaut->removeWhitespace();
$this->oBeaut->iIndent = 1;
// $this->oBeaut->add('right after');
$this->oBeaut->addNewLine();
if ($indent) {
$this->oBeaut->addIndent();
}
// $this->oBeaut->add('after new line');
$this->oBeaut->add($sTag);
} elseif (!$this->oBeaut->isPreviousTokenConstant(T_DOC_COMMENT) && !$this->isPreviousTokenAccess() && $this->isNextTokenAccess()) {
$indent = $this->oBeaut->iIndent;
$this->oBeaut->iIndent = 0;
$this->oBeaut->sIndentChar = "\t";
if ($this->oBeaut->isNextTokenConstant(T_FUNCTION, 2))
$this->addPhpdoc_Header_function(3);
elseif ($this->oBeaut->isNextTokenConstant(T_CLASS, 2))
$this->addPhpdoc_Header_class();
elseif ($this->oBeaut->isNextTokenConstant(T_INTERFACE, 2))
$this->addPhpdoc_Header_interface();
else
$this->add($sTag);
// $this->oBeaut->aOut = sIndentChar;
$this->oBeaut->removeWhitespace();
$this->oBeaut->iIndent = 1;
// $this->oBeaut->add('right after');
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
// $this->oBeaut->add('after new line');
$this->oBeaut->add($sTag);
}
else {
$this->oBeaut->add($sTag);
}
}
function t_doc_comment($sTag) {
return $this->t_comment($sTag);
}
function t_comment($sTag) {
if (preg_match('#^//#', $sTag)) {
$this->oBeaut->add($sTag);
return;
}
$accessTokens = [
T_FINAL,
T_ABSTRACT,
T_PRIVATE,
T_PUBLIC,
T_PROTECTED
];
if ($this->oBeaut->isNextTokenConstant(T_FUNCTION)) {
$this->doctorPhpdoc_Header_function($sTag, 1);
} elseif ($this->oBeaut->isNextTokenConstant($accessTokens) && $this->oBeaut->isNextTokenConstant(T_FUNCTION, 2)) {
$this->doctorPhpdoc_Header_function($sTag, 2);
} elseif ($this->oBeaut->isNextTokenConstant(T_STATIC) && $this->oBeaut->isNextTokenConstant($accessTokens, 2) && $this->oBeaut->isNextTokenConstant(T_FUNCTION, 3)) {
$this->doctorPhpdoc_Header_function($sTag, 3);
} elseif ($this->oBeaut->isNextTokenConstant($accessTokens) && $this->oBeaut->isNextTokenConstant(T_STATIC, 2) && $this->oBeaut->isNextTokenConstant(T_FUNCTION, 3)) {
$this->doctorPhpdoc_Header_function($sTag, 3);
} else {
$this->oBeaut->add($sTag);
}
}
/**
* Determine if when encountering a T_CLASS token a phpDoc Tag is
* to be inserted before.For this, we ensure that a previous Token
* isn't a T_ACCESS one. In this case, the phpDoc Tag should have
* been already inserted by function t_access($sTag)
*
* @param $sTag
* @return
*/
function t_class($sTag) {
if (!$this->oBeaut->isPreviousTokenConstant(T_DOC_COMMENT) && !$this->isPreviousTokenAccess()) {
$this->addPhpdoc_Header_class();
$this->oBeaut->addNewLine();
$this->oBeaut->add($sTag);
}
else {
$this->oBeaut->add($sTag);
}
}
/**
* Determine if when encountering a T_INTERFACE token a phpDoc Tag is
* to be inserted before.For this, we ensure that a previous Token
* isn't a T_ACCESS one. In this case, the phpDoc Tag should have
* been already inserted by function t_access($sTag)
*
* @param $sTag
* @return
*/
function t_interface($sTag) {
if (!$this->oBeaut->isPreviousTokenConstant(T_DOC_COMMENT) && !$this->isPreviousTokenAccess()) {
$this->addPhpdoc_Header_interface();
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add($sTag);
}
else {
$this->oBeaut->add($sTag);
}
}
protected function _getFunctionParams($functionOffset) {
$params = "";
$functionParams = array();
$begin = intval($functionOffset + 3);
while (($this->oBeaut->getNextTokenContent($begin) != ')') || preg_match('#array\s*\($#si', $params)) {
$params = $params . ' ' . $this->oBeaut->getNextTokenContent($begin);
$begin++;
}
$functionParams = explode(",", $params);
$functionParams = array_filter($functionParams);
foreach ($functionParams as &$param) {
$param = explode('=', $param);
$param = array_map('trim', $param);
$name = explode(' ', $param[0]);
$name = array_filter($name);
$name = end($name);
$param[0] = $name;
}
return $functionParams;
}
protected function doctorPhpdoc_Header_function($commentContent, $functionOffset = 1) {
if (!empty($commentContent) && stristr($commentContent, '***')) {
$this->oBeaut->add($commentContent);
return;
}
$functionParams = $this->_getFunctionParams($functionOffset);
$lines = explode("\n", $commentContent);
// Strip out /**, */, etc.
foreach ($lines as $idx => &$line) {
$line = trim($line);
if ($line == '/**') {
unset($lines[$idx]);
}
if ($line == '**/' || $line == '*/') {
unset($lines[$idx]);
}
$line = ltrim($line, '* ');
}
// These are enumerated because I'm on a quick + dirty hack spree.
// Feel free to make nice.
$newLines = [];
$docParams = [];
$docLines = [];
$haveDoc = [];
$haveChanges = false;
reset($lines);
foreach ($lines as $idx => $line) {
if (!preg_match('#^(@\w+)#', $line, $matches)) {
$newLines[] = $line;
continue;
}
// Assumes that next lines after a PHPdoc line are continuations of the same content.
// May or may not be a good assumption.
while (is_string(current($lines)) && !empty(current($lines)) && current($lines)[0] != '@' && $idx < count($lines)) {
$line .= current($lines);
next($lines);
$idx++;
unset($lines[$idx]);
$idx++;
}
$docType = $matches[1];
$haveDoc[$docType] = True;
if ($docType == '@param') {
$line = preg_replace("#^(@param\s+(Controller|string|enum|array)\s+)(\w+)#", '$1$$3', $line);
$found = false;
if (preg_match(
'#^@param\s+' . // @param
'((\[?[\w\|]+\]?)\s+)?' . // string or [thing] or mixed|array
'([\$\#][\w\d]+|variable)' . // $thin3g or #th7ing (real life sucks) or variable
'(\.?\s+(.*))?$#', // Space and other stuff
$line,
$matches
)) {
$found = true;
$type = $matches[2];
$name = $matches[3];
$comments = isset($matches[4]) ? $matches[4] : '';
// Yep.
$name = str_replace('#', '$', $name);
} else if (preg_match('#^@param\s+(\[?\w+\]?)((\s+-\s+|\t+|\. )(.*))?#', $line, $matches)) {
$found = true;
$type = '';
$name = $matches[1];
$comments = isset($matches[4]) ? $matches[4] : '';
}
if (!$found) {
trigger_error("Can't parse docblock line $line; leaving in place.");
$newLines[] = substr($line, 1);
continue;
}
$comments = trim($comments);
$docParams[strtolower($name)] = [
'type' => $type,
'name' => $name,
'comments' => $comments
];
continue;
}
$docLines[] = $line;
}
foreach ($functionParams as $idx => $params) {
$lowerName = strtolower($params[0]);
if (isset($docParams[$lowerName]) && $docParams[$lowerName]['name'] !== $params[0]) {
$docParams[$lowerName]['name'] = $params[0];
}
}
if (!isset($haveDoc['@return'])) {
$haveChanges = true;
$docLines[] = '@return [type]';
}
if (!isset($haveDoc['@author'])) {
$haveChanges = true;
array_unshift($docLines, '@author [placeholder]');
}
reset($functionParams);
foreach ($functionParams as $param) {
$name = $param[0];
$default = isset($param[1]) ? $param[1] : null;
$lowerName = strtolower($name);
if (empty($docParams[$lowerName])) {
$docParams[$lowerName] = [
'type' => '',
'name' => $name,
'comments' => ''
];
}
}
if (count($functionParams) !== count($docParams)) {
$haveChanges = true;
}
if (end($newLines)) {
$newLines[] = '';
}
foreach ($functionParams as $functionParam) {
$lowerName = strtolower($functionParam[0]);
$param = $docParams[$lowerName];
if (empty($param['type']) || $type == 'type') {
$param['type'] = '[type]';
}
if (empty($param['comments']) || preg_match('#^\s*$#', $param['comments']) || $param['comments'] == 'placeholder') {
$param['comments'] = '[placeholder]';
}
$line = sprintf('@param %s %s %s', $param['type'], $param['name'], $param['comments']);
$newLines[] = $line;
}
foreach ($docLines as $idx => $line) {
$newLines[] = $line;
}
$lastWasBlank = false;
$newLines = array_values($newLines);
foreach ($newLines as $idx => $line) {
if ($lastWasBlank && empty(trim($line))) {
unset($newLines[$idx - 1]);
}
if (empty(trim($line))) {
$lastWasBlank = true;
} else {
$lastWasBlank = false;
}
}
foreach ($newLines as &$line) {
$line = ' * ' . $line;
$line = preg_replace('#[\t ]+$#', '', $line);
}
$this->oBeaut->removeWhitespace();
$this->oBeaut->addNewLine();
$this->oBeaut->addNewLine();
$this->oBeaut->add("/**");
$this->oBeaut->addNewLine();
$newLines = join("\n", $newLines);
$this->oBeaut->add($newLines);
$this->oBeaut->addNewLine();
$this->oBeaut->add(" */");
return;
}
/**
* Determine if when encountering a T_FUNCTION token a phpDoc Tag has
* to be inserted before.For this, we ensure that a previous Token
* isn't a T_ACCESS one. In this case, the phpDoc Tag should have
* been already inserted by function t_access($sTag)
*
* @param $sTag
* @return
*/
function t_function($sTag) {
$sName = $this->oBeaut->getNextTokenContent(1);
// Could be a closure/lambda fN
if (!preg_match('#\w+#', $sName)) {
$this->oBeaut->add($sTag);
} else if (!$this->oBeaut->isPreviousTokenConstant(T_DOC_COMMENT) && !$this->isPreviousTokenAccess()) {
$this->addPhpdoc_Header_function(1);
$this->oBeaut->addNewLine();
$this->oBeaut->addIndent();
$this->oBeaut->add($sTag);
} else {
$this->oBeaut->add($sTag);
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment