From adc44efc28648e6934ae206f9c9d071063d2c067 Mon Sep 17 00:00:00 2001 From: Alex Tartan Date: Wed, 28 Aug 2019 01:09:13 +0300 Subject: [PATCH] Extended ReflectionHelper --- composer.lock | 159 +++++++++--------- src/Helpers/ReflectionHelper.php | 17 +- test/Helpers/ReflectionHelperTest.php | 26 +++ test/Helpers/TestAssets/TestPrivateChild.php | 8 + test/Helpers/TestAssets/TestPrivateParent.php | 15 ++ 5 files changed, 142 insertions(+), 83 deletions(-) create mode 100644 test/Helpers/TestAssets/TestPrivateChild.php create mode 100644 test/Helpers/TestAssets/TestPrivateParent.php diff --git a/composer.lock b/composer.lock index 8143ee2..bc4c839 100644 --- a/composer.lock +++ b/composer.lock @@ -1428,34 +1428,35 @@ }, { "name": "ocramius/package-versions", - "version": "1.4.0", + "version": "1.5.1", "source": { "type": "git", "url": "https://github.com/Ocramius/PackageVersions.git", - "reference": "a4d4b60d0e60da2487bd21a2c6ac089f85570dbb" + "reference": "1d32342b8c1eb27353c8887c366147b4c2da673c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Ocramius/PackageVersions/zipball/a4d4b60d0e60da2487bd21a2c6ac089f85570dbb", - "reference": "a4d4b60d0e60da2487bd21a2c6ac089f85570dbb", + "url": "https://api.github.com/repos/Ocramius/PackageVersions/zipball/1d32342b8c1eb27353c8887c366147b4c2da673c", + "reference": "1d32342b8c1eb27353c8887c366147b4c2da673c", "shasum": "" }, "require": { "composer-plugin-api": "^1.0.0", - "php": "^7.1.0" + "php": "^7.3.0" }, "require-dev": { - "composer/composer": "^1.6.3", - "doctrine/coding-standard": "^5.0.1", + "composer/composer": "^1.8.6", + "doctrine/coding-standard": "^6.0.0", "ext-zip": "*", - "infection/infection": "^0.7.1", - "phpunit/phpunit": "^7.0.0" + "infection/infection": "^0.13.4", + "phpunit/phpunit": "^8.2.5", + "vimeo/psalm": "^3.4.9" }, "type": "composer-plugin", "extra": { "class": "PackageVersions\\Installer", "branch-alias": { - "dev-master": "2.0.x-dev" + "dev-master": "1.6.x-dev" } }, "autoload": { @@ -1474,7 +1475,7 @@ } ], "description": "Composer plugin that provides efficient querying for installed package versions (no runtime IO)", - "time": "2019-02-21T12:16:21+00:00" + "time": "2019-07-17T15:49:50+00:00" }, { "name": "padraic/humbug_get_contents", @@ -1635,18 +1636,18 @@ "authors": [ { "name": "Arne Blankerts", - "role": "Developer", - "email": "arne@blankerts.de" + "email": "arne@blankerts.de", + "role": "Developer" }, { "name": "Sebastian Heuer", - "role": "Developer", - "email": "sebastian@phpeople.de" + "email": "sebastian@phpeople.de", + "role": "Developer" }, { "name": "Sebastian Bergmann", - "role": "Developer", - "email": "sebastian@phpunit.de" + "email": "sebastian@phpunit.de", + "role": "Developer" } ], "description": "Component for reading phar.io manifest information from a PHP Archive (PHAR)", @@ -1682,18 +1683,18 @@ "authors": [ { "name": "Arne Blankerts", - "role": "Developer", - "email": "arne@blankerts.de" + "email": "arne@blankerts.de", + "role": "Developer" }, { "name": "Sebastian Heuer", - "role": "Developer", - "email": "sebastian@phpeople.de" + "email": "sebastian@phpeople.de", + "role": "Developer" }, { "name": "Sebastian Bergmann", - "role": "Developer", - "email": "sebastian@phpunit.de" + "email": "sebastian@phpunit.de", + "role": "Developer" } ], "description": "Library for handling version information and constraints", @@ -1751,9 +1752,9 @@ "authors": [ { "name": "Kitamura Satoshi", - "role": "Original creator", "email": "with.no.parachute@gmail.com", - "homepage": "https://www.facebook.com/satooshi.jp" + "homepage": "https://www.facebook.com/satooshi.jp", + "role": "Original creator" }, { "name": "Takashi Matsuo", @@ -2328,8 +2329,8 @@ "authors": [ { "name": "Sebastian Bergmann", - "role": "lead", - "email": "sebastian@phpunit.de" + "email": "sebastian@phpunit.de", + "role": "lead" } ], "description": "FilterIterator implementation that filters files based on a list of suffixes.", @@ -2370,8 +2371,8 @@ "authors": [ { "name": "Sebastian Bergmann", - "role": "lead", - "email": "sebastian@phpunit.de" + "email": "sebastian@phpunit.de", + "role": "lead" } ], "description": "Simple template engine.", @@ -2419,8 +2420,8 @@ "authors": [ { "name": "Sebastian Bergmann", - "role": "lead", - "email": "sebastian@phpunit.de" + "email": "sebastian@phpunit.de", + "role": "lead" } ], "description": "Utility class for timing", @@ -2910,8 +2911,8 @@ "authors": [ { "name": "Marco Pivetta", - "role": "maintainer", - "email": "ocramius@gmail.com" + "email": "ocramius@gmail.com", + "role": "maintainer" } ], "description": "Prevents installation of composer packages with known security vulnerabilities: no API, simply require it", @@ -3481,8 +3482,8 @@ "authors": [ { "name": "Sebastian Bergmann", - "role": "lead", - "email": "sebastian@phpunit.de" + "email": "sebastian@phpunit.de", + "role": "lead" } ], "description": "Collection of value objects that represent the types of the PHP type system", @@ -3524,8 +3525,8 @@ "authors": [ { "name": "Sebastian Bergmann", - "role": "lead", - "email": "sebastian@phpunit.de" + "email": "sebastian@phpunit.de", + "role": "lead" } ], "description": "Library that helps with managing the version number of Git-hosted PHP projects", @@ -3612,16 +3613,16 @@ }, { "name": "symfony/config", - "version": "v4.3.2", + "version": "v4.3.4", "source": { "type": "git", "url": "https://github.com/symfony/config.git", - "reference": "9198eea354be75794a7b1064de00d9ae9ae5090f" + "reference": "07d49c0f823e0bc367c6d84e35b61419188a5ece" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/config/zipball/9198eea354be75794a7b1064de00d9ae9ae5090f", - "reference": "9198eea354be75794a7b1064de00d9ae9ae5090f", + "url": "https://api.github.com/repos/symfony/config/zipball/07d49c0f823e0bc367c6d84e35b61419188a5ece", + "reference": "07d49c0f823e0bc367c6d84e35b61419188a5ece", "shasum": "" }, "require": { @@ -3672,20 +3673,20 @@ ], "description": "Symfony Config Component", "homepage": "https://symfony.com", - "time": "2019-06-08T06:33:08+00:00" + "time": "2019-08-26T08:26:39+00:00" }, { "name": "symfony/console", - "version": "v4.3.3", + "version": "v4.3.4", "source": { "type": "git", "url": "https://github.com/symfony/console.git", - "reference": "8b0ae5742ce9aaa8b0075665862c1ca397d1c1d9" + "reference": "de63799239b3881b8a08f8481b22348f77ed7b36" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/console/zipball/8b0ae5742ce9aaa8b0075665862c1ca397d1c1d9", - "reference": "8b0ae5742ce9aaa8b0075665862c1ca397d1c1d9", + "url": "https://api.github.com/repos/symfony/console/zipball/de63799239b3881b8a08f8481b22348f77ed7b36", + "reference": "de63799239b3881b8a08f8481b22348f77ed7b36", "shasum": "" }, "require": { @@ -3747,20 +3748,20 @@ ], "description": "Symfony Console Component", "homepage": "https://symfony.com", - "time": "2019-07-24T17:13:59+00:00" + "time": "2019-08-26T08:26:39+00:00" }, { "name": "symfony/filesystem", - "version": "v4.3.3", + "version": "v4.3.4", "source": { "type": "git", "url": "https://github.com/symfony/filesystem.git", - "reference": "b9896d034463ad6fd2bf17e2bf9418caecd6313d" + "reference": "9abbb7ef96a51f4d7e69627bc6f63307994e4263" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/filesystem/zipball/b9896d034463ad6fd2bf17e2bf9418caecd6313d", - "reference": "b9896d034463ad6fd2bf17e2bf9418caecd6313d", + "url": "https://api.github.com/repos/symfony/filesystem/zipball/9abbb7ef96a51f4d7e69627bc6f63307994e4263", + "reference": "9abbb7ef96a51f4d7e69627bc6f63307994e4263", "shasum": "" }, "require": { @@ -3797,20 +3798,20 @@ ], "description": "Symfony Filesystem Component", "homepage": "https://symfony.com", - "time": "2019-06-23T08:51:25+00:00" + "time": "2019-08-20T14:07:54+00:00" }, { "name": "symfony/finder", - "version": "v4.3.3", + "version": "v4.3.4", "source": { "type": "git", "url": "https://github.com/symfony/finder.git", - "reference": "9638d41e3729459860bb96f6247ccb61faaa45f2" + "reference": "86c1c929f0a4b24812e1eb109262fc3372c8e9f2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/finder/zipball/9638d41e3729459860bb96f6247ccb61faaa45f2", - "reference": "9638d41e3729459860bb96f6247ccb61faaa45f2", + "url": "https://api.github.com/repos/symfony/finder/zipball/86c1c929f0a4b24812e1eb109262fc3372c8e9f2", + "reference": "86c1c929f0a4b24812e1eb109262fc3372c8e9f2", "shasum": "" }, "require": { @@ -3846,7 +3847,7 @@ ], "description": "Symfony Finder Component", "homepage": "https://symfony.com", - "time": "2019-06-28T13:16:30+00:00" + "time": "2019-08-14T12:26:46+00:00" }, { "name": "symfony/polyfill-ctype", @@ -4025,16 +4026,16 @@ }, { "name": "symfony/process", - "version": "v4.3.3", + "version": "v4.3.4", "source": { "type": "git", "url": "https://github.com/symfony/process.git", - "reference": "856d35814cf287480465bb7a6c413bb7f5f5e69c" + "reference": "e89969c00d762349f078db1128506f7f3dcc0d4a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/process/zipball/856d35814cf287480465bb7a6c413bb7f5f5e69c", - "reference": "856d35814cf287480465bb7a6c413bb7f5f5e69c", + "url": "https://api.github.com/repos/symfony/process/zipball/e89969c00d762349f078db1128506f7f3dcc0d4a", + "reference": "e89969c00d762349f078db1128506f7f3dcc0d4a", "shasum": "" }, "require": { @@ -4070,20 +4071,20 @@ ], "description": "Symfony Process Component", "homepage": "https://symfony.com", - "time": "2019-05-30T16:10:05+00:00" + "time": "2019-08-26T08:26:39+00:00" }, { "name": "symfony/service-contracts", - "version": "v1.1.5", + "version": "v1.1.6", "source": { "type": "git", "url": "https://github.com/symfony/service-contracts.git", - "reference": "f391a00de78ec7ec8cf5cdcdae59ec7b883edb8d" + "reference": "ea7263d6b6d5f798b56a45a5b8d686725f2719a3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/service-contracts/zipball/f391a00de78ec7ec8cf5cdcdae59ec7b883edb8d", - "reference": "f391a00de78ec7ec8cf5cdcdae59ec7b883edb8d", + "url": "https://api.github.com/repos/symfony/service-contracts/zipball/ea7263d6b6d5f798b56a45a5b8d686725f2719a3", + "reference": "ea7263d6b6d5f798b56a45a5b8d686725f2719a3", "shasum": "" }, "require": { @@ -4128,20 +4129,20 @@ "interoperability", "standards" ], - "time": "2019-06-13T11:15:36+00:00" + "time": "2019-08-20T14:44:19+00:00" }, { "name": "symfony/stopwatch", - "version": "v4.3.2", + "version": "v4.3.4", "source": { "type": "git", "url": "https://github.com/symfony/stopwatch.git", - "reference": "6b100e9309e8979cf1978ac1778eb155c1f7d93b" + "reference": "1e4ff456bd625be5032fac9be4294e60442e9b71" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/stopwatch/zipball/6b100e9309e8979cf1978ac1778eb155c1f7d93b", - "reference": "6b100e9309e8979cf1978ac1778eb155c1f7d93b", + "url": "https://api.github.com/repos/symfony/stopwatch/zipball/1e4ff456bd625be5032fac9be4294e60442e9b71", + "reference": "1e4ff456bd625be5032fac9be4294e60442e9b71", "shasum": "" }, "require": { @@ -4178,20 +4179,20 @@ ], "description": "Symfony Stopwatch Component", "homepage": "https://symfony.com", - "time": "2019-05-27T08:16:38+00:00" + "time": "2019-08-07T11:52:19+00:00" }, { "name": "symfony/yaml", - "version": "v4.3.3", + "version": "v4.3.4", "source": { "type": "git", "url": "https://github.com/symfony/yaml.git", - "reference": "34d29c2acd1ad65688f58452fd48a46bd996d5a6" + "reference": "5a0b7c32dc3ec56fd4abae8a4a71b0cf05013686" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/yaml/zipball/34d29c2acd1ad65688f58452fd48a46bd996d5a6", - "reference": "34d29c2acd1ad65688f58452fd48a46bd996d5a6", + "url": "https://api.github.com/repos/symfony/yaml/zipball/5a0b7c32dc3ec56fd4abae8a4a71b0cf05013686", + "reference": "5a0b7c32dc3ec56fd4abae8a4a71b0cf05013686", "shasum": "" }, "require": { @@ -4237,7 +4238,7 @@ ], "description": "Symfony Yaml Component", "homepage": "https://symfony.com", - "time": "2019-07-24T14:47:54+00:00" + "time": "2019-08-20T14:27:59+00:00" }, { "name": "theseer/tokenizer", @@ -4272,8 +4273,8 @@ "authors": [ { "name": "Arne Blankerts", - "role": "Developer", - "email": "arne@blankerts.de" + "email": "arne@blankerts.de", + "role": "Developer" } ], "description": "A small library for converting tokenized PHP source code into XML and potentially other formats", diff --git a/src/Helpers/ReflectionHelper.php b/src/Helpers/ReflectionHelper.php index 338ddd7..6c76496 100644 --- a/src/Helpers/ReflectionHelper.php +++ b/src/Helpers/ReflectionHelper.php @@ -3,14 +3,20 @@ namespace AlexTartan\Helpers; +use function get_class; + final class ReflectionHelper { /** * @param mixed $value */ - public static function setPrivatePropertyValue(object $object, string $property, $value): void + public static function setPrivatePropertyValue(object $object, string $property, $value, string $className = null): void { - $reflection = new \ReflectionProperty(get_class($object), $property); + if ($className === null) { + $className = get_class($object); + } + + $reflection = new \ReflectionProperty($className, $property); $reflection->setAccessible(true); $reflection->setValue($object, $value); } @@ -18,9 +24,12 @@ public static function setPrivatePropertyValue(object $object, string $property, /** * @return mixed */ - public static function getPrivatePropertyValue(object $object, string $propertyName) + public static function getPrivatePropertyValue(object $object, string $propertyName, string $className = null) { - $reflection = new \ReflectionClass(get_class($object)); + if ($className === null) { + $className = get_class($object); + } + $reflection = new \ReflectionClass($className); $property = $reflection->getProperty($propertyName); $property->setAccessible(true); diff --git a/test/Helpers/ReflectionHelperTest.php b/test/Helpers/ReflectionHelperTest.php index 8c87152..0331b24 100644 --- a/test/Helpers/ReflectionHelperTest.php +++ b/test/Helpers/ReflectionHelperTest.php @@ -4,6 +4,8 @@ namespace AlexTartanTest\Helpers; use AlexTartan\Helpers\ReflectionHelper; +use AlexTartanTest\Helpers\TestAssets\TestPrivateChild; +use AlexTartanTest\Helpers\TestAssets\TestPrivateParent; use PHPUnit\Framework\TestCase; /** @@ -50,6 +52,30 @@ public function getVal(): int ); } + public function testGetPrivatePropertyValueFromParentClass(): void + { + $x = new TestPrivateChild(); + + $value = ReflectionHelper::getPrivatePropertyValue($x, 'prop', TestPrivateParent::class); + + self::assertSame( + $value, + 155 + ); + } + + public function testSetPrivatePropertyValueFromParentClass(): void + { + $x = new TestPrivateChild(); + + ReflectionHelper::setPrivatePropertyValue($x, 'prop', 255, TestPrivateParent::class); + + self::assertSame( + ReflectionHelper::getPrivatePropertyValue($x, 'prop', TestPrivateParent::class), + 255 + ); + } + public function testGetPrivatePropertyValue(): void { $x = new class() diff --git a/test/Helpers/TestAssets/TestPrivateChild.php b/test/Helpers/TestAssets/TestPrivateChild.php new file mode 100644 index 0000000..cbec5a6 --- /dev/null +++ b/test/Helpers/TestAssets/TestPrivateChild.php @@ -0,0 +1,8 @@ +prop; + } +}