Skip to content

Commit

Permalink
added PhpTidy Lib+Vendor
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroasterisk committed Nov 15, 2013
1 parent 02b5ae2 commit f2e8666
Show file tree
Hide file tree
Showing 3 changed files with 2,497 additions and 0 deletions.
69 changes: 69 additions & 0 deletions Lib/PhpTidy.php
@@ -0,0 +1,69 @@
<?php
/**
* Convenience wrapper for phptidy.php script in app/Plugin/Icing/Vendor/phptidy.php
*
*
*/
Class PhpTidy {

/**
*
*/
static function script() {
$phptidypath = dirname(__DIR__) . DS . 'Vendor' . DS . 'phptidy.php';
if (!is_file($phptidypath)) {
throw new OutOfBoundsException('PhpTidy:setup() unable to find `Vendor/phptidy.php`');
}
return $phptidypath;
}

/**
* Tidy up some source code as a string
* - make a temp file
* - run PhpTidy::file() on the temp file
* - capture output
* - remove temp file
*
* @param string $input
* @return string $output formatted
*/
public static function string($input) {
$path = TMP . 'phptidy-' . md5($input) . '.php';
file_put_contents($path, $input);
PhpTidy::file($path);
$output = file_get_contents($path);
unlink($path);
return $output;
}

/**
* Tidy up source code as a file
* - executes the phptidy.php script via php CLI functionality
*
* @param string $path to file
* @return void;
* @throws OutOfBoundsException
*/
public static function file($path) {
if (is_array($path)) {
foreach ($path as $_path) {
PhpTidy::file($_path);
}
return;
}
if (!is_file($path)) {
throw new OutOfBoundsException('PhpTidy::file() unable to find file (should be a full path) ' . $path);
}
$phpexe = exec('which php');
if (empty($phpexe)) {
$phpexe = 'php';
}
$cmd = sprintf('%s %s replace %s',
$phpexe,
escapeshellarg(PhpTidy::script()),
escapeshellarg($path)
);
return exec($cmd);
}

}
103 changes: 103 additions & 0 deletions Test/Case/Lib/PhpTidyTest.php
@@ -0,0 +1,103 @@
<?php
App::uses('PhpTidy', 'Icing.Lib');

/**
* PhpTidy Library Test Case
*
* @package
*/


class PhpTidyTest extends CakeTestCase {
public $fixtures = array();

/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
}

/**
* tearDown method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
}

/**
*
*/
public function testPhpTidyScript() {
$expect = dirname(dirname(dirname(__DIR__))) . DS . 'Vendor' . DS . 'phptidy.php';
$this->assertEquals($expect, PhpTidy::script());
}

/**
*
*/
public function testPhpTidyString_basic() {
// pass in a sting of PHP code to tidy up
$basic = "<?php /* basic */ ?>\n";
$this->assertEquals($basic, PhpTidy::string($basic));
$noTrailingLine = '<?php /* basic */ ?>';
$this->assertEquals($basic, PhpTidy::string($noTrailingLine));
}

/**
*
*/
public function testPhpTidyString_formatting() {
// pass in a sting of PHP code to tidy up
$input = '<?php
Class Testerizer extends Object {
/**
* bad docblock
* @param string $a
* @return mixed $output
*/
public function func1 ($a = "string") {
return $output; ' . '
}
public static function func2(){}
}
?>';
$expect = '<?php' . "\n" .
'class Testerizer extends Object {' . "\n" .
' /**' . "\n" .
' * bad docblock' . "\n" .
' *' . "\n" .
' * @param string $a' . "\n" .
' * @return mixed $output' . "\n" .
' */' . "\n" .
' public function func1($a = "string") {' . "\n" .
' return $output;' . "\n" .
' }' . "\n" .
' /**' . "\n" .
' *' . "\n" .
' */' . "\n" .
' public static function func2() {}' . "\n" .
'}' . "\n" .
"?>\n";
$this->assertEquals($expect, PhpTidy::string($input));
}

/**
*
*/
public function testPhpTidyFile_good_single() {
// pass in a filename of PHP code to tidy up
$noTrailingLine = '<?php /* basic */ ?>';
$path = TMP . 'test-phptidytest-1.php';
file_put_contents($path, $noTrailingLine);
$output = PhpTidy::file($path);
$this->assertEquals('Replaced 1 files.', $output);
$result = file_get_contents($path);
$expect = "<?php /* basic */ ?>\n";
$this->assertEquals($expect, $result);
}
}

0 comments on commit f2e8666

Please sign in to comment.