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

Nullable flag on constructor property promotion, closes #183 #184

Open
wants to merge 1 commit into
base: 4.12.x
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions src/Generator/ParameterGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ public function getType()
: null;
}

/**
* @return bool
*/
public function getNullable()
{
return $this->type && $this->type->getNullable();
}

/**
* @param string $name
* @return ParameterGenerator
Expand Down
4 changes: 4 additions & 0 deletions src/Generator/PromotedParameterGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ public static function fromParameterGeneratorWithVisibility(ParameterGenerator $
);
}

if ($type && $generator->getNullable()) {
$type = '?' . $type;
}

return new self(
$name,
$type,
Expand Down
8 changes: 8 additions & 0 deletions src/Generator/TypeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ public function __toString(): string
return $this->type->toString();
}

/**
* @return bool Nullable flag
*/
public function getNullable()
{
return $this->nullable;
}

/**
* @return bool[]|string[] ordered tuple, first key represents whether the type is nullable, second is the
* trimmed string
Expand Down
25 changes: 25 additions & 0 deletions test/Generator/PromotedParameterGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace LaminasTest\Code\Generator;

use Laminas\Code\Generator\PromotedParameterGenerator;
use Laminas\Code\Reflection\ParameterReflection;
use LaminasTest\Code\TestAsset\ClassWithPromotedProperties;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;

#[Group('Laminas_Code_Generator')]
#[Group('Laminas_Code_Generator_Php')]
class PromotedParameterGeneratorTest extends TestCase
{
public function testNullablePromotedProperty(): void
{
$parameterReflection = new ParameterReflection([ClassWithPromotedProperties::class, '__construct'], 0);

$generator = PromotedParameterGenerator::fromReflection($parameterReflection);

$this->assertSame('protected ?int $nullable', $generator->generate());
}
}
18 changes: 18 additions & 0 deletions test/TestAsset/ClassWithPromotedProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace LaminasTest\Code\TestAsset;

/**
* Class with a promoted constructor properties
*
* @license MIT
*/
class ClassWithPromotedProperties
{
public function __construct(
protected ?int $nullable
) {
}
}