DeprecationHelper.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 ?: __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. $ownerClass = $this->deprecationHelperGetPropertyOwner( $name );
  78. $qualifiedName = ( $ownerClass ?: get_class( $this ) ) . '::$' . $name;
  79. if ( $ownerClass ) {
  80. // Someone tried to access a normal non-public property. Try to behave like PHP would.
  81. trigger_error( "Cannot access non-public property $qualifiedName", E_USER_ERROR );
  82. } else {
  83. // Non-existing property. Try to behave like PHP would.
  84. trigger_error( "Undefined property: $qualifiedName", E_USER_NOTICE );
  85. }
  86. return null;
  87. }
  88. public function __set( $name, $value ) {
  89. if ( isset( $this->deprecatedPublicProperties[$name] ) ) {
  90. list( $version, $class, $component ) = $this->deprecatedPublicProperties[$name];
  91. $qualifiedName = $class . '::$' . $name;
  92. wfDeprecated( $qualifiedName, $version, $component, 3 );
  93. $this->$name = $value;
  94. return;
  95. }
  96. $ownerClass = $this->deprecationHelperGetPropertyOwner( $name );
  97. $qualifiedName = ( $ownerClass ?: get_class( $this ) ) . '::$' . $name;
  98. if ( $ownerClass ) {
  99. // Someone tried to access a normal non-public property. Try to behave like PHP would.
  100. trigger_error( "Cannot access non-public property $qualifiedName", E_USER_ERROR );
  101. } else {
  102. // Non-existing property. Try to behave like PHP would.
  103. $this->$name = $value;
  104. }
  105. }
  106. /**
  107. * Like property_exists but also check for non-visible private properties and returns which
  108. * class in the inheritance chain declared the property.
  109. * @param string $property
  110. * @return string|bool Best guess for the class in which the property is defined. False if
  111. * the object does not have such a property.
  112. */
  113. private function deprecationHelperGetPropertyOwner( $property ) {
  114. // Returning false is a non-error path and should avoid slow checks like reflection.
  115. // Use array cast hack instead.
  116. $obfuscatedProps = array_keys( (array)$this );
  117. $obfuscatedPropTail = "\0$property";
  118. foreach ( $obfuscatedProps as $obfuscatedProp ) {
  119. // private props are in the form \0<classname>\0<propname>
  120. if ( strpos( $obfuscatedProp, $obfuscatedPropTail, 1 ) !== false ) {
  121. $classname = substr( $obfuscatedProp, 1, -strlen( $obfuscatedPropTail ) );
  122. if ( $classname === '*' ) {
  123. // protected property; we didn't get the name, but we are on an error path
  124. // now so it's fine to use reflection
  125. return ( new ReflectionProperty( $this, $property ) )->getDeclaringClass()->getName();
  126. }
  127. return $classname;
  128. }
  129. }
  130. return false;
  131. }
  132. }