Skip to content

Commit

Permalink
Merge pull request #2 from PruneMazui/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
PruneMazui committed Nov 14, 2017
2 parents 34782f8 + e5d3289 commit a804ba4
Show file tree
Hide file tree
Showing 21 changed files with 444 additions and 49 deletions.
2 changes: 2 additions & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage_clover: report/clover.xml
json_path: report/coveralls-upload.json
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ __zephir_ide_helper.php
/report
/.vagrant
/develop
/tests/files/output.php
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: php

php:
- 7.0
- 7.1
- 7.2

before_script:
- curl -s http://getcomposer.org/installer | php
- php composer.phar install

after_success:
- vendor/bin/coveralls -v
8 changes: 2 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@
"php": ">=7.0"
},
"require-dev": {
"phpunit/phpunit": "^6.4"
"phpunit/phpunit": "^6.4",
"php-coveralls/php-coveralls": "^1.0"
},
"autoload": {
"psr-4": {
"PruneMazui\\ZephirIdeHelper\\" : "src"
}
},
"scripts": {
"pre-update-cmd": [
"php -r \"if (!function_exists('zephir_parse_file')) {throw new \\Exception();}\""
]
}
}
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</filter>

<logging>
<!-- <log type="coverage-clover" target="report/clover.xml"/> -->
<log type="coverage-clover" target="./report/clover.xml"/>
<!-- <log type="coverage-html" target="./report" lowUpperBound="35" highLowerBound="70"/> -->
</logging>

Expand Down
Empty file added readme.md
Empty file.
57 changes: 40 additions & 17 deletions src/CommandRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class CommandRunner

/**
* Handle operation
*
* @codeCoverageIgnore
* @return void
*/
public static function exec(): bool
Expand All @@ -24,6 +26,10 @@ public static function exec(): bool

public function run(array $arg): bool
{
if (count($arg) == 0) {
throw new \RuntimeException('illegal argument.');
}

$this->executedScriptName = array_shift($arg);

$option = null;
Expand Down Expand Up @@ -68,39 +74,56 @@ public function run(array $arg): bool
return false;
}

$parser = new Parser();
try {
$parser = new Parser();

$parser->add($this->target);
$parser->add($this->target);

if (! count($parser->getFiles())) {
$this->showHelp('Target file is not found.');
return false;
}
if (! count($parser->getFiles())) {
$this->showHelp('Target file is not found.');
return false;
}

$definition = new Definition();
foreach ($parser->getParseResultGenerator() as $result) {
$definition->reflectParse($result);
}
$definition = new Definition();
foreach ($parser->getParseResultGenerator() as $result) {
$definition->reflectParse($result);
}

if (file_put_contents($this->file, $definition->encode())) {
$this->notify("The php file generated. \n" . realpath($this->file));
return true;
}

if (file_put_contents($this->file, $definition->encode())) {
$this->notify("Failed to write php file.");
return false;

return true;
}
} catch (DefinitionException $x) {
$this->notify("Parse error occured. " . $ex->getMessage());
return false;

echo "Failed to write php file.";
return false;
} catch (\Exception $ex) {
$this->notify("Fatal error occured. " . $ex->getMessage());
return false;
}
}

private function showHelp(string $prefix = '')
{
$content = $prefix . <<< EOC
Usage: {$this->executedScriptName} [-option] target
Specify the target file(.zep) or directory(recursive analayze the file) to be analyzed
Specify the target file(.zep) or
directory(recursive analayze the file) to be analyzed
-f, --file Export php file name. (Default: __zephir_ide_helper.php)
EOC;

echo trim($content);
$this->notify(trim($content));
}

private function notify(string $content)
{
echo $content . "\n";
}
}
9 changes: 8 additions & 1 deletion src/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use PruneMazui\ZephirIdeHelper\Element\NamespaceElement;
use PruneMazui\ZephirIdeHelper\Element\UseElement;
use PruneMazui\ZephirIdeHelper\Element\ClassElement;
use PruneMazui\ZephirIdeHelper\Element\InterfaceElement;

class Definition implements EncodableInterface
{
Expand Down Expand Up @@ -55,6 +56,12 @@ public function reflectParse(array $parse_result): Definition

case ClassElement::TYPE:
$namespace->addClass(ClassElement::factory($row, $comment));
$comment = '';
break;

case InterfaceElement::TYPE:
$namespace->addInterface(InterfaceElement::factory($row, $comment));
$comment = '';
break;

case self::TYPE_NEXT_CLASS_COMMENT:
Expand All @@ -77,7 +84,7 @@ public function encode(): string
$content = "<?php\n";

foreach ($this->namespaces as $namespace) {
$content .= $namespace->encode();
$content .= $namespace->encode() . "\n";
}

return $content;
Expand Down
5 changes: 5 additions & 0 deletions src/Element/ClassElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ public static function factory(array $params, string $comment): self
throw new DefinitionException('class name is required.');
}

$type = $params['type'] ?? '';
if ($type !== self::TYPE) {
throw new DefinitionException('Not match type ' . self::TYPE . ' AND ' . $type . '.');
}

$ret->isAbstract = $params['abstract'] ?? false;
$ret->isFinal = $params['final'] ?? false;

Expand Down
130 changes: 130 additions & 0 deletions src/Element/InterfaceElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php
namespace PruneMazui\ZephirIdeHelper\Element;

use PruneMazui\ZephirIdeHelper\EncodableInterface;
use PruneMazui\ZephirIdeHelper\Util;

class InterfaceElement extends AbstractNamedElement implements EncodableInterface, PHPDocSupportInterface
{
use TraitPHPDocGenerator;

const TYPE = 'interface';

/**
* @var string
*/
private $comment = '';

/**
* @var MethodElement[]
*/
private $methods = [];

/**
* @return string
*/
public function getComment(): string
{
return $this->comment;
}

/**
* {@inheritDoc}
* @see \PruneMazui\ZephirIdeHelper\Element\PHPDocSupportInterface::isDeprecated()
*/
public function isDeprecated(): bool
{
return false;
}

/**
* {@inheritDoc}
* @see \PruneMazui\ZephirIdeHelper\Element\PHPDocSupportInterface::isStatic()
*/
public function isStatic(): bool
{
return false;
}

/**
* {@inheritDoc}
* @see \PruneMazui\ZephirIdeHelper\Element\PHPDocSupportInterface::isFinal()
*/
public function isFinal(): bool
{
return false;
}

/**
* {@inheritDoc}
* @see \PruneMazui\ZephirIdeHelper\Element\PHPDocSupportInterface::isAbstract()
*/
public function isAbstract(): bool
{
return false;
}

/**
* @return \PruneMazui\ZephirIdeHelper\Element\MethodElement[]
*/
public function getMethods(): array
{
return $this->methods;
}

/**
* @param array $params
* @param string $comment
* @throws DefinitionException
* @return self
*/
public static function factory(array $params, string $comment): self
{
$ret = new self();

$ret->comment = $comment;

$ret->name = $params['name'] ?? '';
if (! strlen($ret->name)) {
throw new DefinitionException('class name is required.');
}

$type = $params['type'] ?? '';
if ($type !== self::TYPE) {
throw new DefinitionException('Not match type ' . self::TYPE . ' AND ' . $type . '.');
}

// methods
$methods = $params['definition']['methods'] ?? [];
foreach ($methods as $method) {
$ret->methods[] = MethodElement::factory($method, true);
}

return $ret;
}

/**
* {@inheritDoc}
* @see \PruneMazui\ZephirIdeHelper\EncodableInterface::encode()
*/
public function encode(): string
{
$content = $this->generatorPHPDoc($this);

if (strlen($content)) {
$content .= "\n";
}

$content .= 'interface ' . $this->getName() . ' ';

$content .= "\n{\n";

foreach ($this->methods as $method) {
$content .= Util::indent($method->encode() . "\n");
}

$content .= "}\n";

return $content;
}
}
22 changes: 16 additions & 6 deletions src/Element/MethodElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ class MethodElement extends AbstractNamedElement implements EncodableInterface,
*/
private $comment = '';

/**
* @var bool
*/
private $belongInterface = false;

/**
* @return bool
*/
Expand Down Expand Up @@ -112,10 +117,11 @@ public function getComment(): string

/**
* @param array $params
* @param bool optional $belong_interface
* @throws DefinitionException
* @return self
*/
public static function factory(array $params): self
public static function factory(array $params, bool $belong_interface = false): self
{
$ret = new self();

Expand All @@ -129,11 +135,15 @@ public static function factory(array $params): self
throw new DefinitionException('Not match type ' . self::TYPE . ' AND ' . $type . '.');
}

$ret->belongInterface = $belong_interface;

$visibility = $params['visibility'] ?? [];
$ret->isPublic = in_array('public', $visibility);
$ret->isStatic = in_array('static', $visibility);
$ret->isFinal = in_array('final', $visibility);
$ret->isAbstract = in_array('abstract', $visibility);
if (! $belong_interface) {
$ret->isPublic = in_array('public', $visibility);
$ret->isStatic = in_array('static', $visibility);
$ret->isFinal = in_array('final', $visibility);
$ret->isAbstract = in_array('abstract', $visibility);
}
$ret->isDeprecated = in_array('deprecated', $visibility);

$parameters = $params['parameters'] ?? [];
Expand Down Expand Up @@ -234,7 +244,7 @@ public function encode(): string
$argment[] = $argument->encode();
}
$content .= implode(', ', $argment);
$content .= ")\n{" . "}\n";
$content .= $this->isAbstract || $this->belongInterface ? ");\n" : ")\n{" . "}\n";

return $content;
}
Expand Down

0 comments on commit a804ba4

Please sign in to comment.