DeprecationHelper.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. * Use this trait in classes which have properties for which public access
  24. * is deprecated. Set the list of properties in $deprecatedPublicProperties
  25. * and make the properties non-public. The trait will preserve public access
  26. * but issue deprecation warnings when it is needed.
  27. *
  28. * Example usage:
  29. * class Foo {
  30. * use DeprecationHelper;
  31. * protected $bar;
  32. * public function __construct() {
  33. * $this->deprecatePublicProperty( 'bar', '1.21', __CLASS__ );
  34. * }
  35. * }
  36. *
  37. * $foo = new Foo;
  38. * $foo->bar; // works but logs a warning
  39. *
  40. * Cannot be used with classes that have their own __get/__set methods.
  41. *
  42. * @since 1.32
  43. */
  44. trait DeprecationHelper {
  45. /**
  46. * List of deprecated properties, in <property name> => [<version>, <class>, <component>] format
  47. * where <version> is the MediaWiki version where the property got deprecated, <class> is the
  48. * the name of the class defining the property, <component> is the MediaWiki component
  49. * (extension, skin etc.) for use in the deprecation warning) or null if it is MediaWiki.
  50. * E.g. [ 'mNewRev' => [ '1.32', 'DifferenceEngine', null ]
  51. * @var string[]
  52. */
  53. protected $deprecatedPublicProperties = [];
  54. /**
  55. * Mark a property as deprecated. Only use this for properties that used to be public and only
  56. * call it in the constructor.
  57. * @param string $property The name of the property.
  58. * @param string $version MediaWiki version where the property became deprecated.
  59. * @param string|null $class The class which has the deprecated property. This can usually be
  60. * guessed, but PHP can get confused when both the parent class and the subclass use the
  61. * trait, so it should be specified in classes meant for subclassing.
  62. * @param string|null $component
  63. * @see wfDeprecated()
  64. */
  65. protected function deprecatePublicProperty(
  66. $property, $version, $class = null, $component = null
  67. ) {
  68. $this->deprecatedPublicProperties[$property] = [ $version, $class ?: get_class(), $component ];
  69. }
  70. public function __get( $name ) {
  71. if ( isset( $this->deprecatedPublicProperties[$name] ) ) {
  72. list( $version, $class, $component ) = $this->deprecatedPublicProperties[$name];
  73. $qualifiedName = $class . '::$' . $name;
  74. wfDeprecated( $qualifiedName, $version, $component, 3 );
  75. return $this->$name;
  76. }
  77. $qualifiedName = get_class() . '::$' . $name;
  78. if ( $this->deprecationHelperGetPropertyOwner( $name ) ) {
  79. // Someone tried to access a normal non-public property. Try to behave like PHP would.
  80. trigger_error( "Cannot access non-public property $qualifiedName", E_USER_ERROR );
  81. } else {
  82. // Non-existing property. Try to behave like PHP would.
  83. trigger_error( "Undefined property: $qualifiedName", E_USER_NOTICE );
  84. }
  85. return null;
  86. }
  87. public function __set( $name, $value ) {
  88. if ( isset( $this->deprecatedPublicProperties[$name] ) ) {
  89. list( $version, $class, $component ) = $this->deprecatedPublicProperties[$name];
  90. $qualifiedName = $class . '::$' . $name;
  91. wfDeprecated( $qualifiedName, $version, $component, 3 );
  92. $this->$name = $value;
  93. return;
  94. }
  95. $qualifiedName = get_class() . '::$' . $name;
  96. if ( $this->deprecationHelperGetPropertyOwner( $name ) ) {
  97. // Someone tried to access a normal non-public property. Try to behave like PHP would.
  98. trigger_error( "Cannot access non-public property $qualifiedName", E_USER_ERROR );
  99. } else {
  100. // Non-existing property. Try to behave like PHP would.
  101. $this->$name = $value;
  102. }
  103. }
  104. /**
  105. * Like property_exists but also check for non-visible private properties and returns which
  106. * class in the inheritance chain declared the property.
  107. * @param string $property
  108. * @return string|bool Best guess for the class in which the property is defined.
  109. */
  110. private function deprecationHelperGetPropertyOwner( $property ) {
  111. // Easy branch: check for protected property / private property of the current class.
  112. if ( property_exists( $this, $property ) ) {
  113. // The class name is not necessarily correct here but getting the correct class
  114. // name would be expensive, this will work most of the time and getting it
  115. // wrong is not a big deal.
  116. return __CLASS__;
  117. }
  118. // property_exists() returns false when the property does exist but is private (and not
  119. // defined by the current class, for some value of "current" that differs slightly
  120. // between engines).
  121. // Since PHP triggers an error on public access of non-public properties but happily
  122. // allows public access to undefined properties, we need to detect this case as well.
  123. // Reflection is slow so use array cast hack to check for that:
  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. // sanity; this shouldn't be possible as protected properties were handled earlier
  132. $classname = __CLASS__;
  133. }
  134. return $classname;
  135. }
  136. }
  137. return false;
  138. }
  139. }