Skip to content

Commit

Permalink
fix: consider missing indices in array
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Rieschl <thomas@trinet.at>
  • Loading branch information
rieschl committed Apr 23, 2021
1 parent 67dc6c0 commit fa9d202
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Generator/PropertyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static function fromReflection(PropertyReflection $reflectionProperty)

$allDefaultProperties = $reflectionProperty->getDeclaringClass()->getDefaultProperties();

$defaultValue = $allDefaultProperties[$reflectionProperty->getName()];
$defaultValue = $allDefaultProperties[$reflectionProperty->getName()] ?? null;
$property->setDefaultValue($defaultValue);
if ($defaultValue === null) {
$property->omitDefaultValue = true;
Expand Down
12 changes: 12 additions & 0 deletions test/Generator/PropertyGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Laminas\Code\Generator\PropertyValueGenerator;
use Laminas\Code\Generator\ValueGenerator;
use Laminas\Code\Reflection\ClassReflection;
use Laminas\Code\Reflection\PropertyReflection;
use LaminasTest\Code\Generator\TestAsset\ClassWithTypedProperty;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;
use stdClass;
Expand Down Expand Up @@ -324,4 +326,14 @@ public function testFromReflectionOmitsDefaultValueIfItIsNull(): void

$this->assertSame(' public static $fooStaticProperty;', $code);
}

public function testFromReflectionOmitsTypeHintInTypedProperty(): void
{
$reflectionProperty = new PropertyReflection(ClassWithTypedProperty::class, 'typedProperty');

$generator = PropertyGenerator::fromReflection($reflectionProperty);
$code = $generator->generate();

self::assertSame(' public $typedProperty;', $code);
}
}
10 changes: 10 additions & 0 deletions test/Generator/TestAsset/ClassWithTypedProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace LaminasTest\Code\Generator\TestAsset;

final class ClassWithTypedProperty
{
public string $typedProperty;
}

0 comments on commit fa9d202

Please sign in to comment.