Skip to content

Commit

Permalink
fix code issues, some more phpdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
gossi committed Nov 16, 2016
1 parent d075bd0 commit c77af75
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 13 deletions.
10 changes: 4 additions & 6 deletions src/generator/CodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class CodeGenerator {

/** @var CodeGeneratorConfig */
protected $config;

/** @var ModelGenerator */
protected $generator;

/**
*
Expand Down Expand Up @@ -59,12 +62,7 @@ public function getConfig() {
* @return string the generated code
*/
public function generate(GenerateableInterface $model) {
// if ($this->config->getGenerateDocblock()) {
// $model->generateDocblock();
// }

$generator = new ModelGenerator($this->config);
return $generator->generate($model);
return $this->generator->generate($model);
}

}
13 changes: 12 additions & 1 deletion src/generator/builder/AbstractBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,30 @@ public function __construct(ModelGenerator $generator) {
* @param AbstractModel $model
* @return void
*/
public abstract function build($model);
abstract public function build(AbstractModel $model);

/**
* @param AbstractModel $model
* @return void
*/
protected function generate(AbstractModel $model) {
$builder = $this->generator->getFactory()->getBuilder($model);
$builder->build($model);
}

/**
* @return void
*/
protected function ensureBlankLine() {
if (!$this->writer->endsWith("\n\n") && strlen($this->writer->rtrim()->getContent()) > 0) {
$this->writer->writeln();
}
}

/**
* @param DocblockInterface $model
* @return void
*/
protected function buildDocblock(DocblockInterface $model) {
$this->ensureBlankLine();
if ($this->config->getGenerateDocblock()) {
Expand Down
1 change: 1 addition & 0 deletions src/generator/builder/MethodBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use gossi\codegen\generator\builder\parts\RoutineBuilderPart;
use gossi\codegen\model\AbstractModel;
use gossi\codegen\model\PhpInterface;

class MethodBuilder extends AbstractBuilder {

Expand Down
6 changes: 5 additions & 1 deletion src/generator/builder/parts/RoutineBuilderPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

use TypeBuilderPart;

/**
* @param AbstractModel $model
* @return void
*/
protected abstract function generate(AbstractModel $model);

protected function writeFunctionStatement(RoutineInterface $model) {
Expand Down Expand Up @@ -45,6 +49,6 @@ protected function writeFunctionReturnType(RoutineInterface $model) {
protected function writeBody(RoutineInterface $model) {
$this->writer->writeln(' {')->indent();
$this->writer->writeln(trim($model->getBody()));
$this->writer->outdent()->rtrim()->writeln("}");
$this->writer->outdent()->rtrim()->writeln('}');
}
}
21 changes: 18 additions & 3 deletions src/generator/builder/parts/StructBuilderPart.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
<?php
namespace gossi\codegen\generator\builder\parts;

use gossi\codegen\generator\ComparatorFactory;
use gossi\codegen\model\AbstractModel;
use gossi\codegen\model\AbstractPhpStruct;
use gossi\codegen\model\ConstantsInterface;
use gossi\codegen\model\DocblockInterface;
use gossi\codegen\model\NamespaceInterface;
use gossi\codegen\model\PropertiesInterface;
use gossi\codegen\model\TraitsInterface;
use gossi\codegen\generator\ComparatorFactory;

trait StructBuilderPart {

protected abstract function ensureBlankLine();
protected abstract function generate(AbstractModel $model);
/**
* @return void
*/
abstract protected function ensureBlankLine();

/**
* @param AbstractModel $model
* @return void
*/
abstract protected function generate(AbstractModel $model);

/**
* @param DocblockInterface $model
* @return void
*/
abstract protected function buildDocblock(DocblockInterface $model);

protected function buildHeader(AbstractPhpStruct $model) {
$this->buildNamespace($model);
Expand Down
6 changes: 6 additions & 0 deletions src/generator/builder/parts/TypeBuilderPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ trait TypeBuilderPart {
'double' => 'float'
];

/**
*
* @param AbstractModel $model
* @param bool $allowed
* @return string|null
*/
private function getType(AbstractModel $model, $allowed) {
$type = $model->getType();
if (!empty($type) && strpos($type, '|') === false
Expand Down
10 changes: 9 additions & 1 deletion src/model/RoutineInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@

interface RoutineInterface {

/**
* Sets a collection of parameters
*
* Note: clears all parameters before setting the new ones
*
* @param PhpParameter[] $parameters
* @return $this
*/
public function setParameters(array $parameters);

/**
Expand Down Expand Up @@ -66,7 +74,7 @@ public function replaceParameter($position, PhpParameter $parameter);
/**
* Remove a parameter at a given position
*
* @param int|string|PhpParameter $position
* @param int|string|PhpParameter $param
* @return $this
*/
public function removeParameter($param);
Expand Down
2 changes: 1 addition & 1 deletion src/model/parts/ParametersPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function replaceParameter($position, PhpParameter $parameter) {
/**
* Remove a parameter at a given position
*
* @param int|string|PhpParameter $position
* @param int|string|PhpParameter $param
* @return $this
*/
public function removeParameter($param) {
Expand Down
67 changes: 67 additions & 0 deletions src/parser/visitor/ParserVisitorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,92 @@

interface ParserVisitorInterface {

/**
* Visit a struct
*
* @param ClassLike $node
* @return void
*/
public function visitStruct(ClassLike $node);

/**
* Visit a class
*
* @param Class_ $node
* @return void
*/
public function visitClass(Class_ $node);

/**
* Visit an interface
*
* @param Interface_ $node
* @return void
*/
public function visitInterface(Interface_ $node);

/**
* Visit a trait
*
* @param Trait_ $node
* @return void
*/
public function visitTrait(Trait_ $node);

/**
* Visit a use statement inside a struct
*
* @param TraitUse $node
* @return void
*/
public function visitTraitUse(TraitUse $node);

/**
* Visit class constants
*
* @param ClassConst $node
* @return void
*/
public function visitConstants(ClassConst $node);

/**
* Visit a constant
*
* @param Const_ $node
* @param Doc $doc
* @return void
*/
public function visitConstant(Const_ $node, Doc $doc = null);

/**
* Visit a property
*
* @param Property $node
* @return void
*/
public function visitProperty(Property $node);

/**
* Visit a namespace statement
*
* @param Namespace_ $node
* @return void
*/
public function visitNamespace(Namespace_ $node);

/**
* Visit a use statement
*
* @param UseUse $node
* @return void
*/
public function visitUseStatement(UseUse $node);

/**
* Visit a method
*
* @param ClassMethod $node
* @return void
*/
public function visitMethod(ClassMethod $node);
}
31 changes: 31 additions & 0 deletions src/parser/visitor/parts/ValueParserPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ trait ValueParserPart {
'true' => true
];

/**
* Parses the value of a node into the model
*
* @param ValueInterface $obj
* @param Node $node
* @return void
*/
private function parseValue(ValueInterface $obj, Node $node) {
$value = $node instanceof Const_ ? $node->value : $node->default;
if ($value !== null) {
Expand All @@ -31,6 +38,12 @@ private function parseValue(ValueInterface $obj, Node $node) {
}
}

/**
* Returns whether this node is a primitive value
*
* @param Node $node
* @return boolean
*/
private function isPrimitive(Node $node) {
return $node instanceof String_
|| $node instanceof LNumber
Expand All @@ -39,6 +52,12 @@ private function isPrimitive(Node $node) {
|| $this->isNull($node);
}

/**
* Returns the primitive value
*
* @param Node $node
* @return mixed
*/
private function getPrimitiveValue(Node $node) {
if ($this->isBool($node)) {
return (bool) $this->getExpression($node);
Expand All @@ -51,6 +70,12 @@ private function getPrimitiveValue(Node $node) {
return $node->value;
}

/**
* Returns whether this node is a boolean value
*
* @param Node $node
* @return boolean
*/
private function isBool(Node $node) {
if ($node instanceof ConstFetch) {
$const = $node->name->parts[0];
Expand All @@ -62,6 +87,12 @@ private function isBool(Node $node) {
}
}

/**
* Returns whether this node is a null value
*
* @param Node $node
* @return boolean
*/
private function isNull(Node $node) {
if ($node instanceof ConstFetch) {
$const = $node->name->parts[0];
Expand Down

0 comments on commit c77af75

Please sign in to comment.