Skip to content

Commit

Permalink
Support laminas code ^4.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lstrojny committed Jul 3, 2024
1 parent 59a5c10 commit f10e13c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"require": {
"php": "~8.1.0 || ~8.2.0 || ~8.3.0",
"azjezz/psl": "^2.1",
"laminas/laminas-code": "^4.14.0",
"laminas/laminas-code": "^4.5.0",
"php-soap/cached-engine": "~0.2",
"php-soap/engine": "^2.10.1",
"php-soap/encoding": "~0.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function assemble(ContextInterface $context)
);
}

if ($this->options->useTypeHints()) {
if ($this->options->useTypeHints() && method_exists($propertyGenerator, 'setType')) {
$propertyGenerator->setType(TypeGenerator::fromTypeString($property->getPhpType()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,39 @@ function it_is_an_assembler()
*/
function it_assembles_property_without_default_value()
{
$assembler = new PropertyAssembler(
PropertyAssemblerOptions::create()->withTypeHints(false)
);
$context = $this->createContext();
$assembler->assemble($context);
$code = $context->getClass()->generate();
$expected = <<<CODE
namespace MyNamespace;
class MyType
{
/**
* Type specific docs
*
* @var string
*/
private \$prop1;
}
CODE;

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

/**
* @test
*/
function it_assembles_property_with_type()
{
if (!method_exists(PropertyGenerator::class, 'setType')) {
$this->markTestSkipped('This test requires laminas-code >=4.6.0');
}

$assembler = new PropertyAssembler();
$context = $this->createContext();
$assembler->assemble($context);
Expand Down Expand Up @@ -64,7 +97,9 @@ class MyType
function it_assembles_property_with_default_value()
{
$assembler = new PropertyAssembler(
PropertyAssemblerOptions::create()->withOptionalValue()
PropertyAssemblerOptions::create()
->withTypeHints(false)
->withOptionalValue()
);
$context = $this->createContext();
$assembler->assemble($context);
Expand All @@ -79,7 +114,7 @@ class MyType
*
* @var null | string
*/
private ?string \$prop1 = null;
private \$prop1 = null;
}
CODE;
Expand All @@ -94,7 +129,9 @@ class MyType
function it_assembles_with_visibility_without_default_value()
{
$assembler = new PropertyAssembler(
PropertyAssemblerOptions::create()->withVisibility(PropertyGenerator::VISIBILITY_PUBLIC)
PropertyAssemblerOptions::create()
->withVisibility(PropertyGenerator::VISIBILITY_PUBLIC)
->withTypeHints(false)
);
$context = $this->createContext();
$assembler->assemble($context);
Expand All @@ -109,7 +146,7 @@ class MyType
*
* @var string
*/
public string \$prop1;
public \$prop1;
}
CODE;
Expand All @@ -123,7 +160,9 @@ class MyType
function it_assembles_without_doc_blocks()
{
$assembler = new PropertyAssembler(
PropertyAssemblerOptions::create()->withDocBlocks(false)
PropertyAssemblerOptions::create()
->withDocBlocks(false)
->withTypeHints(false)
);
$context = $this->createContext();
$assembler->assemble($context);
Expand All @@ -133,7 +172,7 @@ function it_assembles_without_doc_blocks()
class MyType
{
private string \$prop1;
private \$prop1;
}
CODE;
Expand Down Expand Up @@ -175,7 +214,10 @@ class MyType
*/
function it_assembles_a_doc_block_that_does_not_wrap()
{
$assembler = new PropertyAssembler();
$assembler = new PropertyAssembler(
PropertyAssemblerOptions::create()
->withTypeHints(false)
);
$context = $this->createContextWithLongType();

$assembler->assemble($context);
Expand All @@ -189,7 +231,7 @@ class MyType
/**
* @var \\This\\Is\\My\\Very\\Very\\Long\\Namespace\\And\\Class\\Name\\That\\Should\\Not\\Never\\Ever\\Wrap
*/
private \\This\\Is\\My\\Very\\Very\\Long\\Namespace\\And\\Class\\Name\\That\\Should\\Not\\Never\\Ever\\Wrap \$prop1;
private \$prop1;
}
CODE;
Expand All @@ -201,7 +243,9 @@ class MyType
*/
function it_assembles_properties_with_advanced_types()
{
$assembler = new PropertyAssembler();
$assembler = new PropertyAssembler(
PropertyAssemblerOptions::create()->withTypeHints(false)
);
$class = new ClassGenerator('MyType', 'MyNamespace');
$type = new Type($namespace = 'MyNamespace', 'MyType', [
$property = Property::fromMetaData(
Expand All @@ -224,7 +268,7 @@ class MyType
/**
* @var array<int<0,max>, string>
*/
private array \$prop1;
private \$prop1;
}
CODE;
Expand All @@ -243,7 +287,9 @@ function it_overwrite_props_during_assembling()
$assembler1->assemble($context);

$assembler2 = new PropertyAssembler(
PropertyAssemblerOptions::create()->withVisibility(PropertyGenerator::VISIBILITY_PUBLIC)
PropertyAssemblerOptions::create()
->withVisibility(PropertyGenerator::VISIBILITY_PUBLIC)
->withTypeHints(false)
);
$assembler2->assemble($context);

Expand All @@ -258,7 +304,7 @@ class MyType
*
* @var string
*/
public string \$prop1;
public \$prop1;
}
CODE;
Expand All @@ -271,7 +317,7 @@ class MyType
function it_assembles_property_with_null()
{
$assembler = new PropertyAssembler(
PropertyAssemblerOptions::create()
PropertyAssemblerOptions::create()->withTypeHints(false)
);
$context = $this->createContextWithNullableType();
$assembler->assemble($context);
Expand All @@ -286,7 +332,7 @@ class MyType
*
* @var null | string
*/
private ?string \$prop1 = null;
private \$prop1 = null;
}
CODE;
Expand Down

0 comments on commit f10e13c

Please sign in to comment.