| 1 | <?php |
| 2 | /** |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | * http://www.gnu.org/copyleft/gpl.html |
| 17 | * |
| 18 | * @file |
| 19 | */ |
| 20 | |
| 21 | namespace MediaWiki\Debug; |
| 22 | |
| 23 | use Error; |
| 24 | use ReflectionFunction; |
| 25 | use ReflectionProperty; |
| 26 | |
| 27 | /** |
| 28 | * Trait for issuing warnings on deprecated access. |
| 29 | * |
| 30 | * Use this trait in classes which have properties for which public access |
| 31 | * is deprecated or implementation has been moved to another class. |
| 32 | * Set the list of properties in $deprecatedPublicProperties |
| 33 | * and make the properties non-public. The trait will preserve public access |
| 34 | * but issue deprecation warnings when it is needed. |
| 35 | * |
| 36 | * Example usage: |
| 37 | * class Foo { |
| 38 | * use DeprecationHelper; |
| 39 | * protected $bar; |
| 40 | * public function __construct() { |
| 41 | * $this->deprecatePublicProperty( 'bar', '1.21', __CLASS__ ); |
| 42 | * $this->deprecatePublicPropertyFallback( |
| 43 | * 'movedValue', |
| 44 | * '1.35', |
| 45 | * function () { |
| 46 | * return MediaWikiServices()::getInstance() |
| 47 | * ->getNewImplementationService()->getValue(); |
| 48 | * }, |
| 49 | * function ( $value ) { |
| 50 | * MediaWikiServices()::getInstance() |
| 51 | * ->getNewImplementationService()->setValue( $value ); |
| 52 | * } |
| 53 | * ); |
| 54 | * } |
| 55 | * } |
| 56 | * |
| 57 | * $foo = new Foo; |
| 58 | * $foo->bar; // works but logs a warning |
| 59 | * $foo->movedValue = 10; // works but logs a warning |
| 60 | * $movedValue = $foo->movedValue; // also works |
| 61 | * |
| 62 | * Cannot be used with classes that have their own __get/__set methods. |
| 63 | * |
| 64 | * @since 1.32 |
| 65 | */ |
| 66 | trait DeprecationHelper { |
| 67 | |
| 68 | /** |
| 69 | * List of deprecated properties, in <property name> => [<version>, <class>, |
| 70 | * <component>, <getter>, <setter> ] format where <version> is the MediaWiki version |
| 71 | * where the property got deprecated, <class> is the |
| 72 | * the name of the class defining the property, <component> is the MediaWiki component |
| 73 | * (extension, skin etc.) for use in the deprecation warning) or null if it is MediaWiki. |
| 74 | * E.g. [ 'mNewRev' => [ '1.32', 'DifferenceEngine', null ] |
| 75 | * @var string[][] |
| 76 | */ |
| 77 | protected static $deprecatedPublicProperties = []; |
| 78 | |
| 79 | /** |
| 80 | * Whether to emit a deprecation warning when unknown properties are accessed. |
| 81 | * |
| 82 | * @var bool|array |
| 83 | */ |
| 84 | private $dynamicPropertiesAccessDeprecated = false; |
| 85 | |
| 86 | /** |
| 87 | * Mark a property as deprecated. Only use this for properties that used to be public and only |
| 88 | * call it in the constructor. |
| 89 | * |
| 90 | * @note Providing callbacks makes it not serializable |
| 91 | * |
| 92 | * @param string $property The name of the property. |
| 93 | * @param string $version MediaWiki version where the property became deprecated. |
| 94 | * @param string|null $class The class which has the deprecated property. This can usually be |
| 95 | * guessed, but PHP can get confused when both the parent class and the subclass use the |
| 96 | * trait, so it should be specified in classes meant for subclassing. |
| 97 | * @param string|null $component |
| 98 | * @see wfDeprecated() |
| 99 | */ |
| 100 | protected function deprecatePublicProperty( |
| 101 | $property, |
| 102 | $version, |
| 103 | $class = null, |
| 104 | $component = null |
| 105 | ) { |
| 106 | if ( isset( self::$deprecatedPublicProperties[$property] ) ) { |
| 107 | return; |
| 108 | } |
| 109 | self::$deprecatedPublicProperties[$property] = [ |
| 110 | $version, |
| 111 | $class ?: __CLASS__, |
| 112 | $component, |
| 113 | null, null |
| 114 | ]; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Mark a removed public property as deprecated and provide fallback getter and setter callables. |
| 119 | * Only use this for properties that used to be public and only |
| 120 | * call it in the constructor. |
| 121 | * |
| 122 | * @param string $property The name of the property. |
| 123 | * @param string $version MediaWiki version where the property became deprecated. |
| 124 | * @param callable|string $getter A user provided getter that implements a `get` logic |
| 125 | * for the property. If a string is given, it is called as a method on $this. |
| 126 | * @param callable|string|null $setter A user provided setter that implements a `set` logic |
| 127 | * for the property. If a string is given, it is called as a method on $this. |
| 128 | * @param string|null $class The class which has the deprecated property. |
| 129 | * @param string|null $component |
| 130 | * |
| 131 | * @since 1.36 |
| 132 | * @see wfDeprecated() |
| 133 | */ |
| 134 | protected function deprecatePublicPropertyFallback( |
| 135 | string $property, |
| 136 | string $version, |
| 137 | $getter, |
| 138 | $setter = null, |
| 139 | $class = null, |
| 140 | $component = null |
| 141 | ) { |
| 142 | if ( isset( self::$deprecatedPublicProperties[$property] ) ) { |
| 143 | return; |
| 144 | } |
| 145 | self::$deprecatedPublicProperties[$property] = [ |
| 146 | $version, |
| 147 | $class ?: __CLASS__, |
| 148 | null, |
| 149 | $getter, |
| 150 | $setter, |
| 151 | $component |
| 152 | ]; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Emit deprecation warnings when dynamic and unknown properties |
| 157 | * are accessed. |
| 158 | * |
| 159 | * @param string $version MediaWiki version where the property became deprecated. |
| 160 | * @param string|null $class The class which has the deprecated property. |
| 161 | * @param string|null $component |
| 162 | */ |
| 163 | protected function deprecateDynamicPropertiesAccess( |
| 164 | string $version, |
| 165 | ?string $class = null, |
| 166 | ?string $component = null |
| 167 | ) { |
| 168 | $this->dynamicPropertiesAccessDeprecated = [ $version, $class ?: __CLASS__, $component ]; |
| 169 | } |
| 170 | |
| 171 | public function __isset( $name ) { |
| 172 | // Overriding magic __isset is required not only for isset() and empty(), |
| 173 | // but to correctly support null coalescing for dynamic properties, |
| 174 | // e.g. $foo->bar ?? 'default' |
| 175 | if ( isset( self::$deprecatedPublicProperties[$name] ) ) { |
| 176 | [ $version, $class, $component, $getter ] = self::$deprecatedPublicProperties[$name]; |
| 177 | $qualifiedName = $class . '::$' . $name; |
| 178 | wfDeprecated( $qualifiedName, $version, $component, 2 ); |
| 179 | if ( $getter ) { |
| 180 | return $this->deprecationHelperCallGetter( $getter ); |
| 181 | } |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | $ownerClass = $this->deprecationHelperGetPropertyOwner( $name ); |
| 186 | if ( $ownerClass ) { |
| 187 | // Someone tried to access a normal non-public property. Try to behave like PHP would. |
| 188 | return false; |
| 189 | } else { |
| 190 | if ( $this->dynamicPropertiesAccessDeprecated ) { |
| 191 | [ $version, $class, $component ] = $this->dynamicPropertiesAccessDeprecated; |
| 192 | $qualifiedName = $class . '::$' . $name; |
| 193 | wfDeprecated( $qualifiedName, $version, $component, 2 ); |
| 194 | } |
| 195 | return false; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | public function __get( $name ) { |
| 200 | if ( get_object_vars( $this ) === [] ) { |
| 201 | // Object is being destructed, all bets are off (T363492); |
| 202 | // in particular, we can't check $this->dynamicPropertiesAccessDeprecated anymore. |
| 203 | // Just get the property and hope for the best... |
| 204 | return $this->$name; |
| 205 | } |
| 206 | |
| 207 | if ( isset( self::$deprecatedPublicProperties[$name] ) ) { |
| 208 | [ $version, $class, $component, $getter ] = self::$deprecatedPublicProperties[$name]; |
| 209 | $qualifiedName = $class . '::$' . $name; |
| 210 | wfDeprecated( $qualifiedName, $version, $component, 2 ); |
| 211 | if ( $getter ) { |
| 212 | return $this->deprecationHelperCallGetter( $getter ); |
| 213 | } |
| 214 | return $this->$name; |
| 215 | } |
| 216 | |
| 217 | $ownerClass = $this->deprecationHelperGetPropertyOwner( $name ); |
| 218 | $qualifiedName = ( $ownerClass ?: get_class( $this ) ) . '::$' . $name; |
| 219 | if ( $ownerClass ) { |
| 220 | // Someone tried to access a normal non-public property. Try to behave like PHP would. |
| 221 | throw new Error( "Cannot access non-public property $qualifiedName" ); |
| 222 | } elseif ( property_exists( $this, $name ) ) { |
| 223 | // Normally __get method will not be even called if the property exists, |
| 224 | // but in tests if we mock an object that uses DeprecationHelper, |
| 225 | // __get and __set magic methods will be mocked as well, and called |
| 226 | // regardless of the property existence. Support that use-case. |
| 227 | return $this->$name; |
| 228 | } else { |
| 229 | // Non-existing property. Try to behave like PHP would. |
| 230 | trigger_error( "Undefined property: $qualifiedName", E_USER_NOTICE ); |
| 231 | } |
| 232 | return null; |
| 233 | } |
| 234 | |
| 235 | public function __set( $name, $value ) { |
| 236 | if ( get_object_vars( $this ) === [] ) { |
| 237 | // Object is being destructed, all bets are off (T363492); |
| 238 | // in particular, we can't check $this->dynamicPropertiesAccessDeprecated anymore. |
| 239 | // Just set the property and hope for the best... |
| 240 | $this->$name = $value; |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | if ( isset( self::$deprecatedPublicProperties[$name] ) ) { |
| 245 | [ $version, $class, $component, , $setter ] = self::$deprecatedPublicProperties[$name]; |
| 246 | $qualifiedName = $class . '::$' . $name; |
| 247 | wfDeprecated( $qualifiedName, $version, $component, 2 ); |
| 248 | if ( $setter ) { |
| 249 | $this->deprecationHelperCallSetter( $setter, $value ); |
| 250 | } elseif ( property_exists( $this, $name ) ) { |
| 251 | $this->$name = $value; |
| 252 | } else { |
| 253 | throw new Error( "Cannot access non-public property $qualifiedName" ); |
| 254 | } |
| 255 | return; |
| 256 | } |
| 257 | |
| 258 | $ownerClass = $this->deprecationHelperGetPropertyOwner( $name ); |
| 259 | $qualifiedName = ( $ownerClass ?: get_class( $this ) ) . '::$' . $name; |
| 260 | if ( $ownerClass ) { |
| 261 | // Someone tried to access a normal non-public property. Try to behave like PHP would. |
| 262 | throw new Error( "Cannot access non-public property $qualifiedName" ); |
| 263 | } else { |
| 264 | if ( $this->dynamicPropertiesAccessDeprecated ) { |
| 265 | [ $version, $class, $component ] = $this->dynamicPropertiesAccessDeprecated; |
| 266 | $qualifiedName = $class . '::$' . $name; |
| 267 | wfDeprecated( $qualifiedName, $version, $component, 2 ); |
| 268 | } |
| 269 | // Non-existing property. Try to behave like PHP would. |
| 270 | $this->$name = $value; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Like property_exists but also check for non-visible private properties and returns which |
| 276 | * class in the inheritance chain declared the property. |
| 277 | * @param string $property |
| 278 | * @return string|bool Best guess for the class in which the property is defined. False if |
| 279 | * the object does not have such a property. |
| 280 | */ |
| 281 | private function deprecationHelperGetPropertyOwner( $property ) { |
| 282 | // Returning false is a non-error path and should avoid slow checks like reflection. |
| 283 | // Use array cast hack instead. |
| 284 | $obfuscatedProps = array_keys( (array)$this ); |
| 285 | $obfuscatedPropTail = "\0$property"; |
| 286 | foreach ( $obfuscatedProps as $obfuscatedProp ) { |
| 287 | // private props are in the form \0<classname>\0<propname> |
| 288 | if ( strpos( $obfuscatedProp, $obfuscatedPropTail, 1 ) !== false ) { |
| 289 | $classname = substr( $obfuscatedProp, 1, -strlen( $obfuscatedPropTail ) ); |
| 290 | if ( $classname === '*' ) { |
| 291 | // protected property; we didn't get the name, but we are on an error path |
| 292 | // now so it's fine to use reflection |
| 293 | return ( new ReflectionProperty( $this, $property ) )->getDeclaringClass()->getName(); |
| 294 | } |
| 295 | return $classname; |
| 296 | } |
| 297 | } |
| 298 | return false; |
| 299 | } |
| 300 | |
| 301 | private function deprecationHelperCallGetter( $getter ) { |
| 302 | if ( is_string( $getter ) ) { |
| 303 | $getter = [ $this, $getter ]; |
| 304 | } elseif ( ( new ReflectionFunction( $getter ) )->getClosureThis() !== null ) { |
| 305 | $getter = $getter->bindTo( $this ); |
| 306 | } |
| 307 | return $getter(); |
| 308 | } |
| 309 | |
| 310 | private function deprecationHelperCallSetter( $setter, $value ) { |
| 311 | if ( is_string( $setter ) ) { |
| 312 | $setter = [ $this, $setter ]; |
| 313 | } elseif ( ( new ReflectionFunction( $setter ) )->getClosureThis() !== null ) { |
| 314 | $setter = $setter->bindTo( $this ); |
| 315 | } |
| 316 | $setter( $value ); |
| 317 | } |
| 318 | } |
| 319 | /** @deprecated class alias since 1.43 */ |
| 320 | class_alias( DeprecationHelper::class, 'DeprecationHelper' ); |