23use ReflectionFunction;
24use ReflectionProperty;
65trait DeprecationHelper {
76 protected static $deprecatedPublicProperties = [];
83 private $dynamicPropertiesAccessDeprecated =
false;
105 if ( isset( self::$deprecatedPublicProperties[$property] ) ) {
108 self::$deprecatedPublicProperties[$property] = [
141 if ( isset( self::$deprecatedPublicProperties[$property] ) ) {
144 self::$deprecatedPublicProperties[$property] = [
164 string $class =
null,
165 string $component =
null
167 $this->dynamicPropertiesAccessDeprecated = [ $version, $class ?: __CLASS__, $component ];
174 if ( isset( self::$deprecatedPublicProperties[$name] ) ) {
175 [ $version, $class, $component, $getter ] = self::$deprecatedPublicProperties[$name];
176 $qualifiedName = $class .
'::$' . $name;
177 wfDeprecated( $qualifiedName, $version, $component, 2 );
179 return $this->deprecationHelperCallGetter( $getter );
184 $ownerClass = $this->deprecationHelperGetPropertyOwner( $name );
189 if ( $this->dynamicPropertiesAccessDeprecated ) {
190 [ $version, $class, $component ] = $this->dynamicPropertiesAccessDeprecated;
191 $qualifiedName = $class .
'::$' . $name;
192 wfDeprecated( $qualifiedName, $version, $component, 2 );
199 if ( get_object_vars( $this ) === [] ) {
206 if ( isset( self::$deprecatedPublicProperties[$name] ) ) {
207 [ $version, $class, $component, $getter ] = self::$deprecatedPublicProperties[$name];
208 $qualifiedName = $class .
'::$' . $name;
209 wfDeprecated( $qualifiedName, $version, $component, 2 );
211 return $this->deprecationHelperCallGetter( $getter );
216 $ownerClass = $this->deprecationHelperGetPropertyOwner( $name );
217 $qualifiedName = ( $ownerClass ?: get_class( $this ) ) .
'::$' . $name;
220 trigger_error(
"Cannot access non-public property $qualifiedName", E_USER_ERROR );
221 } elseif ( property_exists( $this, $name ) ) {
229 trigger_error(
"Undefined property: $qualifiedName", E_USER_NOTICE );
234 public function __set( $name, $value ) {
235 if ( get_object_vars( $this ) === [] ) {
239 $this->$name = $value;
243 if ( isset( self::$deprecatedPublicProperties[$name] ) ) {
244 [ $version, $class, $component, , $setter ] = self::$deprecatedPublicProperties[$name];
245 $qualifiedName = $class .
'::$' . $name;
246 wfDeprecated( $qualifiedName, $version, $component, 2 );
248 $this->deprecationHelperCallSetter( $setter, $value );
249 } elseif ( property_exists( $this, $name ) ) {
250 $this->$name = $value;
252 trigger_error(
"Cannot access non-public property $qualifiedName", E_USER_ERROR );
257 $ownerClass = $this->deprecationHelperGetPropertyOwner( $name );
258 $qualifiedName = ( $ownerClass ?: get_class( $this ) ) .
'::$' . $name;
261 trigger_error(
"Cannot access non-public property $qualifiedName", E_USER_ERROR );
263 if ( $this->dynamicPropertiesAccessDeprecated ) {
264 [ $version, $class, $component ] = $this->dynamicPropertiesAccessDeprecated;
265 $qualifiedName = $class .
'::$' . $name;
266 wfDeprecated( $qualifiedName, $version, $component, 2 );
269 $this->$name = $value;
280 private function deprecationHelperGetPropertyOwner( $property ) {
283 $obfuscatedProps = array_keys( (array)$this );
284 $obfuscatedPropTail =
"\0$property";
285 foreach ( $obfuscatedProps as $obfuscatedProp ) {
287 if ( strpos( $obfuscatedProp, $obfuscatedPropTail, 1 ) !==
false ) {
288 $classname = substr( $obfuscatedProp, 1, -strlen( $obfuscatedPropTail ) );
289 if ( $classname ===
'*' ) {
292 return (
new ReflectionProperty( $this, $property ) )->getDeclaringClass()->getName();
300 private function deprecationHelperCallGetter( $getter ) {
301 if ( is_string( $getter ) ) {
302 $getter = [ $this, $getter ];
303 } elseif ( (
new ReflectionFunction( $getter ) )->getClosureThis() !==
null ) {
304 $getter = $getter->bindTo( $this );
309 private function deprecationHelperCallSetter( $setter, $value ) {
310 if ( is_string( $setter ) ) {
311 $setter = [ $this, $setter ];
312 } elseif ( (
new ReflectionFunction( $setter ) )->getClosureThis() !==
null ) {
313 $setter = $setter->bindTo( $this );
319class_alias( DeprecationHelper::class,
'DeprecationHelper' );
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.