60trait DeprecationHelper {
71 protected static $deprecatedPublicProperties = [];
78 private $dynamicPropertiesAccessDeprecated =
false;
100 if ( isset( self::$deprecatedPublicProperties[$property] ) ) {
103 self::$deprecatedPublicProperties[$property] = [
136 if ( isset( self::$deprecatedPublicProperties[$property] ) ) {
139 self::$deprecatedPublicProperties[$property] = [
159 string $class =
null,
160 string $component =
null
162 $this->dynamicPropertiesAccessDeprecated = [ $version, $class ?: __CLASS__, $component ];
169 if ( isset( self::$deprecatedPublicProperties[$name] ) ) {
170 [ $version, $class, $component, $getter ] = self::$deprecatedPublicProperties[$name];
171 $qualifiedName = $class .
'::$' . $name;
172 wfDeprecated( $qualifiedName, $version, $component, 2 );
174 return $this->deprecationHelperCallGetter( $getter );
179 $ownerClass = $this->deprecationHelperGetPropertyOwner( $name );
184 if ( $this->dynamicPropertiesAccessDeprecated ) {
185 [ $version, $class, $component ] = $this->dynamicPropertiesAccessDeprecated;
186 $qualifiedName = $class .
'::$' . $name;
187 wfDeprecated( $qualifiedName, $version, $component, 2 );
194 if ( isset( self::$deprecatedPublicProperties[$name] ) ) {
195 [ $version, $class, $component, $getter ] = self::$deprecatedPublicProperties[$name];
196 $qualifiedName = $class .
'::$' . $name;
197 wfDeprecated( $qualifiedName, $version, $component, 2 );
199 return $this->deprecationHelperCallGetter( $getter );
204 $ownerClass = $this->deprecationHelperGetPropertyOwner( $name );
205 $qualifiedName = ( $ownerClass ?: get_class( $this ) ) .
'::$' . $name;
208 trigger_error(
"Cannot access non-public property $qualifiedName", E_USER_ERROR );
209 } elseif ( property_exists( $this, $name ) ) {
217 trigger_error(
"Undefined property: $qualifiedName", E_USER_NOTICE );
222 public function __set( $name, $value ) {
223 if ( isset( self::$deprecatedPublicProperties[$name] ) ) {
224 [ $version, $class, $component, , $setter ] = self::$deprecatedPublicProperties[$name];
225 $qualifiedName = $class .
'::$' . $name;
226 wfDeprecated( $qualifiedName, $version, $component, 2 );
228 $this->deprecationHelperCallSetter( $setter, $value );
229 } elseif ( property_exists( $this, $name ) ) {
230 $this->$name = $value;
232 trigger_error(
"Cannot access non-public property $qualifiedName", E_USER_ERROR );
237 $ownerClass = $this->deprecationHelperGetPropertyOwner( $name );
238 $qualifiedName = ( $ownerClass ?: get_class( $this ) ) .
'::$' . $name;
241 trigger_error(
"Cannot access non-public property $qualifiedName", E_USER_ERROR );
243 if ( $this->dynamicPropertiesAccessDeprecated ) {
244 [ $version, $class, $component ] = $this->dynamicPropertiesAccessDeprecated;
245 $qualifiedName = $class .
'::$' . $name;
246 wfDeprecated( $qualifiedName, $version, $component, 2 );
249 $this->$name = $value;
260 private function deprecationHelperGetPropertyOwner( $property ) {
263 $obfuscatedProps = array_keys( (array)$this );
264 $obfuscatedPropTail =
"\0$property";
265 foreach ( $obfuscatedProps as $obfuscatedProp ) {
267 if ( strpos( $obfuscatedProp, $obfuscatedPropTail, 1 ) !==
false ) {
268 $classname = substr( $obfuscatedProp, 1, -strlen( $obfuscatedPropTail ) );
269 if ( $classname ===
'*' ) {
272 return (
new ReflectionProperty( $this, $property ) )->getDeclaringClass()->getName();
280 private function deprecationHelperCallGetter( $getter ) {
281 if ( is_string( $getter ) ) {
282 $getter = [ $this, $getter ];
283 } elseif ( (
new ReflectionFunction( $getter ) )->getClosureThis() !==
null ) {
284 $getter = $getter->bindTo( $this );
289 private function deprecationHelperCallSetter( $setter, $value ) {
290 if ( is_string( $setter ) ) {
291 $setter = [ $this, $setter ];
292 } elseif ( (
new ReflectionFunction( $setter ) )->getClosureThis() !==
null ) {
293 $setter = $setter->bindTo( $this );
deprecatePublicPropertyFallback(string $property, string $version, $getter, $setter=null, $class=null, $component=null)
Mark a removed public property as deprecated and provide fallback getter and setter callables.
deprecatePublicProperty( $property, $version, $class=null, $component=null)
Mark a property as deprecated.
deprecateDynamicPropertiesAccess(string $version, string $class=null, string $component=null)
Emit deprecation warnings when dynamic and unknown properties are accessed.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.