Skip to content

Commit

Permalink
Add refactoring framework and client.
Browse files Browse the repository at this point in the history
  • Loading branch information
yunosh committed Feb 27, 2017
1 parent 78f8e98 commit 893d51b
Show file tree
Hide file tree
Showing 14 changed files with 1,090 additions and 0 deletions.
13 changes: 13 additions & 0 deletions framework/Refactor/Test.php
@@ -0,0 +1,13 @@
<?php
/**
* Class phpdoc
*/
class Test
{
/**
* Constructor.
*/
function Test(Baz $bar, $foo = null)
{
}
}
26 changes: 26 additions & 0 deletions framework/Refactor/bin/horde-refactor
@@ -0,0 +1,26 @@
#!/usr/bin/env php
<?php
/**
* Copyright 2017 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (BSD). If you
* did not receive this file, see http://www.horde.org/licenses/bsd.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Refactor
*/

/**
* Script to help refactoring of PHP code.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @copyright 2017 Horde LLC
* @license http://www.horde.org/licenses/bsd BSD
* @package Refactor
*/

require_once 'Horde/Autoloader/Default.php';
Horde\Refactor\Cli::main();
24 changes: 24 additions & 0 deletions framework/Refactor/docs/Horde/Refactor/LICENSE
@@ -0,0 +1,24 @@
Copyright 2017 The Horde Project. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE HORDE PROJECT
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
94 changes: 94 additions & 0 deletions framework/Refactor/lib/Horde/Refactor/Cli.php
@@ -0,0 +1,94 @@
<?php
/**
* Copyright 2017 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (BSD). If you
* did not receive this file, see http://www.horde.org/licenses/bsd.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Refactor
*/

namespace Horde\Refactor;
use Horde\Refactor\Rule;
use Horde_Argv_Parser;
use Horde_Argv_Option;

/**
* Command line tool for refactoring of PHP code.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @copyright 2017 Horde LLC
* @license http://www.horde.org/licenses/bsd BSD
* @package Refactor
*/
class Cli
{
/**
* The main entry point for the application.
*/
public static function main()
{
$parser = new Horde_Argv_Parser(
array('usage' => '%prog [OPTIONS] RefactoringFile.php RefactoringClass ...')
);
$parser->addOptions(
array(
new Horde_Argv_Option(
'-f',
'--file',
array(
'action' => 'store',
'help' => 'File to be refactored',
)
),
new Horde_Argv_Option(
'-d',
'--directory',
array(
'action' => 'store',
'help' => 'Directory to be recursively refactored',
)
),
new Horde_Argv_Option(
'-u',
'--update',
array(
'action' => 'store_true',
'help' => 'Overwrite the refatored files',
)
),
)
);
list($options, $arguments) = $parser->parseArgs();

if ((!$options->file && !$options->directory) ||
!$arguments ||
count($arguments) % 2 == 1) {
$parser->printHelp();
return;
}

if (!$options->update) {
$renderer = new \Horde_Text_Diff_Renderer_Unified();
}
for ($i = 0; $i < count($arguments); $i += 2) {
require $arguments[$i];
$class = 'Horde\\Refactor\\Rule\\' . $arguments[$i + 1];
$rule = new $class($options->file);
$rule->run();
if ($options->update) {
echo $rule->dump();
} else {
$diff = new \Horde_Text_Diff(
'auto',
array(file($options->file), explode("\n", $rule->dump()))
);
echo $renderer->render($diff);
}
}
}
}
48 changes: 48 additions & 0 deletions framework/Refactor/lib/Horde/Refactor/Exception/NotFound.php
@@ -0,0 +1,48 @@
<?php
/**
* Copyright 2017 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (BSD). If you
* did not receive this file, see http://www.horde.org/licenses/bsd.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Refactor
*/

namespace Horde\Refactor\Exception;
use Horde\Refactor\Translation;

/**
* Exception thrown if an expected token wasn't found.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @copyright 2017 Horde LLC
* @license http://www.horde.org/licenses/bsd BSD
* @package Refactor
*/
class NotFound extends \Horde_Exception_NotFound
{
/**
* Constructor.
*
* @param integer|string $token The expected token.
*/
public function __construct($token)
{
if (is_array($token)) {
$token = $token[0];
}
if (is_int($token)) {
$name = token_name($token);
} else {
$name = $token;
}
$message = sprintf(
Translation::t("Token \"%s\" Not Found"), $name
);
parent::__construct($message, $token);
}
}
@@ -0,0 +1,48 @@
<?php
/**
* Copyright 2017 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (BSD). If you
* did not receive this file, see http://www.horde.org/licenses/bsd.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Refactor
*/

namespace Horde\Refactor\Exception;
use Horde\Refactor\Translation;

/**
* Exception thrown if an unexpected token was found.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @copyright 2017 Horde LLC
* @license http://www.horde.org/licenses/bsd BSD
* @package Refactor
*/
class UnexpectedToken extends \Horde_Exception
{
/**
* Constructor.
*
* @param integer|string $token The unexpected token.
*/
public function __construct($token)
{
if (is_array($token)) {
$token = $token[0];
}
if (is_int($token)) {
$name = token_name($token);
} else {
$name = $token;
}
$message = sprintf(
Translation::t("Unexpected Token \"%s\""), $name
);
parent::__construct($message, $token);
}
}
53 changes: 53 additions & 0 deletions framework/Refactor/lib/Horde/Refactor/Regexp.php
@@ -0,0 +1,53 @@
<?php
/**
* Copyright 2017 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (BSD). If you
* did not receive this file, see http://www.horde.org/licenses/bsd.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Refactor
*/

namespace Horde\Refactor;

/**
* Class for value objects representing a regular expression.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @copyright 2017 Horde LLC
* @license http://www.horde.org/licenses/bsd BSD
* @package Refactor
*/
class Regexp
{
/**
* Processed regular expression string.
*
* @var string
*/
protected $_regexp;

/**
* Constructor.
*
* @param string $regexp A regular expression, without markers.
*/
public function __construct($regexp)
{
$this->_regexp = '/' . preg_quote($regexp, '/') . '/';
}

/**
* Returns the string representation, including markers.
*
* @return string String representation of this regular expression.
*/
public function __toString()
{
return $this->_regexp;
}
}
63 changes: 63 additions & 0 deletions framework/Refactor/lib/Horde/Refactor/Rule.php
@@ -0,0 +1,63 @@
<?php
/**
* Copyright 2017 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (BSD). If you
* did not receive this file, see http://www.horde.org/licenses/bsd.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Refactor
*/

namespace Horde\Refactor;

/**
* This class represents both a file being refactored and a refactoring rule
* applied to this file.
*
* Extend this class to implement actual refactorings.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @copyright 2017 Horde LLC
* @license http://www.horde.org/licenses/bsd BSD
* @package Refactor
*/
abstract class Rule
{
/**
* Current list of tokens.
*
* @var Horde\Refactor\Tokens
*/
protected $_tokens;

/**
* Constructor.
*
* @param string $file Name of the file to parse and refactor.
*/
public function __construct($file)
{
$this->_tokens = new Tokens(
token_get_all(file_get_contents($file))
);
}

/**
* Applies the actual refactoring to the tokenized code.
*/
abstract public function run();

/**
* Returns the file code in its current state.
*
* @return string The file code.
*/
public function dump()
{
return (string)$this->_tokens;
}
}

0 comments on commit 893d51b

Please sign in to comment.