Skip to content

Commit

Permalink
Merge pull request #352 from simivar/feature/with-docblocks-in-methods
Browse files Browse the repository at this point in the history
Add option to remove docblocks from getters and setters
  • Loading branch information
veewee authored Apr 30, 2021
2 parents f38ed20 + ab3ad36 commit a7e4afd
Show file tree
Hide file tree
Showing 13 changed files with 333 additions and 90 deletions.
23 changes: 19 additions & 4 deletions docs/code-generation/assemblers.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ Example output:
}
```

Generating type-hints is disabled by default, but can be enabled by passing `FluentSetterAssemblerOption` instance to the constructor with the option withTypeHints set to true.
Generating doc blocks is enabled by default, but can be disabled by passing `FluentSetterAssemblerOption` instance to the constructor with the option `withDocBlocks` set to false. This is normally used in conjunction with `withTypeHints`

Example
```php
new FluentSetterAssembler((new FluentSetterAssemblerOptions())->withTypeHints())
new FluentSetterAssembler((new FluentSetterAssemblerOptions())->withDocBlocks(false)->withTypeHints())
```

```php
Expand Down Expand Up @@ -185,7 +185,12 @@ Example output:
}
```

This assembler needs to be constructed with an instance of `GetterAssemblerOptions`
Generating doc blocks is enabled by default, but can be disabled by passing `GetterAssemblerOptions` instance to the constructor with the option `withDocBlocks` set to false. This is normally used in conjunction with `withTypeHints`

Example
```php
new GetterAssembler((new GetterAssemblerOptions())->withDocBlocks(false)->withTypeHints())
```

## InterfaceAssembler

Expand Down Expand Up @@ -375,8 +380,12 @@ Example output:
}
```

This assembler needs to be constructed with an instance of `SetterAssemblerOptions`.
Generating doc blocks is enabled by default, but can be disabled by passing `SetterAssemblerOptions` instance to the constructor with the option `withDocBlocks` set to false. This is normally used in conjunction with `withTypeHints`

Example
```php
new SetterAssembler((new SetterAssemblerOptions())->withDocBlocks(false)->withTypeHints())
```

## TraitAssembler

Expand Down Expand Up @@ -435,6 +444,12 @@ Example output:
}
```

Generating doc blocks is enabled by default, but can be disabled by passing `ImmutableSetterAssemblerOptions` instance to the constructor with the option `withDocBlocks` set to false. This is normally used in conjunction with `withTypeHints`

Example
```php
new ImmutableSetterAssembler((new ImmutableSetterAssemblerOptions())->withDocBlocks(false)->withTypeHints())
```

# Creating your own Assembler

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,33 +52,33 @@ public function assemble(ContextInterface $context)
try {
$methodName = Normalizer::generatePropertyMethod('set', $property->getName());
$class->removeMethod($methodName);
$class->addMethodFromGenerator(
MethodGenerator::fromArray([
'name' => $methodName,
'parameters' => $this->getParameter($property),
'visibility' => MethodGenerator::VISIBILITY_PUBLIC,
'body' => sprintf(
'$this->%1$s = $%1$s;%2$sreturn $this;',
$property->getName(),
$class::LINE_FEED
),
'returntype' => $this->options->useReturnType()
? $class->getNamespaceName().'\\'.$class->getName()
: null,
'docblock' => DocBlockGeneratorFactory::fromArray([
'tags' => [
[
'name' => 'param',
'description' => sprintf('%s $%s', $property->getType(), $property->getName()),
],
[
'name' => 'return',
'description' => '$this',
],

$methodGenerator = new MethodGenerator($methodName);
$methodGenerator->setParameters($this->getParameter($property));
$methodGenerator->setVisibility(MethodGenerator::VISIBILITY_PUBLIC);
$methodGenerator->setBody(sprintf(
'$this->%1$s = $%1$s;%2$sreturn $this;',
$property->getName(),
$class::LINE_FEED
));
if ($this->options->useReturnType()) {
$methodGenerator->setReturnType($class->getNamespaceName().'\\'.$class->getName());
}
if ($this->options->useDocBlocks()) {
$methodGenerator->setDocBlock(DocBlockGeneratorFactory::fromArray([
'tags' => [
[
'name' => 'param',
'description' => sprintf('%s $%s', $property->getType(), $property->getName()),
],
[
'name' => 'return',
'description' => '$this',
],
]),
])
);
],
]));
}
$class->addMethodFromGenerator($methodGenerator);
} catch (\Exception $e) {
throw AssemblerException::fromException($e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class FluentSetterAssemblerOptions
*/
private $returnType = false;

/**
* @var bool
*/
private $docBlocks = true;

/**
* @return FluentSetterAssemblerOptions
*/
Expand Down Expand Up @@ -70,4 +75,25 @@ public function useReturnType(): bool
{
return $this->returnType;
}

/**
* @param bool $withDocBlocks
*
* @return FluentSetterAssemblerOptions
*/
public function withDocBlocks(bool $withDocBlocks = true): FluentSetterAssemblerOptions
{
$new = clone $this;
$new->docBlocks = $withDocBlocks;

return $new;
}

/**
* @return bool
*/
public function useDocBlocks(): bool
{
return $this->docBlocks;
}
}
36 changes: 20 additions & 16 deletions src/Phpro/SoapClient/CodeGenerator/Assembler/GetterAssembler.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,27 @@ public function assemble(ContextInterface $context)
$prefix = $this->getPrefix($property);
$methodName = Normalizer::generatePropertyMethod($prefix, $property->getName());
$class->removeMethod($methodName);
$class->addMethodFromGenerator(
MethodGenerator::fromArray([
'name' => $methodName,
'parameters' => [],
'visibility' => MethodGenerator::VISIBILITY_PUBLIC,
'body' => sprintf('return $this->%s;', $property->getName()),
'returntype' => $this->options->useReturnType() ? $property->getCodeReturnType() : null,
'docblock' => DocBlockGeneratorFactory::fromArray([
'tags' => [
[
'name' => 'return',
'description' => $property->getType(),
],

$methodGenerator = new MethodGenerator($methodName);
$methodGenerator->setVisibility(MethodGenerator::VISIBILITY_PUBLIC);
$methodGenerator->setBody(sprintf('return $this->%s;', $property->getName()));

if ($this->options->useReturnType()) {
$methodGenerator->setReturnType($property->getCodeReturnType());
}

if ($this->options->useDocBlocks()) {
$methodGenerator->setDocBlock(DocBlockGeneratorFactory::fromArray([
'tags' => [
[
'name' => 'return',
'description' => $property->getType(),
],
]),
])
);
],
]));
}

$class->addMethodFromGenerator($methodGenerator);
} catch (\Exception $e) {
throw AssemblerException::fromException($e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class GetterAssemblerOptions
*/
private $returnType = false;

/**
* @var bool
*/
private $docBlocks = true;

/**
* @return GetterAssemblerOptions
*/
Expand Down Expand Up @@ -70,4 +75,25 @@ public function useReturnType(): bool
{
return $this->returnType;
}

/**
* @param bool $withDocBlocks
*
* @return GetterAssemblerOptions
*/
public function withDocBlocks(bool $withDocBlocks = true): GetterAssemblerOptions
{
$new = clone $this;
$new->docBlocks = $withDocBlocks;

return $new;
}

/**
* @return bool
*/
public function useDocBlocks(): bool
{
return $this->docBlocks;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,32 +66,28 @@ public function assemble(ContextInterface $context)
if ($this->options->useTypeHints()) {
$parameterOptions['type'] = $property->getType();
}
$returnType = $this->options->useReturnTypes()
? $class->getNamespaceName() . '\\' . $class->getName()
: null;
$class->addMethodFromGenerator(
MethodGenerator::fromArray(
[
'name' => $methodName,
'parameters' => [$parameterOptions],
'visibility' => MethodGenerator::VISIBILITY_PUBLIC,
'body' => implode($class::LINE_FEED, $lines),
'returntype' => $returnType,
'docblock' => DocBlockGeneratorFactory::fromArray([
'tags' => [
[
'name' => 'param',
'description' => sprintf('%s $%s', $property->getType(), $property->getName()),
],
[
'name' => 'return',
'description' => $class->getName(),
],
],
]),
]
)
);

$methodGenerator = new MethodGenerator($methodName);
$methodGenerator->setParameters([$parameterOptions]);
$methodGenerator->setBody(implode($class::LINE_FEED, $lines));
if ($this->options->useReturnTypes()) {
$methodGenerator->setReturnType($class->getNamespaceName() . '\\' . $class->getName());
}
if ($this->options->useDocBlocks()) {
$methodGenerator->setDocBlock(DocBlockGeneratorFactory::fromArray([
'tags' => [
[
'name' => 'param',
'description' => sprintf('%s $%s', $property->getType(), $property->getName()),
],
[
'name' => 'return',
'description' => $class->getName(),
],
],
]));
}
$class->addMethodFromGenerator($methodGenerator);
} catch (\Exception $e) {
throw AssemblerException::fromException($e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class ImmutableSetterAssemblerOptions
*/
private $returnTypes = false;

/**
* @var bool
*/
private $docBlocks = true;

/**
* @return ImmutableSetterAssemblerOptions
*/
Expand Down Expand Up @@ -63,4 +68,25 @@ public static function create(): ImmutableSetterAssemblerOptions
{
return new self();
}

/**
* @param bool $withDocBlocks
*
* @return ImmutableSetterAssemblerOptions
*/
public function withDocBlocks(bool $withDocBlocks = true): ImmutableSetterAssemblerOptions
{
$new = clone $this;
$new->docBlocks = $withDocBlocks;

return $new;
}

/**
* @return bool
*/
public function useDocBlocks(): bool
{
return $this->docBlocks;
}
}
35 changes: 17 additions & 18 deletions src/Phpro/SoapClient/CodeGenerator/Assembler/SetterAssembler.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,23 @@ public function assemble(ContextInterface $context)
}
$methodName = Normalizer::generatePropertyMethod('set', $property->getName());
$class->removeMethod($methodName);
$class->addMethodFromGenerator(
MethodGenerator::fromArray(
[
'name' => $methodName,
'parameters' => [$parameterOptions],
'visibility' => MethodGenerator::VISIBILITY_PUBLIC,
'body' => sprintf('$this->%1$s = $%1$s;', $property->getName()),
'docblock' => DocBlockGeneratorFactory::fromArray([
'tags' => [
[
'name' => 'param',
'description' => sprintf('%s $%s', $property->getType(), $property->getName()),
],
],
]),
]
)
);

$methodGenerator = new MethodGenerator($methodName);
$methodGenerator->setParameters([$parameterOptions]);
$methodGenerator->setVisibility(MethodGenerator::VISIBILITY_PUBLIC);
$methodGenerator->setBody(sprintf('$this->%1$s = $%1$s;', $property->getName()));
if ($this->options->useDocBlocks()) {
$methodGenerator->setDocBlock(DocBlockGeneratorFactory::fromArray([
'tags' => [
[
'name' => 'param',
'description' => sprintf('%s $%s', $property->getType(), $property->getName()),
],
],
]));
}

$class->addMethodFromGenerator($methodGenerator);
} catch (\Exception $e) {
throw AssemblerException::fromException($e);
}
Expand Down
Loading

0 comments on commit a7e4afd

Please sign in to comment.