Skip to content

Commit

Permalink
#74 PrettifyCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
amostajo committed Jan 26, 2020
1 parent 34e9c87 commit 675c8be
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/Core/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @copyright 10Quality <http://www.10quality.com>
* @license MIT
* @package WPMVC\Commands
* @version 1.1.7
* @version 1.1.9
*/
class Builder
{
Expand Down Expand Up @@ -56,15 +56,24 @@ class Builder
*/
protected $stmts = [];

/**
* Flag that indicates if build process is in debug mode or not.
* @since 1.1.9
* @var bool
*/
protected $is_debug_mode = false;

/**
* Constructor.
* @since 1.0.0
*
* @param string $filename
* @param bool $debug
*/
public function __construct($filename)
public function __construct($filename, $debug = false)
{
$this->filename = $filename;
$this->is_debug_mode = $debug;
$this->traverser = new NodeTraverser;
}

Expand All @@ -73,10 +82,11 @@ public function __construct($filename)
* @since 1.0.0
*
* @param string $filename
* @param bool $debug
*/
public static function builder($filename)
public static function builder($filename, $debug = false)
{
$builder = new self($filename);
$builder = new self($filename, $debug);
$builder->engine = new BuilderFactory;
return $builder;
}
Expand All @@ -86,10 +96,11 @@ public static function builder($filename)
* @since 1.0.0
*
* @param string $filename
* @param bool $debug
*/
public static function parser($filename)
public static function parser($filename, $debug = false)
{
$builder = new self($filename);
$builder = new self($filename, $debug);
$builder->engine = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
// Parse
$builder->parse();
Expand Down Expand Up @@ -152,7 +163,8 @@ public function build()
{
$printer = new Printer;
$this->stmts = $this->traverser->traverse($this->stmts);
//$this->debug();
if ( $this->is_debug_mode )
$this->debug();
// Save into file
file_put_contents(
$this->filename,
Expand Down
66 changes: 66 additions & 0 deletions src/PrettifyCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace WPMVC\Commands;

use Exception;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use WPMVC\Commands\Base\BaseCommand as Command;
use Ayuco\Exceptions\NoticeException;
use WPMVC\Commands\Core\Builder;

/**
* Prettifies PHP content using this package's pretty printer.
*
* @author Ale Mostajo
* @copyright 10 Quality <http://www.10quality.com>
* @license MIT
* @package WPMVC\Commands
* @version 1.1.9
*/
class PrettifyCommand extends Command
{
/**
* Command key.
* @since 1.1.9
* @var string
*/
protected $key = 'prettify';

/**
* Command description.
* @since 1.1.9
* @var string
*/
protected $description = 'Prettifies the PHP code inside the "/app" directory.';

/**
* Calls to command action.
* @since 1.1.9
*
* @param array $args Action arguments.
*/
public function call($args = [])
{
try {
$path = $this->rootPath.'/app';
if (!is_dir($path)) return;
$dir = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS);
foreach (new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::SELF_FIRST) as $filename => $item) {
if (!$item->isDir()
&& !preg_match('/app(\/|\\\)(Boot|Config)(\/|\\\)/', $filename)
&& $item->getExtension() === 'php'
) {
$builder = Builder::parser($filename);
$builder->build();
}
}
} catch (Exception $e) {
file_put_contents(
$this->rootPath.'/error_log',
$e->getMessage()
);
throw new NoticeException('Command "'.$this->key.'": Fatal error occurred.');
}
}
}
120 changes: 120 additions & 0 deletions tests/cases/PrettifyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
/**
* Tests the prettify command.
*
* @author Ale Mostajo
* @copyright 10 Quality <http://www.10quality.com>
* @license MIT
* @package WPMVC\Commands
* @version 1.1.9
*/
class PrettifyTest extends WpmvcAyucoTestCase
{
/**
* Tests path.
*/
protected $path = [
TESTING_PATH.'/app/Models',
TESTING_PATH.'/app/Classes',
TESTING_PATH.'/app/Functions',
TESTING_PATH.'/app/Boot',
];
/**
* Tests prettify on framework folders.
*/
public function testPrettifyModels()
{
// Prepare
$dir = TESTING_PATH.'/app/Models/';
$filename = TESTING_PATH.'/app/Models/Custom.php';
// Execure
if (!is_dir($dir)) mkdir($dir);
file_put_contents($filename, '<?php class Custom { function artu($artu) {$array2=[5,6,\'7\'];} }');
exec('php '.WPMVC_AYUCO.' prettify');
// Assert
$this->assertStringMatchContents('function artu( $artu )', $filename);
$this->assertStringMatchContents('$array2 = [ 5, 6, \'7\' ];', $filename);
}
/**
* Tests prettify on custom folders and files.
*/
public function testPrettifyCustomClass()
{
// Prepare
$dir = TESTING_PATH.'/app/Classes/';
$filename = TESTING_PATH.'/app/Classes/Test.php';
// Execure
if (!is_dir($dir)) mkdir($dir);
file_put_contents($filename, '<?php class Test { function artu($artu) {$array2=[5,6,\'7\'];} }');
exec('php '.WPMVC_AYUCO.' prettify');
// Assert
$this->assertStringMatchContents('function artu( $artu )', $filename);
$this->assertStringMatchContents('$array2 = [ 5, 6, \'7\' ];', $filename);
}
/**
* Tests prettify on function files.
*/
public function testPrettifyFunctionFile()
{
// Prepare
$dir = TESTING_PATH.'/app/Functions/';
$filename = TESTING_PATH.'/app/Functions/functions.php';
// Execure
if (!is_dir($dir)) mkdir($dir);
file_put_contents($filename, '<?php function artu($artu) {$array2=[5,6,\'7\'];}');
exec('php '.WPMVC_AYUCO.' prettify');
// Assert
$this->assertStringMatchContents('function artu( $artu )', $filename);
$this->assertStringMatchContents('$array2 = [ 5, 6, \'7\' ];', $filename);
}
/**
* Tests preserved configuretion files.
*/
public function testNoPrettifyConfigFiles()
{
// Prepare
$dir = TESTING_PATH.'/app/Config/';
$filename = TESTING_PATH.'/app/Config/test.php';
// Execure
if (!is_dir($dir)) mkdir($dir);
file_put_contents($filename, '<?php return [\'test\' => 123,321];}');
exec('php '.WPMVC_AYUCO.' prettify');
// Assert
$this->assertStringMatchContents('return [\'test\' => 123,321]', $filename);
$this->assertNotStringMatchContents('[ \'test\' => 123, 321 ]', $filename);
// Teardown
unlink($filename);
}
/**
* Tests preserved boot file.
*/
public function testNoPrettifyBootFile()
{
// Prepare
$dir = TESTING_PATH.'/app/Boot/';
$filename = TESTING_PATH.'/app/Boot/bootstrap.php';
// Execure
if (!is_dir($dir)) mkdir($dir);
file_put_contents($filename, '<?php return [\'test\' => 123,321];}');
exec('php '.WPMVC_AYUCO.' prettify');
// Assert
$this->assertStringMatchContents('return [\'test\' => 123,321]', $filename);
$this->assertNotStringMatchContents('[ \'test\' => 123, 321 ]', $filename);
}
/**
* Tests prettify on root file.
*/
public function testPrettifyRootFile()
{
// Prepare
$filename = TESTING_PATH.'/app/Root.php';
// Execure
file_put_contents($filename, '<?php class Root { function artu($artu) {$array2=[5,6,\'7\'];} }');
exec('php '.WPMVC_AYUCO.' prettify');
// Assert
$this->assertStringMatchContents('function artu( $artu )', $filename);
$this->assertStringMatchContents('$array2 = [ 5, 6, \'7\' ];', $filename);
// Teardown
unlink($filename);
}
}
2 changes: 2 additions & 0 deletions tests/environment/ayuco
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use WPMVC\Commands\CreateCommand;
use WPMVC\Commands\AddCommand;
use WPMVC\Commands\SetCommand;
use WPMVC\Commands\GenerateCommand;
use WPMVC\Commands\PrettifyCommand;


$ayuco = new Listener();
Expand All @@ -20,5 +21,6 @@ $ayuco->register(new CreateCommand(__DIR__));
$ayuco->register(new AddCommand(__DIR__));
$ayuco->register(new SetCommand(__DIR__));
$ayuco->register(new GenerateCommand(__DIR__));
$ayuco->register(new PrettifyCommand(__DIR__));

$ayuco->interpret();

0 comments on commit 675c8be

Please sign in to comment.