Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[lhc/web/wiklou.git] / includes / debug / DeprecationHelper.php
1 <?php
2 /**
3 * Trait for issuing warnings on deprecated access.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * Use this trait in classes which have properties for which public access
25 * is deprecated. Set the list of properties in $deprecatedPublicProperties
26 * and make the properties non-public. The trait will preserve public access
27 * but issue deprecation warnings when it is needed.
28 *
29 * Example usage:
30 * class Foo {
31 * use DeprecationHelper;
32 * protected $bar;
33 * public function __construct() {
34 * $this->deprecatePublicProperty( 'bar', '1.21', __CLASS__ );
35 * }
36 * }
37 *
38 * $foo = new Foo;
39 * $foo->bar; // works but logs a warning
40 *
41 * Cannot be used with classes that have their own __get/__set methods.
42 *
43 * @since 1.32
44 */
45 trait DeprecationHelper {
46
47 /**
48 * List of deprecated properties, in <property name> => [<version>, <class>, <component>] format
49 * where <version> is the MediaWiki version where the property got deprecated, <class> is the
50 * the name of the class defining the property, <component> is the MediaWiki component
51 * (extension, skin etc.) for use in the deprecation warning) or null if it is MediaWiki.
52 * E.g. [ 'mNewRev' => [ '1.32', 'DifferenceEngine', null ]
53 * @var string[][]
54 */
55 protected $deprecatedPublicProperties = [];
56
57 /**
58 * Mark a property as deprecated. Only use this for properties that used to be public and only
59 * call it in the constructor.
60 * @param string $property The name of the property.
61 * @param string $version MediaWiki version where the property became deprecated.
62 * @param string|null $class The class which has the deprecated property. This can usually be
63 * guessed, but PHP can get confused when both the parent class and the subclass use the
64 * trait, so it should be specified in classes meant for subclassing.
65 * @param string|null $component
66 * @see wfDeprecated()
67 */
68 protected function deprecatePublicProperty(
69 $property, $version, $class = null, $component = null
70 ) {
71 $this->deprecatedPublicProperties[$property] = [ $version, $class ?: __CLASS__, $component ];
72 }
73
74 public function __get( $name ) {
75 if ( isset( $this->deprecatedPublicProperties[$name] ) ) {
76 list( $version, $class, $component ) = $this->deprecatedPublicProperties[$name];
77 $qualifiedName = $class . '::$' . $name;
78 wfDeprecated( $qualifiedName, $version, $component, 3 );
79 return $this->$name;
80 }
81
82 $ownerClass = $this->deprecationHelperGetPropertyOwner( $name );
83 $qualifiedName = ( $ownerClass ?: get_class( $this ) ) . '::$' . $name;
84 if ( $ownerClass ) {
85 // Someone tried to access a normal non-public property. Try to behave like PHP would.
86 trigger_error( "Cannot access non-public property $qualifiedName", E_USER_ERROR );
87 } else {
88 // Non-existing property. Try to behave like PHP would.
89 trigger_error( "Undefined property: $qualifiedName", E_USER_NOTICE );
90 }
91 return null;
92 }
93
94 public function __set( $name, $value ) {
95 if ( isset( $this->deprecatedPublicProperties[$name] ) ) {
96 list( $version, $class, $component ) = $this->deprecatedPublicProperties[$name];
97 $qualifiedName = $class . '::$' . $name;
98 wfDeprecated( $qualifiedName, $version, $component, 3 );
99 $this->$name = $value;
100 return;
101 }
102
103 $ownerClass = $this->deprecationHelperGetPropertyOwner( $name );
104 $qualifiedName = ( $ownerClass ?: get_class( $this ) ) . '::$' . $name;
105 if ( $ownerClass ) {
106 // Someone tried to access a normal non-public property. Try to behave like PHP would.
107 trigger_error( "Cannot access non-public property $qualifiedName", E_USER_ERROR );
108 } else {
109 // Non-existing property. Try to behave like PHP would.
110 $this->$name = $value;
111 }
112 }
113
114 /**
115 * Like property_exists but also check for non-visible private properties and returns which
116 * class in the inheritance chain declared the property.
117 * @param string $property
118 * @return string|bool Best guess for the class in which the property is defined. False if
119 * the object does not have such a property.
120 */
121 private function deprecationHelperGetPropertyOwner( $property ) {
122 // Returning false is a non-error path and should avoid slow checks like reflection.
123 // Use array cast hack instead.
124 $obfuscatedProps = array_keys( (array)$this );
125 $obfuscatedPropTail = "\0$property";
126 foreach ( $obfuscatedProps as $obfuscatedProp ) {
127 // private props are in the form \0<classname>\0<propname>
128 if ( strpos( $obfuscatedProp, $obfuscatedPropTail, 1 ) !== false ) {
129 $classname = substr( $obfuscatedProp, 1, -strlen( $obfuscatedPropTail ) );
130 if ( $classname === '*' ) {
131 // protected property; we didn't get the name, but we are on an error path
132 // now so it's fine to use reflection
133 return ( new ReflectionProperty( $this, $property ) )->getDeclaringClass()->getName();
134 }
135 return $classname;
136 }
137 }
138 return false;
139 }
140
141 }