Skip to content
Permalink
Browse files

added Reflection::getPropertyType() (for PHP 7.4)

  • Loading branch information...
dg committed Mar 15, 2019
1 parent f9b35f7 commit 574e1f3baa2f62698370469c9f5cebbfcc0bd922
Showing with 54 additions and 0 deletions.
  1. +8 −0 src/Utils/Reflection.php
  2. +46 −0 tests/Utils/Reflection.getPropertyType.phptx
@@ -47,6 +47,14 @@ public static function getParameterType(\ReflectionParameter $param): ?string
}
public static function getPropertyType(\ReflectionProperty $prop): ?string
{
return PHP_VERSION_ID >= 70400 && $prop->hasType()
? self::normalizeType($prop->getType()->getName(), $prop)
: null;
}
private static function normalizeType(string $type, $reflection): string
{
$lower = strtolower($type);
@@ -0,0 +1,46 @@
<?php

/**
* Test: Nette\Utils\Reflection::getPropertyType
* @phpversion 7.4
*/

declare(strict_types=1);

use Nette\Utils\Reflection;
use Test\B; // for testing purposes
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


class A
{
public Undeclared $undeclared;
public B $b;
public array $array;
public self $self;
public $none;
public ?B $nullable;
}

class AExt extends A
{
public parent $parent;
}

$class = new ReflectionClass('A');
$props = $class->getProperties();

Assert::same('Undeclared', Reflection::getPropertyType($props[0]));
Assert::same('Test\B', Reflection::getPropertyType($props[1]));
Assert::same('array', Reflection::getPropertyType($props[2]));
Assert::same('A', Reflection::getPropertyType($props[3]));
Assert::null(Reflection::getPropertyType($props[4]));
Assert::same('Test\B', Reflection::getPropertyType($props[5]));

$class = new ReflectionClass('AExt');
$props = $class->getProperties();

Assert::same('A', Reflection::getPropertyType($props[0]));

0 comments on commit 574e1f3

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.