From ae17c4b857f3c2f76218797ebe193e8f46240528 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 12 Nov 2020 17:58:05 +0100 Subject: [PATCH] Various minor cleanups --- src/ProxyManager/Factory/AbstractBaseFactory.php | 2 +- .../Generator/Util/IdentifierSuffixer.php | 2 +- .../MethodGenerator/CallInitializer.php | 4 ++-- .../ProxyGenerator/Util/PublicScopeSimulator.php | 4 ++-- .../ValueHolder/MethodGenerator/Constructor.php | 2 +- ...ccessInterceptorValueHolderFunctionalTest.php | 2 +- .../LazyLoadingValueHolderFunctionalTest.php | 4 ++-- .../MethodGenerator/CallInitializerTest.php | 8 ++++---- .../Util/PublicScopeSimulatorTest.php | 16 ++++++++-------- .../MethodGenerator/ConstructorTest.php | 8 ++++---- .../ClassWithFinalMethods.php | 2 +- 11 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/ProxyManager/Factory/AbstractBaseFactory.php b/src/ProxyManager/Factory/AbstractBaseFactory.php index 463a6c203..dd2cad901 100644 --- a/src/ProxyManager/Factory/AbstractBaseFactory.php +++ b/src/ProxyManager/Factory/AbstractBaseFactory.php @@ -35,7 +35,7 @@ abstract class AbstractBaseFactory public function __construct(?Configuration $configuration = null) { - $this->configuration = $configuration ?: new Configuration(); + $this->configuration = $configuration ?? new Configuration(); } /** diff --git a/src/ProxyManager/Generator/Util/IdentifierSuffixer.php b/src/ProxyManager/Generator/Util/IdentifierSuffixer.php index d4c95531a..05921683c 100644 --- a/src/ProxyManager/Generator/Util/IdentifierSuffixer.php +++ b/src/ProxyManager/Generator/Util/IdentifierSuffixer.php @@ -36,7 +36,7 @@ public static function getIdentifier(string $name): string /** @var string|null $salt */ static $salt; - $salt ??= $salt = self::loadBaseHashSalt(); + $salt ??= self::loadBaseHashSalt(); $suffix = substr(sha1($name . $salt), 0, 5); if (! preg_match(self::VALID_IDENTIFIER_FORMAT, $name)) { diff --git a/src/ProxyManager/ProxyGenerator/LazyLoadingGhost/MethodGenerator/CallInitializer.php b/src/ProxyManager/ProxyGenerator/LazyLoadingGhost/MethodGenerator/CallInitializer.php index 0867a1f06..493c212de 100644 --- a/src/ProxyManager/ProxyGenerator/LazyLoadingGhost/MethodGenerator/CallInitializer.php +++ b/src/ProxyManager/ProxyGenerator/LazyLoadingGhost/MethodGenerator/CallInitializer.php @@ -96,7 +96,7 @@ private function propertiesInitializationCode(Properties $properties): string foreach ($properties->getGroupedPrivateProperties() as $className => $privateProperties) { $cacheKey = 'cache' . str_replace('\\', '_', $className); $assignments[] = 'static $' . $cacheKey . ";\n\n" - . '$' . $cacheKey . ' ?: $' . $cacheKey . " = \\Closure::bind(static function (\$instance) {\n" + . '$' . $cacheKey . ' ?? $' . $cacheKey . " = \\Closure::bind(static function (\$instance) {\n" . $this->getPropertyDefaultsAssignments($privateProperties) . "\n" . '}, null, ' . var_export($className, true) . ");\n\n" . '$' . $cacheKey . "(\$this);\n\n"; @@ -139,7 +139,7 @@ private function propertiesReferenceArrayCode(Properties $properties): string $cacheKey = 'cacheFetch' . str_replace('\\', '_', $className); $code .= 'static $' . $cacheKey . ";\n\n" - . '$' . $cacheKey . ' ?: $' . $cacheKey + . '$' . $cacheKey . ' ?? $' . $cacheKey . " = \\Closure::bind(function (\$instance, array & \$properties) {\n" . $this->generatePrivatePropertiesAssignmentsCode($classPrivateProperties) . '}, $this, ' . var_export($className, true) . ");\n\n" diff --git a/src/ProxyManager/ProxyGenerator/Util/PublicScopeSimulator.php b/src/ProxyManager/ProxyGenerator/Util/PublicScopeSimulator.php index 61dd3cd23..33307aa97 100644 --- a/src/ProxyManager/ProxyGenerator/Util/PublicScopeSimulator.php +++ b/src/ProxyManager/ProxyGenerator/Util/PublicScopeSimulator.php @@ -79,7 +79,7 @@ private static function getUndefinedPropertyNotice(string $operationType, string return ''; } - return ' $backtrace = debug_backtrace(false);' . "\n" + return ' $backtrace = debug_backtrace(false, 1);' . "\n" . ' trigger_error(' . "\n" . ' sprintf(' . "\n" . ' \'Undefined property: %s::$%s in %s on line %s\',' . "\n" @@ -147,7 +147,7 @@ private static function getOperation(string $operationType, string $nameParamete */ private static function getScopeReBind(): string { - return '$backtrace = debug_backtrace(true);' . "\n" + return '$backtrace = debug_backtrace(true, 2);' . "\n" . '$scopeObject = isset($backtrace[1][\'object\'])' . ' ? $backtrace[1][\'object\'] : new \ProxyManager\Stub\EmptyClassStub();' . "\n" . '$accessor = $accessor->bindTo($scopeObject, get_class($scopeObject));' . "\n"; diff --git a/src/ProxyManager/ProxyGenerator/ValueHolder/MethodGenerator/Constructor.php b/src/ProxyManager/ProxyGenerator/ValueHolder/MethodGenerator/Constructor.php index 9c8dc2dae..d25d2b3e9 100644 --- a/src/ProxyManager/ProxyGenerator/ValueHolder/MethodGenerator/Constructor.php +++ b/src/ProxyManager/ProxyGenerator/ValueHolder/MethodGenerator/Constructor.php @@ -40,7 +40,7 @@ public static function generateMethod(ReflectionClass $originalClass, PropertyGe $constructor->setBody( 'static $reflection;' . "\n\n" . 'if (! $this->' . $valueHolder->getName() . ') {' . "\n" - . ' $reflection = $reflection ?: new \ReflectionClass(' + . ' $reflection = $reflection ?? new \ReflectionClass(' . var_export($originalClass->getName(), true) . ");\n" . ' $this->' . $valueHolder->getName() . ' = $reflection->newInstanceWithoutConstructor();' . "\n" diff --git a/tests/ProxyManagerTest/Functional/AccessInterceptorValueHolderFunctionalTest.php b/tests/ProxyManagerTest/Functional/AccessInterceptorValueHolderFunctionalTest.php index 91ef6e8e0..806c3a691 100644 --- a/tests/ProxyManagerTest/Functional/AccessInterceptorValueHolderFunctionalTest.php +++ b/tests/ProxyManagerTest/Functional/AccessInterceptorValueHolderFunctionalTest.php @@ -273,7 +273,7 @@ public function testPropertyUnset( AccessInterceptorValueHolderInterface $proxy, string $publicProperty ): void { - $instance = $proxy->getWrappedValueHolderValue() ?: $instance; + $instance = $proxy->getWrappedValueHolderValue() ?? $instance; unset($proxy->$publicProperty); self::assertFalse(isset($instance->$publicProperty)); diff --git a/tests/ProxyManagerTest/Functional/LazyLoadingValueHolderFunctionalTest.php b/tests/ProxyManagerTest/Functional/LazyLoadingValueHolderFunctionalTest.php index e64715b80..bf6294b4f 100644 --- a/tests/ProxyManagerTest/Functional/LazyLoadingValueHolderFunctionalTest.php +++ b/tests/ProxyManagerTest/Functional/LazyLoadingValueHolderFunctionalTest.php @@ -203,7 +203,7 @@ public function testPropertyAbsence(object $instance, VirtualProxyInterface $pro self::markTestSkipped('Non-nullable typed properties cannot be removed/unset'); } - $instance = $proxy->getWrappedValueHolderValue() ?: $instance; + $instance = $proxy->getWrappedValueHolderValue() ?? $instance; $instance->$publicProperty = null; self::assertFalse(isset($proxy->$publicProperty)); self::assertTrue($proxy->isProxyInitialized()); @@ -214,7 +214,7 @@ public function testPropertyAbsence(object $instance, VirtualProxyInterface $pro */ public function testPropertyUnset(object $instance, VirtualProxyInterface $proxy, string $publicProperty): void { - $instance = $proxy->getWrappedValueHolderValue() ?: $instance; + $instance = $proxy->getWrappedValueHolderValue() ?? $instance; unset($proxy->$publicProperty); self::assertTrue($proxy->isProxyInitialized()); diff --git a/tests/ProxyManagerTest/ProxyGenerator/LazyLoadingGhost/MethodGenerator/CallInitializerTest.php b/tests/ProxyManagerTest/ProxyGenerator/LazyLoadingGhost/MethodGenerator/CallInitializerTest.php index 0c84f5023..8855e7da7 100644 --- a/tests/ProxyManagerTest/ProxyGenerator/LazyLoadingGhost/MethodGenerator/CallInitializerTest.php +++ b/tests/ProxyManagerTest/ProxyGenerator/LazyLoadingGhost/MethodGenerator/CallInitializerTest.php @@ -50,7 +50,7 @@ public function testBodyStructure(): void $this->protectedProperty2 = 'protectedProperty2'; static $cacheProxyManagerTestAsset_ClassWithMixedProperties; -$cacheProxyManagerTestAsset_ClassWithMixedProperties ?: $cacheProxyManagerTestAsset_ClassWithMixedProperties = \Closure::bind(static function ($instance) { +$cacheProxyManagerTestAsset_ClassWithMixedProperties ?? $cacheProxyManagerTestAsset_ClassWithMixedProperties = \Closure::bind(static function ($instance) { $instance->privateProperty0 = 'privateProperty0'; $instance->privateProperty1 = 'privateProperty1'; $instance->privateProperty2 = 'privateProperty2'; @@ -72,7 +72,7 @@ public function testBodyStructure(): void static $cacheFetchProxyManagerTestAsset_ClassWithMixedProperties; -$cacheFetchProxyManagerTestAsset_ClassWithMixedProperties ?: $cacheFetchProxyManagerTestAsset_ClassWithMixedProperties = \Closure::bind(function ($instance, array & $properties) { +$cacheFetchProxyManagerTestAsset_ClassWithMixedProperties ?? $cacheFetchProxyManagerTestAsset_ClassWithMixedProperties = \Closure::bind(function ($instance, array & $properties) { $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedProperties' . "\0" . 'privateProperty0'] = & $instance->privateProperty0; $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedProperties' . "\0" . 'privateProperty1'] = & $instance->privateProperty1; $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedProperties' . "\0" . 'privateProperty2'] = & $instance->privateProperty2; @@ -175,7 +175,7 @@ public function testBodyStructureWithTypedProperties(): void $this->protectedNullableClassProperty = NULL; static $cacheProxyManagerTestAsset_ClassWithMixedTypedProperties; -$cacheProxyManagerTestAsset_ClassWithMixedTypedProperties ?: $cacheProxyManagerTestAsset_ClassWithMixedTypedProperties = \Closure::bind(static function ($instance) { +$cacheProxyManagerTestAsset_ClassWithMixedTypedProperties ?? $cacheProxyManagerTestAsset_ClassWithMixedTypedProperties = \Closure::bind(static function ($instance) { $instance->privateUnTypedProperty = 'privateUnTypedProperty'; $instance->privateUnTypedPropertyWithoutDefaultValue = NULL; $instance->privateBoolProperty = true; @@ -262,7 +262,7 @@ public function testBodyStructureWithTypedProperties(): void static $cacheFetchProxyManagerTestAsset_ClassWithMixedTypedProperties; -$cacheFetchProxyManagerTestAsset_ClassWithMixedTypedProperties ?: $cacheFetchProxyManagerTestAsset_ClassWithMixedTypedProperties = \Closure::bind(function ($instance, array & $properties) { +$cacheFetchProxyManagerTestAsset_ClassWithMixedTypedProperties ?? $cacheFetchProxyManagerTestAsset_ClassWithMixedTypedProperties = \Closure::bind(function ($instance, array & $properties) { $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateUnTypedProperty'] = & $instance->privateUnTypedProperty; $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateUnTypedPropertyWithoutDefaultValue'] = & $instance->privateUnTypedPropertyWithoutDefaultValue; $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateBoolProperty'] = & $instance->privateBoolProperty; diff --git a/tests/ProxyManagerTest/ProxyGenerator/Util/PublicScopeSimulatorTest.php b/tests/ProxyManagerTest/ProxyGenerator/Util/PublicScopeSimulatorTest.php index ad3a65bc4..f89ebc2b4 100644 --- a/tests/ProxyManagerTest/ProxyGenerator/Util/PublicScopeSimulatorTest.php +++ b/tests/ProxyManagerTest/ProxyGenerator/Util/PublicScopeSimulatorTest.php @@ -25,7 +25,7 @@ public function testSimpleGet(): void if (! $realInstanceReflection->hasProperty($foo)) { $targetObject = $this; - $backtrace = debug_backtrace(false); + $backtrace = debug_backtrace(false, 1); trigger_error( sprintf( 'Undefined property: %s::$%s in %s on line %s', @@ -44,7 +44,7 @@ public function testSimpleGet(): void $accessor = function & () use ($targetObject, $name) { return $targetObject->$foo; }; -$backtrace = debug_backtrace(true); +$backtrace = debug_backtrace(true, 2); $scopeObject = isset($backtrace[1]['object']) ? $backtrace[1]['object'] : new \ProxyManager\Stub\EmptyClassStub(); $accessor = $accessor->bindTo($scopeObject, get_class($scopeObject)); $bar = & $accessor(); @@ -78,7 +78,7 @@ public function testSimpleSet(): void $accessor = function & () use ($targetObject, $name, $value) { return $targetObject->$foo = $baz; }; -$backtrace = debug_backtrace(true); +$backtrace = debug_backtrace(true, 2); $scopeObject = isset($backtrace[1]['object']) ? $backtrace[1]['object'] : new \ProxyManager\Stub\EmptyClassStub(); $accessor = $accessor->bindTo($scopeObject, get_class($scopeObject)); $bar = & $accessor(); @@ -112,7 +112,7 @@ public function testSimpleIsset(): void $accessor = function () use ($targetObject, $name) { return isset($targetObject->$foo); }; -$backtrace = debug_backtrace(true); +$backtrace = debug_backtrace(true, 2); $scopeObject = isset($backtrace[1]['object']) ? $backtrace[1]['object'] : new \ProxyManager\Stub\EmptyClassStub(); $accessor = $accessor->bindTo($scopeObject, get_class($scopeObject)); $bar = $accessor(); @@ -146,7 +146,7 @@ public function testSimpleUnset(): void $accessor = function () use ($targetObject, $name) { unset($targetObject->$foo); }; -$backtrace = debug_backtrace(true); +$backtrace = debug_backtrace(true, 2); $scopeObject = isset($backtrace[1]['object']) ? $backtrace[1]['object'] : new \ProxyManager\Stub\EmptyClassStub(); $accessor = $accessor->bindTo($scopeObject, get_class($scopeObject)); $bar = $accessor(); @@ -193,7 +193,7 @@ public function testDelegatesToValueHolderWhenAvailable(): void $accessor = function & () use ($targetObject, $name, $value) { return $targetObject->$foo = $baz; }; -$backtrace = debug_backtrace(true); +$backtrace = debug_backtrace(true, 2); $scopeObject = isset($backtrace[1]['object']) ? $backtrace[1]['object'] : new \ProxyManager\Stub\EmptyClassStub(); $accessor = $accessor->bindTo($scopeObject, get_class($scopeObject)); $bar = & $accessor(); @@ -226,7 +226,7 @@ public function testWillReturnDirectlyWithNoReturnParam(): void if (! $realInstanceReflection->hasProperty($foo)) { $targetObject = $this; - $backtrace = debug_backtrace(false); + $backtrace = debug_backtrace(false, 1); trigger_error( sprintf( 'Undefined property: %s::$%s in %s on line %s', @@ -245,7 +245,7 @@ public function testWillReturnDirectlyWithNoReturnParam(): void $accessor = function & () use ($targetObject, $name) { return $targetObject->$foo; }; -$backtrace = debug_backtrace(true); +$backtrace = debug_backtrace(true, 2); $scopeObject = isset($backtrace[1]['object']) ? $backtrace[1]['object'] : new \ProxyManager\Stub\EmptyClassStub(); $accessor = $accessor->bindTo($scopeObject, get_class($scopeObject)); $returnValue = & $accessor(); diff --git a/tests/ProxyManagerTest/ProxyGenerator/ValueHolder/MethodGenerator/ConstructorTest.php b/tests/ProxyManagerTest/ProxyGenerator/ValueHolder/MethodGenerator/ConstructorTest.php index e75a79eab..f5743d5fa 100644 --- a/tests/ProxyManagerTest/ProxyGenerator/ValueHolder/MethodGenerator/ConstructorTest.php +++ b/tests/ProxyManagerTest/ProxyGenerator/ValueHolder/MethodGenerator/ConstructorTest.php @@ -41,7 +41,7 @@ public function testBodyStructure(): void 'static $reflection; if (! $this->foo) { - $reflection = $reflection ?: new \ReflectionClass(\'ProxyManagerTestAsset\\\\ProxyGenerator\\\\LazyLoading\\\\' + $reflection = $reflection ?? new \ReflectionClass(\'ProxyManagerTestAsset\\\\ProxyGenerator\\\\LazyLoading\\\\' . 'MethodGenerator\\\\ClassWithTwoPublicProperties\'); $this->foo = $reflection->newInstanceWithoutConstructor(); unset($this->bar, $this->baz); @@ -68,7 +68,7 @@ public function testBodyStructureWithoutPublicProperties(): void 'static $reflection; if (! $this->foo) { - $reflection = $reflection ?: new \ReflectionClass(\'ProxyManagerTestAsset\\\\EmptyClass\'); + $reflection = $reflection ?? new \ReflectionClass(\'ProxyManagerTestAsset\\\\EmptyClass\'); $this->foo = $reflection->newInstanceWithoutConstructor(); }', $constructor->getBody() @@ -89,7 +89,7 @@ public function testBodyStructureWithStaticProperties(): void $expectedCode = 'static $reflection; if (! $this->foo) { - $reflection = $reflection ?: new \ReflectionClass(\'ProxyManagerTestAsset\\\\ClassWithMixedProperties\'); + $reflection = $reflection ?? new \ReflectionClass(\'ProxyManagerTestAsset\\\\ClassWithMixedProperties\'); $this->foo = $reflection->newInstanceWithoutConstructor(); unset($this->publicProperty0, $this->publicProperty1, $this->publicProperty2, $this->protectedProperty0, ' . '$this->protectedProperty1, $this->protectedProperty2); @@ -121,7 +121,7 @@ public function testBodyStructureWithVariadicArguments(): void static $reflection; if (! $this->foo) { - $reflection = $reflection ?: new \ReflectionClass('ProxyManagerTestAsset\\ClassWithVariadicConstructorArgument'); + $reflection = $reflection ?? new \ReflectionClass('ProxyManagerTestAsset\\ClassWithVariadicConstructorArgument'); $this->foo = $reflection->newInstanceWithoutConstructor(); \Closure::bind(function (\ProxyManagerTestAsset\ClassWithVariadicConstructorArgument $instance) { unset($instance->foo, $instance->bar); diff --git a/tests/ProxyManagerTestAsset/ClassWithFinalMethods.php b/tests/ProxyManagerTestAsset/ClassWithFinalMethods.php index 749d8a670..4bf7b75b6 100644 --- a/tests/ProxyManagerTestAsset/ClassWithFinalMethods.php +++ b/tests/ProxyManagerTestAsset/ClassWithFinalMethods.php @@ -18,7 +18,7 @@ final public function foo() { } - final private function bar() + private function bar() { }