Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tomáš Votruba
committed
May 31, 2015
1 parent
4dbef42
commit 886d930
Showing
6 changed files
with
226 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"bootstrap": "src/Herrera/Box/Compactor/PhpNette.php", | ||
"output": "apigen.phar", | ||
"chmod": "0755", | ||
"compactors": [ | ||
"ApiGen\\Herrera\\Box\\Compactor\\PhpNette" | ||
], | ||
"compression": "GZ", | ||
"extract": false, | ||
"main": "bin/apigen", | ||
"files": [ | ||
"bin/bootstrap.php", | ||
"vendor/autoload.php", | ||
"LICENSE" | ||
], | ||
"finder": [ | ||
{ | ||
"in": "src" | ||
}, | ||
{ | ||
"name": ["*.php", "*.json", "*.latte", "*.neon", "*.css", "*.js", "*.png"], | ||
"exclude": ["test", "Test", "Tests", "tests" ], | ||
"in": [ | ||
"vendor/andrewsville", | ||
"vendor/apigen", | ||
"vendor/composer", | ||
"vendor/hamcrest", | ||
"vendor/herrera-io", | ||
"vendor/justinrainbow", | ||
"vendor/kdyby", | ||
"vendor/kukulich", | ||
"vendor/latte", | ||
"vendor/michelf", | ||
"vendor/nette", | ||
"vendor/seld", | ||
"vendor/symfony", | ||
"vendor/tracy" | ||
] | ||
} | ||
], | ||
"stub": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the ApiGen (http://apigen.org) | ||
* | ||
* For the full copyright and license information, please view | ||
* the file LICENSE that was distributed with this source code. | ||
*/ | ||
|
||
namespace ApiGen\Herrera\Box\Compactor; | ||
|
||
use Herrera\Box\Compactor\CompactorInterface; | ||
|
||
|
||
/** | ||
* Keeps Nette system annotations "method" and "return", minimizes PHP source and preserves line numbers. | ||
*/ | ||
class PhpNette implements CompactorInterface | ||
{ | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function compact($contents) | ||
{ | ||
$output = ''; | ||
foreach (token_get_all($contents) as $token) { | ||
if (is_string($token)) { | ||
$output .= $token; | ||
|
||
} elseif ($token[0] === T_COMMENT) { | ||
$output .= $this->preserveLineNumbers($token); | ||
|
||
} elseif ($this->isCommentWithoutAnnotations($token, ['@return', '@method'])) { | ||
$output .= $this->preserveLineNumbers($token); | ||
|
||
} elseif ($token[0] === T_WHITESPACE) { | ||
if (strpos($token[1], "\n") === FALSE) { | ||
$output .= ' '; | ||
|
||
} else { | ||
$output .= $this->preserveLineNumbers($token); | ||
} | ||
|
||
} else { | ||
$output .= $token[1]; | ||
} | ||
} | ||
|
||
return $output; | ||
} | ||
|
||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports($file) | ||
{ | ||
return (pathinfo($file, PATHINFO_EXTENSION) === 'php'); | ||
} | ||
|
||
|
||
/** | ||
* @return string | ||
*/ | ||
private function preserveLineNumbers(array $token) | ||
{ | ||
return str_repeat("\n", substr_count($token[1], "\n")); | ||
} | ||
|
||
|
||
/** | ||
* @return bool | ||
*/ | ||
private function isCommentWithoutAnnotations(array $token, array $annotationList) | ||
{ | ||
if ($token[0] !== T_DOC_COMMENT) { | ||
return FALSE; | ||
} | ||
foreach ($annotationList as $annotation) { | ||
if (strpos($token[1], $annotation) !== FALSE) { | ||
return FALSE; | ||
} | ||
} | ||
return TRUE; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace ApiGen\Tests\Herrera\Box\Compactor; | ||
|
||
use ApiGen\Herrera\Box\Compactor\PhpNette; | ||
use PHPUnit_Framework_TestCase; | ||
|
||
|
||
class PhpNetteTest extends PHPUnit_Framework_TestCase | ||
{ | ||
|
||
/** | ||
* @var PhpNette | ||
*/ | ||
private $phpNetteCompactor; | ||
|
||
|
||
protected function setUp() | ||
{ | ||
$this->phpNetteCompactor = new PhpNette; | ||
} | ||
|
||
|
||
public function testCompactCommon() | ||
{ | ||
$input = <<<INPUT | ||
<?php | ||
/** | ||
* Some comment | ||
*/ | ||
function getSome() | ||
INPUT; | ||
$expected = <<<COMPACT | ||
<?php | ||
function getSome() | ||
COMPACT; | ||
$this->assertSame($expected, $this->phpNetteCompactor->compact($input)); | ||
} | ||
|
||
|
||
public function testCompactMethodAndReturnAnnotations() | ||
{ | ||
$input = <<<INPUT | ||
<?php | ||
/** | ||
* @author ApiGen | ||
* @method getThis() | ||
* @return That | ||
*/ | ||
function getSomeMore() | ||
INPUT; | ||
$expected = <<<COMPACT | ||
<?php | ||
/** | ||
* @author ApiGen | ||
* @method getThis() | ||
* @return That | ||
*/ | ||
function getSomeMore() | ||
COMPACT; | ||
$this->assertSame($expected, $this->phpNetteCompactor->compact($input)); | ||
} | ||
|
||
|
||
public function testSupports() | ||
{ | ||
$this->assertTrue($this->phpNetteCompactor->supports('file.php')); | ||
$this->assertFalse($this->phpNetteCompactor->supports('file.json')); | ||
} | ||
|
||
} |