Skip to content

Commit

Permalink
Merge pull request #212 from janvernieuwe/issue/210
Browse files Browse the repository at this point in the history
Add ImmutableSetterAssemblerOptions + Make all option objects optional
  • Loading branch information
janvernieuwe committed Jan 18, 2019
2 parents 5cafbda + 65d271b commit 82bb72e
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace spec\Phpro\SoapClient\CodeGenerator\Assembler;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Phpro\SoapClient\CodeGenerator\Assembler\ImmutableSetterAssemblerOptions;

/**
* Class ImmutableSetterAssemblerOptionsSpec
*/
class ImmutableSetterAssemblerOptionsSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType(ImmutableSetterAssemblerOptions::class);
}

function it_should_create_options()
{
$this::create()->shouldBeAnInstanceOf(ImmutableSetterAssemblerOptions::class);
}

function it_should_have_false_as_default()
{
$options = $this::create();
$options->useTypeHints()->shouldBe(false);
}

function it_should_set_type_hints()
{
$options = $this::create()->withTypeHints();
$options->useTypeHints()->shouldBe(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ class ConstructorAssembler implements AssemblerInterface
/**
* ConstructorAssembler constructor.
*
* @param ConstructorAssemblerOptions $options
* @param ConstructorAssemblerOptions|null $options
*/
public function __construct(ConstructorAssemblerOptions $options)
public function __construct(ConstructorAssemblerOptions $options = null)
{
$this->options = $options;
$this->options = $options ?? new ConstructorAssemblerOptions();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ class FluentSetterAssembler implements AssemblerInterface
/**
* FluentSetterAssembler constructor.
*
* @param FluentSetterAssemblerOptions $options
* @param FluentSetterAssemblerOptions|null $options
*/
public function __construct(FluentSetterAssemblerOptions $options)
public function __construct(FluentSetterAssemblerOptions $options = null)
{
$this->options = $options;
$this->options = $options ?? new FluentSetterAssemblerOptions();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ class GetterAssembler implements AssemblerInterface
/**
* GetterAssembler constructor.
*
* @param GetterAssemblerOptions $options
* @param GetterAssemblerOptions|null $options
*/
public function __construct(GetterAssemblerOptions $options)
public function __construct(GetterAssemblerOptions $options = null)
{
$this->options = $options;
$this->options = $options ?? new GetterAssemblerOptions();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@
class ImmutableSetterAssembler implements AssemblerInterface
{

/**
* @var ImmutableSetterAssemblerOptions
*/
private $options;

/**
* ImmutableSetterAssembler constructor.
*
* @param ImmutableSetterAssemblerOptions|null $options
*/
public function __construct(ImmutableSetterAssemblerOptions $options = null)
{
$this->options = $options ?? new ImmutableSetterAssemblerOptions();
}

/**
* @param ContextInterface $context
*
Expand All @@ -31,6 +46,7 @@ public function canAssemble(ContextInterface $context): bool
* Assembles pieces of code.
*
* @param ContextInterface|PropertyContext $context
*
* @throws AssemblerException
*/
public function assemble(ContextInterface $context)
Expand All @@ -46,11 +62,15 @@ public function assemble(ContextInterface $context)
'',
sprintf('return $new;'),
];
$parameterOptions = ['name' => $property->getName()];
if ($this->options->useTypeHints()) {
$parameterOptions['type'] = $property->getType();
}
$class->addMethodFromGenerator(
MethodGenerator::fromArray(
[
'name' => $methodName,
'parameters' => [$property->getName()],
'parameters' => [$parameterOptions],
'visibility' => MethodGenerator::VISIBILITY_PUBLIC,
'body' => implode($class::LINE_FEED, $lines),
'docblock' => DocBlockGenerator::fromArray(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Phpro\SoapClient\CodeGenerator\Assembler;

/**
* Class ImmutableSetterAssemblerOptions
* @package Phpro\SoapClient\CodeGenerator\Assembler
*/
class ImmutableSetterAssemblerOptions
{
/**
* @var bool
*/
private $typeHints = false;

/**
* @return ImmutableSetterAssemblerOptions
*/
public function withTypeHints(): ImmutableSetterAssemblerOptions
{
$new = clone $this;
$new->typeHints = true;

return $new;
}

/**
* @return bool
*/
public function useTypeHints(): bool
{
return $this->typeHints;
}

/**
* @return ImmutableSetterAssemblerOptions
*/
public static function create(): ImmutableSetterAssemblerOptions
{
return new self();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Phpro\SoapClient\CodeGenerator\Assembler;

use Phpro\SoapClient\CodeGenerator\Assembler\AssemblerInterface;
use Phpro\SoapClient\CodeGenerator\Context\ContextInterface;
use Phpro\SoapClient\CodeGenerator\Context\PropertyContext;
use Phpro\SoapClient\CodeGenerator\Util\Normalizer;
Expand All @@ -24,11 +23,12 @@ class SetterAssembler implements AssemblerInterface

/**
* SetterAssembler constructor.
* @param SetterAssemblerOptions $options
*
* @param SetterAssemblerOptions|null $options
*/
public function __construct(SetterAssemblerOptions $options)
public function __construct(SetterAssemblerOptions $options = null)
{
$this->options = $options;
$this->options = $options ?? new SetterAssemblerOptions();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ConstructorAssemblerTest extends TestCase
*/
function it_is_an_assembler()
{
$assembler = new ConstructorAssembler(new ConstructorAssemblerOptions());
$assembler = new ConstructorAssembler();
$this->assertInstanceOf(AssemblerInterface::class, $assembler);
}

Expand All @@ -31,7 +31,7 @@ function it_is_an_assembler()
*/
function it_can_assemble_type_context()
{
$assembler = new ConstructorAssembler(new ConstructorAssemblerOptions());
$assembler = new ConstructorAssembler();
$context = $this->createContext();
$this->assertTrue($assembler->canAssemble($context));
}
Expand All @@ -41,7 +41,7 @@ function it_can_assemble_type_context()
*/
function it_assembles_a_type()
{
$assembler = new ConstructorAssembler(new ConstructorAssemblerOptions());
$assembler = new ConstructorAssembler();
$context = $this->createContext();
$assembler->assemble($context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Phpro\SoapClient\CodeGenerator\Assembler\FluentSetterAssembler;
use Phpro\SoapClient\CodeGenerator\Assembler\FluentSetterAssemblerOptions;
use Phpro\SoapClient\CodeGenerator\Context\PropertyContext;
use Phpro\SoapClient\CodeGenerator\Context\TypeContext;
use Phpro\SoapClient\CodeGenerator\Model\Property;
use Phpro\SoapClient\CodeGenerator\Model\Type;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -36,7 +35,7 @@ function zendOlderOrEqual($version)
*/
function it_is_an_assembler()
{
$assembler = new FluentSetterAssembler(new FluentSetterAssemblerOptions());
$assembler = new FluentSetterAssembler();
$this->assertInstanceOf(AssemblerInterface::class, $assembler);
}

Expand All @@ -45,7 +44,7 @@ function it_is_an_assembler()
*/
function it_can_assemble_property_context()
{
$assembler = new FluentSetterAssembler(new FluentSetterAssemblerOptions());
$assembler = new FluentSetterAssembler();
$context = $this->createContext();
$this->assertTrue($assembler->canAssemble($context));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function zendOlderOrEqual($version)
*/
function it_is_an_assembler()
{
$assembler = new GetterAssembler(new GetterAssemblerOptions());
$assembler = new GetterAssembler();
$this->assertInstanceOf(AssemblerInterface::class, $assembler);
}

Expand All @@ -44,7 +44,7 @@ function it_is_an_assembler()
*/
function it_can_assemble_property_context()
{
$assembler = new GetterAssembler(new GetterAssemblerOptions());
$assembler = new GetterAssembler();
$context = $this->createContext();
$this->assertTrue($assembler->canAssemble($context));
}
Expand All @@ -54,7 +54,7 @@ function it_can_assemble_property_context()
*/
function it_assembles_a_property()
{
$assembler = new GetterAssembler(new GetterAssemblerOptions());
$assembler = new GetterAssembler();
$context = $this->createContext();
$assembler->assemble($context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Phpro\SoapClient\CodeGenerator\Assembler\AssemblerInterface;
use Phpro\SoapClient\CodeGenerator\Assembler\ImmutableSetterAssembler;
use Phpro\SoapClient\CodeGenerator\Assembler\ImmutableSetterAssemblerOptions;
use Phpro\SoapClient\CodeGenerator\Context\PropertyContext;
use Phpro\SoapClient\CodeGenerator\Model\Property;
use Phpro\SoapClient\CodeGenerator\Model\Type;
Expand Down Expand Up @@ -86,4 +87,39 @@ private function createContext()

return new PropertyContext($class, $type, $property);
}

/**
* @test
*/
function it_assembles_with_type_hints() {
$assembler = new ImmutableSetterAssembler((new ImmutableSetterAssemblerOptions())->withTypeHints());
$context = $this->createContext();
$assembler->assemble($context);

$code = $context->getClass()->generate();
$expected = <<<CODE
namespace MyNamespace;
class MyType
{
/**
* @param string \$prop1
* @return MyType
*/
public function withProp1(string \$prop1)
{
\$new = clone \$this;
\$new->prop1 = \$prop1;
return \$new;
}
}
CODE;

$this->assertEquals($expected, $code);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Phpro\SoapClient\CodeGenerator\Assembler\SetterAssembler;
use Phpro\SoapClient\CodeGenerator\Assembler\SetterAssemblerOptions;
use Phpro\SoapClient\CodeGenerator\Context\PropertyContext;
use Phpro\SoapClient\CodeGenerator\Context\TypeContext;
use Phpro\SoapClient\CodeGenerator\Model\Property;
use Phpro\SoapClient\CodeGenerator\Model\Type;
use PHPUnit\Framework\TestCase;
Expand All @@ -25,7 +24,7 @@ class SetterAssemblerTest extends TestCase
*/
function it_is_an_assembler()
{
$assembler = new SetterAssembler(new SetterAssemblerOptions());
$assembler = new SetterAssembler();
$this->assertInstanceOf(AssemblerInterface::class, $assembler);
}

Expand All @@ -34,7 +33,7 @@ function it_is_an_assembler()
*/
function it_can_assemble_property_context()
{
$assembler = new SetterAssembler(new SetterAssemblerOptions());
$assembler = new SetterAssembler();
$context = $this->createContext();
$this->assertTrue($assembler->canAssemble($context));
}
Expand Down Expand Up @@ -72,7 +71,7 @@ public function setProp1(string \$prop1)
}

/**
* @return TypeContext
* @return PropertyContext
*/
private function createContext()
{
Expand Down

0 comments on commit 82bb72e

Please sign in to comment.