diff options
author | jenkins-bot <jenkins-bot@gerrit.wikimedia.org> | 2021-12-13 15:40:19 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@wikimedia.org> | 2021-12-13 15:40:19 +0000 |
commit | aecc9e1b2bacd0edc9c9cd584cc600851da4eabe (patch) | |
tree | 92f8fcc0fa274e1c71b9fd7a4ff8b3d091bbe643 | |
parent | 066ff7d8ffcd8a8532e709e664906edaf8959824 (diff) | |
parent | 972dde710732f4e9088893cab4a30adaa71bc8df (diff) |
Merge "DeprecationHelper: avoid closures"
-rw-r--r-- | includes/Title.php | 56 | ||||
-rw-r--r-- | includes/debug/DeprecationHelper.php | 30 | ||||
-rw-r--r-- | tests/phpunit/includes/debug/DeprecationHelperTest.php | 10 | ||||
-rw-r--r-- | tests/phpunit/includes/debug/TestDeprecatedClass.php | 13 |
4 files changed, 62 insertions, 47 deletions
diff --git a/includes/Title.php b/includes/Title.php index 07d18c80be84..20a838ea9cd8 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -218,43 +218,25 @@ class Title implements LinkTarget, PageIdentity, IDBAccessObject { } private function __construct() { - $this->deprecatePublicProperty( 'mTextform', '1.37', __CLASS__ ); - $this->deprecatePublicProperty( 'mUrlform', '1.37', __CLASS__ ); - $this->deprecatePublicProperty( 'mDbkeyform', '1.37', __CLASS__ ); - $this->deprecatePublicProperty( 'mNamespace', '1.37', __CLASS__ ); - $this->deprecatePublicProperty( 'mInterwiki', '1.37', __CLASS__ ); - $this->deprecatePublicProperty( 'mFragment', '1.37', __CLASS__ ); - - $this->deprecatePublicPropertyFallback( 'mTextform', '1.38', function () { - return $this->getText(); - } ); - - $this->deprecatePublicPropertyFallback( 'mUrlform', '1.38', function () { - return $this->getPartialURL(); - } ); - - $this->deprecatePublicPropertyFallback( 'mDbkeyform', '1.38', function () { - return $this->getDBkey(); - } ); - - $this->deprecatePublicPropertyFallback( 'mNamespace', '1.38', function () { - return $this->getNamespace(); - } ); - - $this->deprecatePublicPropertyFallback( 'mInterwiki', '1.38', function () { - return $this->getInterwiki(); - } ); - - $this->deprecatePublicPropertyFallback( - 'mFragment', - '1.38', - function () { - return $this->getFragment(); - }, - function ( $fragment ) { - $this->setFragment( $fragment ); - } - ); + // Phan is being silly about callable|string, see T297352. + // Note that the silliness doesn't trigger for 'getText', because gettext() exists + // as a global built-in function. + $this->deprecatePublicPropertyFallback( 'mTextform', '1.38', 'getText' ); + + // @phan-suppress-next-line PhanUndeclaredFunctionInCallable + $this->deprecatePublicPropertyFallback( 'mUrlform', '1.38', 'getPartialURL' ); + + // @phan-suppress-next-line PhanUndeclaredFunctionInCallable + $this->deprecatePublicPropertyFallback( 'mDbkeyform', '1.38', 'getDBkey' ); + + // @phan-suppress-next-line PhanUndeclaredFunctionInCallable + $this->deprecatePublicPropertyFallback( 'mNamespace', '1.38', 'getNamespace' ); + + // @phan-suppress-next-line PhanUndeclaredFunctionInCallable + $this->deprecatePublicPropertyFallback( 'mInterwiki', '1.38', 'getInterwiki' ); + + // @phan-suppress-next-line PhanUndeclaredFunctionInCallable + $this->deprecatePublicPropertyFallback( 'mFragment', '1.38', 'getFragment', 'setFragment' ); } /** diff --git a/includes/debug/DeprecationHelper.php b/includes/debug/DeprecationHelper.php index 914986c2ffe7..3a3750fff4bf 100644 --- a/includes/debug/DeprecationHelper.php +++ b/includes/debug/DeprecationHelper.php @@ -112,8 +112,10 @@ trait DeprecationHelper { * * @param string $property The name of the property. * @param string $version MediaWiki version where the property became deprecated. - * @param callable $getter A user provided getter that implements a `get` logic for the property. - * @param callable|null $setter A user provided setter that implements a `set` logic for the property. + * @param callable|string $getter A user provided getter that implements a `get` logic + * for the property. If a string is given, it is called as a method on $this. + * @param callable|string|null $setter A user provided setter that implements a `set` logic + * for the property. If a string is given, it is called as a method on $this. * @param string|null $class The class which has the deprecated property. * @param string|null $component * @@ -123,8 +125,8 @@ trait DeprecationHelper { protected function deprecatePublicPropertyFallback( string $property, string $version, - callable $getter, - ?callable $setter = null, + $getter, + $setter = null, $class = null, $component = null ) { @@ -163,7 +165,7 @@ trait DeprecationHelper { $qualifiedName = $class . '::$' . $name; wfDeprecated( $qualifiedName, $version, $component, 3 ); if ( $getter ) { - return $getter(); + return $this->deprecationHelperCallGetter( $getter ); } return true; } @@ -188,7 +190,7 @@ trait DeprecationHelper { $qualifiedName = $class . '::$' . $name; wfDeprecated( $qualifiedName, $version, $component, 3 ); if ( $getter ) { - return $getter(); + return $this->deprecationHelperCallGetter( $getter ); } return $this->$name; } @@ -217,7 +219,7 @@ trait DeprecationHelper { $qualifiedName = $class . '::$' . $name; wfDeprecated( $qualifiedName, $version, $component, 3 ); if ( $setter ) { - $setter( $value ); + $this->deprecationHelperCallSetter( $setter, $value ); } elseif ( property_exists( $this, $name ) ) { $this->$name = $value; } else { @@ -268,4 +270,18 @@ trait DeprecationHelper { } return false; } + + private function deprecationHelperCallGetter( $getter ) { + if ( is_string( $getter ) ) { + $getter = [ $this, $getter ]; + } + return $getter(); + } + + private function deprecationHelperCallSetter( $setter, $value ) { + if ( is_string( $setter ) ) { + $setter = [ $this, $setter ]; + } + $setter( $value ); + } } diff --git a/tests/phpunit/includes/debug/DeprecationHelperTest.php b/tests/phpunit/includes/debug/DeprecationHelperTest.php index 92d0f411b03d..b1c4a445e0cf 100644 --- a/tests/phpunit/includes/debug/DeprecationHelperTest.php +++ b/tests/phpunit/includes/debug/DeprecationHelperTest.php @@ -40,6 +40,7 @@ class DeprecationHelperTest extends MediaWikiIntegrationTestCase { [ 'protectedDeprecated', 0, null ], [ 'privateDeprecated', null, null ], [ 'fallbackDeprecated', null, null ], + [ 'fallbackDeprecatedMethodName', null, null ], [ 'fallbackGetterOnly', null, null ], [ 'protectedNonDeprecated', E_USER_ERROR, 'Cannot access non-public property TestDeprecatedClass::$protectedNonDeprecated' ], @@ -89,7 +90,7 @@ class DeprecationHelperTest extends MediaWikiIntegrationTestCase { /** * @dataProvider provideSet */ - public function testSet( $propName, $expectedLevel, $expectedMessage ) { + public function testSet( $propName, $expectedLevel, $expectedMessage, $expectedValue = 0 ) { $this->assertPropertySame( 1, $this->testClass, $propName ); if ( $expectedLevel ) { $this->assertErrorTriggered( function () use ( $propName ) { @@ -106,6 +107,8 @@ class DeprecationHelperTest extends MediaWikiIntegrationTestCase { } $this->assertPropertySame( 0, $this->testClass, $propName ); } + + $this->assertPropertySame( $expectedValue, $this->testClass, $propName ); } public function provideSet() { @@ -113,12 +116,13 @@ class DeprecationHelperTest extends MediaWikiIntegrationTestCase { [ 'protectedDeprecated', null, null ], [ 'privateDeprecated', null, null ], [ 'fallbackDeprecated', null, null ], + [ 'fallbackDeprecatedMethodName', null, null ], [ 'fallbackGetterOnly', E_USER_ERROR, 'Cannot access non-public property TestDeprecatedClass::$fallbackGetterOnly' ], [ 'protectedNonDeprecated', E_USER_ERROR, - 'Cannot access non-public property TestDeprecatedClass::$protectedNonDeprecated' ], + 'Cannot access non-public property TestDeprecatedClass::$protectedNonDeprecated', 1 ], [ 'privateNonDeprecated', E_USER_ERROR, - 'Cannot access non-public property TestDeprecatedClass::$privateNonDeprecated' ], + 'Cannot access non-public property TestDeprecatedClass::$privateNonDeprecated', 1 ], [ 'nonExistent', null, null ], ]; } diff --git a/tests/phpunit/includes/debug/TestDeprecatedClass.php b/tests/phpunit/includes/debug/TestDeprecatedClass.php index be5feffc19ea..c12138eee04a 100644 --- a/tests/phpunit/includes/debug/TestDeprecatedClass.php +++ b/tests/phpunit/includes/debug/TestDeprecatedClass.php @@ -10,6 +10,8 @@ class TestDeprecatedClass { private $privateNonDeprecated = 1; private $fallbackDeprecated = 1; + private $foo = 'FOO'; + public function __construct() { $this->deprecatePublicProperty( 'protectedDeprecated', '1.23' ); $this->deprecatePublicProperty( 'privateDeprecated', '1.24' ); @@ -22,6 +24,10 @@ class TestDeprecatedClass { $this->fallbackDeprecated = $value; } ); + $this->deprecatePublicPropertyFallback( 'fallbackDeprecatedMethodName', '1.26', + 'getFoo', + 'setFoo' + ); $this->deprecatePublicPropertyFallback( 'fallbackGetterOnly', '1.25', static function () { return 1; @@ -45,4 +51,11 @@ class TestDeprecatedClass { ]; } + public function getFoo() { + return $this->foo; + } + + public function setFoo( $foo ) { + $this->foo = $foo; + } } |