ObjectHelpers.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * This file is part of the Nette Framework (https://nette.org)
  4. * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  5. */
  6. declare(strict_types=1);
  7. namespace Nette\Utils;
  8. use Nette;
  9. use Nette\MemberAccessException;
  10. /**
  11. * Nette\SmartObject helpers.
  12. * @internal
  13. */
  14. final class ObjectHelpers
  15. {
  16. use Nette\StaticClass;
  17. /**
  18. * @return never
  19. * @throws MemberAccessException
  20. */
  21. public static function strictGet(string $class, string $name): void
  22. {
  23. $rc = new \ReflectionClass($class);
  24. $hint = self::getSuggestion(array_merge(
  25. array_filter($rc->getProperties(\ReflectionProperty::IS_PUBLIC), function ($p) { return !$p->isStatic(); }),
  26. self::parseFullDoc($rc, '~^[ \t*]*@property(?:-read)?[ \t]+(?:\S+[ \t]+)??\$(\w+)~m')
  27. ), $name);
  28. throw new MemberAccessException("Cannot read an undeclared property $class::\$$name" . ($hint ? ", did you mean \$$hint?" : '.'));
  29. }
  30. /**
  31. * @return never
  32. * @throws MemberAccessException
  33. */
  34. public static function strictSet(string $class, string $name): void
  35. {
  36. $rc = new \ReflectionClass($class);
  37. $hint = self::getSuggestion(array_merge(
  38. array_filter($rc->getProperties(\ReflectionProperty::IS_PUBLIC), function ($p) { return !$p->isStatic(); }),
  39. self::parseFullDoc($rc, '~^[ \t*]*@property(?:-write)?[ \t]+(?:\S+[ \t]+)??\$(\w+)~m')
  40. ), $name);
  41. throw new MemberAccessException("Cannot write to an undeclared property $class::\$$name" . ($hint ? ", did you mean \$$hint?" : '.'));
  42. }
  43. /**
  44. * @return never
  45. * @throws MemberAccessException
  46. */
  47. public static function strictCall(string $class, string $method, array $additionalMethods = []): void
  48. {
  49. $trace = debug_backtrace(0, 3); // suppose this method is called from __call()
  50. $context = ($trace[1]['function'] ?? null) === '__call'
  51. ? ($trace[2]['class'] ?? null)
  52. : null;
  53. if ($context && is_a($class, $context, true) && method_exists($context, $method)) { // called parent::$method()
  54. $class = get_parent_class($context);
  55. }
  56. if (method_exists($class, $method)) { // insufficient visibility
  57. $rm = new \ReflectionMethod($class, $method);
  58. $visibility = $rm->isPrivate()
  59. ? 'private '
  60. : ($rm->isProtected() ? 'protected ' : '');
  61. throw new MemberAccessException("Call to {$visibility}method $class::$method() from " . ($context ? "scope $context." : 'global scope.'));
  62. } else {
  63. $hint = self::getSuggestion(array_merge(
  64. get_class_methods($class),
  65. self::parseFullDoc(new \ReflectionClass($class), '~^[ \t*]*@method[ \t]+(?:static[ \t]+)?(?:\S+[ \t]+)??(\w+)\(~m'),
  66. $additionalMethods
  67. ), $method);
  68. throw new MemberAccessException("Call to undefined method $class::$method()" . ($hint ? ", did you mean $hint()?" : '.'));
  69. }
  70. }
  71. /**
  72. * @return never
  73. * @throws MemberAccessException
  74. */
  75. public static function strictStaticCall(string $class, string $method): void
  76. {
  77. $trace = debug_backtrace(0, 3); // suppose this method is called from __callStatic()
  78. $context = ($trace[1]['function'] ?? null) === '__callStatic'
  79. ? ($trace[2]['class'] ?? null)
  80. : null;
  81. if ($context && is_a($class, $context, true) && method_exists($context, $method)) { // called parent::$method()
  82. $class = get_parent_class($context);
  83. }
  84. if (method_exists($class, $method)) { // insufficient visibility
  85. $rm = new \ReflectionMethod($class, $method);
  86. $visibility = $rm->isPrivate()
  87. ? 'private '
  88. : ($rm->isProtected() ? 'protected ' : '');
  89. throw new MemberAccessException("Call to {$visibility}method $class::$method() from " . ($context ? "scope $context." : 'global scope.'));
  90. } else {
  91. $hint = self::getSuggestion(
  92. array_filter((new \ReflectionClass($class))->getMethods(\ReflectionMethod::IS_PUBLIC), function ($m) { return $m->isStatic(); }),
  93. $method
  94. );
  95. throw new MemberAccessException("Call to undefined static method $class::$method()" . ($hint ? ", did you mean $hint()?" : '.'));
  96. }
  97. }
  98. /**
  99. * Returns array of magic properties defined by annotation @property.
  100. * @return array of [name => bit mask]
  101. * @internal
  102. */
  103. public static function getMagicProperties(string $class): array
  104. {
  105. static $cache;
  106. $props = &$cache[$class];
  107. if ($props !== null) {
  108. return $props;
  109. }
  110. $rc = new \ReflectionClass($class);
  111. preg_match_all(
  112. '~^ [ \t*]* @property(|-read|-write|-deprecated) [ \t]+ [^\s$]+ [ \t]+ \$ (\w+) ()~mx',
  113. (string) $rc->getDocComment(),
  114. $matches,
  115. PREG_SET_ORDER
  116. );
  117. $props = [];
  118. foreach ($matches as [, $type, $name]) {
  119. $uname = ucfirst($name);
  120. $write = $type !== '-read'
  121. && $rc->hasMethod($nm = 'set' . $uname)
  122. && ($rm = $rc->getMethod($nm))->name === $nm && !$rm->isPrivate() && !$rm->isStatic();
  123. $read = $type !== '-write'
  124. && ($rc->hasMethod($nm = 'get' . $uname) || $rc->hasMethod($nm = 'is' . $uname))
  125. && ($rm = $rc->getMethod($nm))->name === $nm && !$rm->isPrivate() && !$rm->isStatic();
  126. if ($read || $write) {
  127. $props[$name] = $read << 0 | ($nm[0] === 'g') << 1 | $rm->returnsReference() << 2 | $write << 3 | ($type === '-deprecated') << 4;
  128. }
  129. }
  130. foreach ($rc->getTraits() as $trait) {
  131. $props += self::getMagicProperties($trait->name);
  132. }
  133. if ($parent = get_parent_class($class)) {
  134. $props += self::getMagicProperties($parent);
  135. }
  136. return $props;
  137. }
  138. /**
  139. * Finds the best suggestion (for 8-bit encoding).
  140. * @param (\ReflectionFunctionAbstract|\ReflectionParameter|\ReflectionClass|\ReflectionProperty|string)[] $possibilities
  141. * @internal
  142. */
  143. public static function getSuggestion(array $possibilities, string $value): ?string
  144. {
  145. $norm = preg_replace($re = '#^(get|set|has|is|add)(?=[A-Z])#', '+', $value);
  146. $best = null;
  147. $min = (strlen($value) / 4 + 1) * 10 + .1;
  148. foreach (array_unique($possibilities, SORT_REGULAR) as $item) {
  149. $item = $item instanceof \Reflector ? $item->name : $item;
  150. if ($item !== $value && (
  151. ($len = levenshtein($item, $value, 10, 11, 10)) < $min
  152. || ($len = levenshtein(preg_replace($re, '*', $item), $norm, 10, 11, 10)) < $min
  153. )) {
  154. $min = $len;
  155. $best = $item;
  156. }
  157. }
  158. return $best;
  159. }
  160. private static function parseFullDoc(\ReflectionClass $rc, string $pattern): array
  161. {
  162. do {
  163. $doc[] = $rc->getDocComment();
  164. $traits = $rc->getTraits();
  165. while ($trait = array_pop($traits)) {
  166. $doc[] = $trait->getDocComment();
  167. $traits += $trait->getTraits();
  168. }
  169. } while ($rc = $rc->getParentClass());
  170. return preg_match_all($pattern, implode($doc), $m) ? $m[1] : [];
  171. }
  172. /**
  173. * Checks if the public non-static property exists.
  174. * @return bool|string returns 'event' if the property exists and has event like name
  175. * @internal
  176. */
  177. public static function hasProperty(string $class, string $name)
  178. {
  179. static $cache;
  180. $prop = &$cache[$class][$name];
  181. if ($prop === null) {
  182. $prop = false;
  183. try {
  184. $rp = new \ReflectionProperty($class, $name);
  185. if ($rp->isPublic() && !$rp->isStatic()) {
  186. $prop = $name >= 'onA' && $name < 'on_' ? 'event' : true;
  187. }
  188. } catch (\ReflectionException $e) {
  189. }
  190. }
  191. return $prop;
  192. }
  193. }