mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 09:31:51 +01:00
Reflection - getProperties() method - allow to include properties of parent classes
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
"name": "meritoo/common-library",
|
"name": "meritoo/common-library",
|
||||||
"description": "Useful classes, methods, extensions etc.",
|
"description": "Useful classes, methods, extensions etc.",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"version": "0.0.11",
|
"version": "0.0.12",
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
"name": "Meritoo.pl",
|
"name": "Meritoo.pl",
|
||||||
|
|||||||
@@ -417,12 +417,14 @@ class Reflection
|
|||||||
/**
|
/**
|
||||||
* Returns given object properties
|
* Returns given object properties
|
||||||
*
|
*
|
||||||
* @param array|object|string $source An array of objects, namespaces, object or namespace
|
* @param array|object|string $source An array of objects, namespaces, object or namespace
|
||||||
* @param int $filter (optional) Filter of properties. Uses ReflectionProperty class constants.
|
* @param int $filter (optional) Filter of properties. Uses ReflectionProperty class
|
||||||
* By default all properties are returned.
|
* constants. By default all properties are returned.
|
||||||
|
* @param bool $includeParents (optional) If is set to true, properties of parent classes are
|
||||||
|
* included (recursively). Otherwise - not.
|
||||||
* @return array|ReflectionProperty
|
* @return array|ReflectionProperty
|
||||||
*/
|
*/
|
||||||
public static function getProperties($source, $filter = null)
|
public static function getProperties($source, $filter = null, $includeParents = false)
|
||||||
{
|
{
|
||||||
$className = self::getClassName($source);
|
$className = self::getClassName($source);
|
||||||
$reflection = new ReflectionClass($className);
|
$reflection = new ReflectionClass($className);
|
||||||
@@ -434,14 +436,26 @@ class Reflection
|
|||||||
+ ReflectionProperty::IS_STATIC;
|
+ ReflectionProperty::IS_STATIC;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $reflection->getProperties($filter);
|
$properties = $reflection->getProperties($filter);
|
||||||
|
$parentProperties = [];
|
||||||
|
|
||||||
|
if ($includeParents) {
|
||||||
|
$parent = self::getParentClass($source);
|
||||||
|
|
||||||
|
if (false !== $parent) {
|
||||||
|
$parentClass = $parent->getName();
|
||||||
|
$parentProperties = self::getProperties($parentClass, $filter, $includeParents);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_merge($properties, $parentProperties);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a parent class
|
* Returns a parent class or false if there is no parent class
|
||||||
*
|
*
|
||||||
* @param array|object|string $source An array of objects, namespaces, object or namespace
|
* @param array|object|string $source An array of objects, namespaces, object or namespace
|
||||||
* @return ReflectionClass
|
* @return ReflectionClass|bool
|
||||||
*/
|
*/
|
||||||
public static function getParentClass($source)
|
public static function getParentClass($source)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -19,8 +19,15 @@ class A
|
|||||||
{
|
{
|
||||||
use E;
|
use E;
|
||||||
|
|
||||||
|
private $count = 1;
|
||||||
|
|
||||||
protected function lorem()
|
protected function lorem()
|
||||||
{
|
{
|
||||||
return 'ipsum';
|
return 'ipsum';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function getCount()
|
||||||
|
{
|
||||||
|
return $this->count;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,4 +17,10 @@ namespace Meritoo\Common\Test\Utilities\Reflection;
|
|||||||
*/
|
*/
|
||||||
class B extends A
|
class B extends A
|
||||||
{
|
{
|
||||||
|
protected $name = 'Lorem Ipsum';
|
||||||
|
|
||||||
|
public function getName()
|
||||||
|
{
|
||||||
|
return $this->name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ use Meritoo\Common\Test\Utilities\Reflection\C;
|
|||||||
use Meritoo\Common\Test\Utilities\Reflection\D;
|
use Meritoo\Common\Test\Utilities\Reflection\D;
|
||||||
use Meritoo\Common\Test\Utilities\Reflection\E;
|
use Meritoo\Common\Test\Utilities\Reflection\E;
|
||||||
use Meritoo\Common\Utilities\Reflection;
|
use Meritoo\Common\Utilities\Reflection;
|
||||||
|
use ReflectionProperty;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests of the useful reflection methods
|
* Tests of the useful reflection methods
|
||||||
@@ -176,11 +177,11 @@ class ReflectionTest extends BaseTestCase
|
|||||||
|
|
||||||
public function testGetMethods()
|
public function testGetMethods()
|
||||||
{
|
{
|
||||||
self::assertEquals(0, count(Reflection::getMethods(B::class, true)));
|
self::assertEquals(1, count(Reflection::getMethods(B::class, true)));
|
||||||
self::assertEquals(1, count(Reflection::getMethods(B::class)));
|
self::assertEquals(3, count(Reflection::getMethods(B::class)));
|
||||||
self::assertEquals(1, count(Reflection::getMethods(A::class)));
|
self::assertEquals(2, count(Reflection::getMethods(A::class)));
|
||||||
self::assertEquals(2, count(Reflection::getMethods(C::class, true)));
|
self::assertEquals(2, count(Reflection::getMethods(C::class, true)));
|
||||||
self::assertEquals(3, count(Reflection::getMethods(C::class)));
|
self::assertEquals(5, count(Reflection::getMethods(C::class)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -221,6 +222,23 @@ class ReflectionTest extends BaseTestCase
|
|||||||
self::assertFalse(Reflection::usesTrait(D::class, E::class, true));
|
self::assertFalse(Reflection::usesTrait(D::class, E::class, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testGetProperties()
|
||||||
|
{
|
||||||
|
self::assertCount(1, Reflection::getProperties(B::class));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetPropertiesUsingFilter()
|
||||||
|
{
|
||||||
|
self::assertCount(1, Reflection::getProperties(B::class, ReflectionProperty::IS_PROTECTED));
|
||||||
|
self::assertCount(0, Reflection::getProperties(B::class, ReflectionProperty::IS_PRIVATE));
|
||||||
|
self::assertCount(1, Reflection::getProperties(B::class, ReflectionProperty::IS_PRIVATE, true));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetPropertiesWithParents()
|
||||||
|
{
|
||||||
|
self::assertCount(2, Reflection::getProperties(B::class, null, true));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides invalid class and trait
|
* Provides invalid class and trait
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user