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

Fixed ReflectionSourceStubber for unsupported values of default properties #587

Merged
merged 1 commit into from May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/SourceLocator/SourceStubber/ReflectionSourceStubber.php
Expand Up @@ -4,6 +4,7 @@

namespace Roave\BetterReflection\SourceLocator\SourceStubber;

use LogicException;
use PhpParser\Builder;
use PhpParser\Builder\Class_;
use PhpParser\Builder\Declaration;
Expand Down Expand Up @@ -270,7 +271,11 @@ private function addProperties(Declaration $classNode, CoreReflectionClass $clas
$this->addDocComment($propertyNode, $propertyReflection);

if (array_key_exists($propertyReflection->getName(), $defaultProperties)) {
$propertyNode->setDefault($defaultProperties[$propertyReflection->getName()]);
try {
$propertyNode->setDefault($defaultProperties[$propertyReflection->getName()]);
} catch (LogicException $e) {
// Unsupported value
}
}

$classNode->addStmt($propertyNode);
Expand Down
@@ -0,0 +1,8 @@
<?php

namespace Roave\BetterReflectionTest\Fixture;

class ClassForSourceStubberWithDefaultStaticProperty
{
public static $publicStaticProperty;
}
@@ -0,0 +1,8 @@
<?php

namespace Roave\BetterReflectionTest\Fixture;

class ClassForSourceStubberWithDefaultStaticProperty
{
public static $publicStaticProperty;
}
Expand Up @@ -23,9 +23,11 @@
use Roave\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
use Roave\BetterReflectionTest\BetterReflectionSingleton;
use Roave\BetterReflectionTest\Fixture\ClassForSourceStubber;
use Roave\BetterReflectionTest\Fixture\ClassForSourceStubberWithDefaultStaticProperty;
use Roave\BetterReflectionTest\Fixture\EmptyTrait;
use Roave\BetterReflectionTest\Fixture\InterfaceForSourceStubber;
use Roave\BetterReflectionTest\Fixture\TraitForSourceStubber;
use stdClass;
use Traversable;
use function array_filter;
use function array_map;
Expand Down Expand Up @@ -124,6 +126,18 @@ public function testClassWithoutNamespaceStub() : void
self::assertNull($stubData->getExtensionName());
}

public function testClassStubWithDefaultStaticPropertyWithUnsupportedValue() : void
{
require __DIR__ . '/../../Fixture/ClassForSourceStubberWithDefaultStaticProperty.php';

ClassForSourceStubberWithDefaultStaticProperty::$publicStaticProperty = new stdClass();

$stubData = $this->stubber->generateClassStub(ClassForSourceStubberWithDefaultStaticProperty::class);

self::assertNotNull($stubData);
self::assertStringEqualsFile(__DIR__ . '/../../Fixture/ClassForSourceStubberWithDefaultStaticPropertyExpected.php', $stubData->getStub());
}

public function testInterfaceStub() : void
{
require __DIR__ . '/../../Fixture/InterfaceForSourceStubber.php';
Expand Down