Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow generate PSR compatible code #41

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/config/CodeGeneratorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ protected function configureOptions(OptionsResolver $resolver) {
'useStatementSorting' => CodeGenerator::SORT_USESTATEMENTS_DEFAULT,
'constantSorting' => CodeGenerator::SORT_CONSTANTS_DEFAULT,
'propertySorting' => CodeGenerator::SORT_PROPERTIES_DEFAULT,
'methodSorting' => CodeGenerator::SORT_METHODS_DEFAULT
'methodSorting' => CodeGenerator::SORT_METHODS_DEFAULT,
'generatePsrCode' => false
]);

$resolver->setAllowedTypes('generateDocblock', 'bool');
$resolver->setAllowedTypes('generateEmptyDocblock', 'bool');
$resolver->setAllowedTypes('generateScalarTypeHints', 'bool');
$resolver->setAllowedTypes('generateReturnTypeHints', 'bool');
$resolver->setAllowedTypes('generatePsrCode', 'bool');
$resolver->setAllowedTypes('enableSorting', 'bool');
$resolver->setAllowedTypes('useStatementSorting', ['bool', 'string', '\Closure', 'phootwork\lang\Comparator']);
$resolver->setAllowedTypes('constantSorting', ['bool', 'string', '\Closure', 'phootwork\lang\Comparator']);
Expand Down Expand Up @@ -107,7 +109,16 @@ public function setGenerateEmptyDocblock($generate) {
public function getGenerateScalarTypeHints() {
return $this->options['generateScalarTypeHints'];
}


/**
* Returns whether PSR-code compatible will be generated
*
* @return bool `true` if they will be generated and `false` if not
*/
public function getGeneratePsrCode() {
return $this->options['generatePsrCode'];
}

/**
* Returns whether sorting is enabled
*
Expand All @@ -116,7 +127,7 @@ public function getGenerateScalarTypeHints() {
public function isSortingEnabled() {
return $this->options['enableSorting'];
}

/**
* Returns the use statement sorting
*
Expand Down Expand Up @@ -164,6 +175,16 @@ public function setGenerateScalarTypeHints($generate) {
return $this;
}

/**
* @param bool $generate `true` if they will be generated and `false` if not
* @return $this
*/
public function setGeneratePsrCode($generate) {
$this->options['generatePsrCode'] = $generate;

return $this;
}

/**
* Returns whether return type hints will be generated for method parameters (PHP 7)
*
Expand Down
9 changes: 7 additions & 2 deletions src/generator/builder/ClassBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ public function build(AbstractModel $model) {

// signature
$this->buildSignature($model);

// body
$this->writer->writeln(" {\n")->indent();
if ($this->config->getGeneratePsrCode()) {
$this->writer->writeln("\n{")->indent();
} else {
$this->writer->writeln(" {\n")->indent();
}

$this->buildTraits($model);
$this->buildConstants($model);
$this->buildProperties($model);
Expand Down
7 changes: 6 additions & 1 deletion src/generator/builder/parts/RoutineBuilderPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ protected function writeFunctionReturnType(RoutineInterface $model) {
}

protected function writeBody(RoutineInterface $model) {
$this->writer->writeln(' {')->indent();
if ($this->config->getGeneratePsrCode()) {
$this->writer->writeln("\n{")->indent();
} else {
$this->writer->writeln(" {")->indent();
}

$this->writer->writeln(trim($model->getBody()));
$this->writer->outdent()->rtrim()->writeln('}');
}
Expand Down
5 changes: 4 additions & 1 deletion tests/config/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ public function testCodeGeneratorConfigSetters() {

$config->setGenerateScalarTypeHints(true);
$this->assertTrue($config->getGenerateScalarTypeHints());


$config->setGeneratePsrCode(true);
$this->assertTrue($config->getGeneratePsrCode());

$config->setUseStatementSorting(false);
$this->assertFalse($config->getUseStatementSorting());

Expand Down
12 changes: 11 additions & 1 deletion tests/generator/ClassGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function testABClass() {
$code = $generator->generate($class);
$this->assertEquals($modelCode, $code);
}

public function testRequireTraitsClass() {
$class = PhpClass::create('RequireTraitsClass')
->addRequiredFile('FooBar.php')
Expand All @@ -123,4 +123,14 @@ public function testRequireTraitsClass() {
$this->assertEquals($this->getGeneratedContent('RequireTraitsClass.php'), $code);
}

public function testPsrClass() {
$expected = 'class MyClass' . "\n" . '{' . "\n" . '}';

$class = PhpClass::create('MyClass');
$generator = new ModelGenerator(['generatePsrCode' => true, 'generateDocblock' => false]);
$code = $generator->generate($class);

$this->assertEquals($expected, $code);
}

}
10 changes: 10 additions & 0 deletions tests/generator/MethodGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,14 @@ public function testReturnType() {
$this->assertEquals($expected, $generator->generate($method));
}

public function testPsrCode() {
$expected = "public function foo()\n{\n}\n";

$method = PhpMethod::create('foo');
$generator = new ModelGenerator(['generatePsrCode' => true, 'generateDocblock' => false]);
$code = $generator->generate($method);

$this->assertEquals($expected, $code);
}

}