PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/hphp/runtime/ext/reflection/ext_reflection-classes.php

http://github.com/facebook/hiphop-php
PHP | 1389 lines | 659 code | 87 blank | 643 comment | 41 complexity | 3a9245f9e6fb12af8e290a83204652fb MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. <?hh // partial
  2. namespace {
  3. ///////////////////////////////////////////////////////////////////////////////
  4. // helpers
  5. // This doc comment block generated by idl/sysdoc.php
  6. /**
  7. * ( excerpt from http://php.net/manual/en/class.reflector.php )
  8. *
  9. * Reflector is an interface implemented by all exportable Reflection
  10. * classes.
  11. *
  12. */
  13. interface Reflector {
  14. // This doc comment block generated by idl/sysdoc.php
  15. /**
  16. * ( excerpt from http://php.net/manual/en/reflector.tostring.php )
  17. *
  18. * To string. Warning: This function is currently not documented; only its
  19. * argument list is available.
  20. *
  21. */
  22. <<__Rx, __MaybeMutable>>
  23. public function __toString();
  24. }
  25. trait ReflectionLegacyAttribute {
  26. <<__Rx, __MaybeMutable, __ProvenanceSkipFrame>>
  27. final public function getAttributes(): darray<arraykey, varray<mixed>> {
  28. $denamespaced = darray[];
  29. foreach ($this->getAttributesNamespaced() as $name => $args) {
  30. $pos = strrpos($name, '\\');
  31. $name = ($pos === false) ? $name : substr($name, $pos + 1);
  32. $denamespaced[$name] = $args;
  33. }
  34. return $denamespaced;
  35. }
  36. <<__Rx, __MaybeMutable>>
  37. final public function getAttribute(string $name): ?varray<mixed> {
  38. return hphp_array_idx($this->getAttributes(), $name, null);
  39. }
  40. }
  41. trait ReflectionTypedAttribute {
  42. <<__Rx, __MaybeMutable>>
  43. final public function getAttributeClass(classname $c) {
  44. $attrs = $this->getAttributesNamespaced();
  45. $args = hphp_array_idx($attrs, $c, null);
  46. if ($args === null) {
  47. return null;
  48. } else {
  49. return new $c(...$args);
  50. }
  51. }
  52. }
  53. // This doc comment block generated by idl/sysdoc.php
  54. /**
  55. * ( excerpt from http://php.net/manual/en/class.reflectionexception.php )
  56. *
  57. * The ReflectionException class.
  58. *
  59. */
  60. class ReflectionException extends Exception {
  61. }
  62. ///////////////////////////////////////////////////////////////////////////////
  63. // parameter
  64. // This doc comment block generated by idl/sysdoc.php
  65. /**
  66. * ( excerpt from http://php.net/manual/en/class.reflectionparameter.php )
  67. *
  68. * The ReflectionParameter class retrieves information about function's or
  69. * method's parameters.
  70. *
  71. * To introspect function parameters, first create an instance of the
  72. * ReflectionFunction or ReflectionMethod classes and then use their
  73. * ReflectionFunctionAbstract::getParameters() method to retrieve an array
  74. * of parameters.
  75. *
  76. */
  77. class ReflectionParameter implements Reflector {
  78. public $info;
  79. public $name;
  80. public $paramTypeInfo;
  81. // This doc comment block generated by idl/sysdoc.php
  82. /**
  83. * ( excerpt from
  84. * http://php.net/manual/en/reflectionparameter.construct.php )
  85. *
  86. * Constructs a ReflectionParameter class. Warning: This function is
  87. * currently not documented; only its argument list is available.
  88. *
  89. * @func mixed The function to reflect parameters from.
  90. * @param mixed The parameter.
  91. *
  92. * @return mixed No value is returned.
  93. */
  94. <<__Rx>>
  95. public function __construct($func, $param) {
  96. if (is_null($func) && is_null($param)) {
  97. return;
  98. }
  99. if ($func is Closure) {
  100. $params = (new ReflectionFunction($func))->getParameters();
  101. } else if (is_string($func)) {
  102. $double_colon = strpos($func, "::");
  103. if ($double_colon === false) {
  104. $params = (new ReflectionFunction($func))->getParameters();
  105. } else {
  106. $class = substr($func, 0, $double_colon);
  107. $method = substr($func, $double_colon + 2);
  108. $params = (new ReflectionMethod($class, $method))->getParameters();
  109. }
  110. } else if (is_array($func)) {
  111. $params = (new ReflectionMethod($func[0], $func[1]))->getParameters();
  112. } else {
  113. throw new ReflectionException(
  114. "The parameter class is expected to be either a string, " .
  115. "an array(class, method) or a callable object"
  116. );
  117. }
  118. if (is_string($param)) {
  119. foreach ($params as $p) {
  120. if ($p->name === $param) {
  121. $this->info = $p->info;
  122. $this->name = $p->name;
  123. break;
  124. }
  125. }
  126. if ($this->info === null) {
  127. throw new ReflectionException("The parameter specified by its name " .
  128. "could not be found");
  129. }
  130. } else if (is_int($param)) {
  131. if ($param < 0 || $param >= count($params)) {
  132. throw new ReflectionException("The parameter specified by its offset " .
  133. "could not be found");
  134. }
  135. $p = $params[$param];
  136. $this->info = $p->info;
  137. $this->name = $p->name;
  138. } else {
  139. throw new ReflectionException(
  140. "The parameter value is expected to be either a string or integer"
  141. );
  142. }
  143. }
  144. // This doc comment block generated by idl/sysdoc.php
  145. /**
  146. * ( excerpt from http://php.net/manual/en/reflectionparameter.tostring.php
  147. * )
  148. *
  149. * To string. Warning: This function is currently not documented; only its
  150. * argument list is available.
  151. *
  152. */
  153. <<__Rx, __MaybeMutable>>
  154. public function toString() {
  155. $type = $this->getTypeText();
  156. if ($type !== '') {
  157. if ($this->isOptional() && $this->getDefaultValue() === null) {
  158. $type .= ' or NULL';
  159. }
  160. $type .= ' ';
  161. }
  162. $out = 'Parameter #'.$this->getPosition().' [ ';
  163. $inout = '';
  164. $reference = '';
  165. if ($this->isInOut()) {
  166. $inout = 'inout ';
  167. } else if ($this->isPassedByReference()) {
  168. $reference = '&';
  169. }
  170. $param = $inout.$type.$reference.'$'.$this->getName();
  171. if ($this->isOptional()) {
  172. $default = var_export($this->getDefaultValue(), true);
  173. $out .= '<optional> '.$param.' = '.$default;
  174. } else {
  175. $out .= '<required> '.$param;
  176. }
  177. $out .= ' ]';
  178. return $out;
  179. }
  180. <<__Rx, __MaybeMutable>>
  181. public function __toString() {
  182. return $this->toString();
  183. }
  184. // Prevent cloning
  185. final public function __clone() {
  186. throw new BadMethodCallException(
  187. 'Trying to clone an uncloneable object of class ReflectionParameter'
  188. );
  189. }
  190. // This doc comment block generated by idl/sysdoc.php
  191. /**
  192. * ( excerpt from http://php.net/manual/en/reflectionparameter.export.php )
  193. *
  194. * Exports. Warning: This function is currently not documented; only its
  195. * argument list is available.
  196. *
  197. * @func mixed The function name.
  198. * @param mixed The parameter name.
  199. * @ret mixed Setting to TRUE will return the export, as opposed
  200. * to emitting it. Setting to FALSE (the default) will
  201. * do the opposite.
  202. *
  203. * @return mixed The exported reflection.
  204. */
  205. public static function export($func, $param, $ret=false) {
  206. $obj = new ReflectionParameter($func, $param);
  207. $str = (string)$obj;
  208. if ($ret) {
  209. return $str;
  210. }
  211. print $str;
  212. }
  213. // This doc comment block generated by idl/sysdoc.php
  214. /**
  215. * ( excerpt from http://php.net/manual/en/reflectionparameter.getname.php
  216. * )
  217. *
  218. * Gets the name of the parameter.
  219. *
  220. * @return mixed The name of the reflected parameter.
  221. */
  222. <<__Rx, __MaybeMutable>>
  223. public function getName() {
  224. return $this->info['name'];
  225. }
  226. // This doc comment block generated by idl/sysdoc.php
  227. /**
  228. * ( excerpt from
  229. * http://php.net/manual/en/reflectionparameter.ispassedbyreference.php )
  230. *
  231. * Checks if the parameter is passed in by reference. Warning: This
  232. * function is currently not documented; only its argument list is
  233. * available.
  234. *
  235. * @return mixed TRUE if the parameter is passed in by reference,
  236. * otherwise FALSE
  237. */
  238. <<__Rx, __MaybeMutable>>
  239. public function isPassedByReference() {
  240. // FIXME: backward compatibility hack for places that were checking for
  241. // inout by looking at reffy wrappers.
  242. return $this->isInOut();
  243. }
  244. // This doc comment block generated by idl/sysdoc.php
  245. /**
  246. * ( excerpt from
  247. * http://php.net/manual/en/reflectionparameter.canbepassedbyvalue.php )
  248. *
  249. * Returns whether this parameter can be passed by value. Warning: This
  250. * function is currently not documented; only its argument list is
  251. * available.
  252. *
  253. * @return mixed Returns TRUE if the parameter can be passed by value,
  254. * FALSE otherwise. Returns NULL in case of an error.
  255. */
  256. <<__Rx, __MaybeMutable>>
  257. public function canBePassedByValue() {
  258. // FIXME: backward compatibility hack for places that were checking for
  259. // inout by looking at reffy wrappers.
  260. return !$this->isInOut();
  261. }
  262. /**
  263. * Checks if this is an inout parameter.
  264. *
  265. * @return bool TRUE if the parameter is inout, otherwise FALSE
  266. */
  267. <<__Rx, __MaybeMutable>>
  268. public function isInOut() {
  269. return ($this->info['inout'] ?? false);
  270. }
  271. // This doc comment block generated by idl/sysdoc.php
  272. /**
  273. * ( excerpt from
  274. * http://php.net/manual/en/reflectionparameter.getdeclaringclass.php )
  275. *
  276. * Gets the declaring class. Warning: This function is currently not
  277. * documented; only its argument list is available.
  278. *
  279. * @return mixed A ReflectionClass object.
  280. */
  281. <<__Rx, __MaybeMutable>>
  282. public function getDeclaringClass() {
  283. if (!($this->info['class'] ?? false)) {
  284. return null;
  285. }
  286. return new ReflectionClass($this->info['class']);
  287. }
  288. // This doc comment block generated by idl/sysdoc.php
  289. /**
  290. * ( excerpt from
  291. * http://php.net/manual/en/reflectionparameter.getdeclaringfunction.php )
  292. *
  293. * Gets the declaring function. Warning: This function is currently not
  294. * documented; only its argument list is available.
  295. *
  296. * @return mixed A ReflectionFunction object.
  297. */
  298. <<__Rx, __MaybeMutable>>
  299. public function getDeclaringFunction() {
  300. if (!($this->info['class'] ?? false)) {
  301. return new ReflectionFunction($this->info['function']);
  302. }
  303. return new ReflectionMethod($this->info['class'], $this->info['function']);
  304. }
  305. // This doc comment block generated by idl/sysdoc.php
  306. /**
  307. * ( excerpt from http://php.net/manual/en/reflectionparameter.getclass.php
  308. * )
  309. *
  310. * Gets a class. Warning: This function is currently not documented; only
  311. * its argument list is available.
  312. *
  313. * @return mixed A ReflectionClass object.
  314. */
  315. <<__Rx, __MaybeMutable>>
  316. public function getClass() {
  317. if (!($this->info['type'] ?? false)) {
  318. return null;
  319. }
  320. $ltype = strtolower($this->info['type']);
  321. $nonClassTypehints = darray[
  322. 'hh\\bool' => 1,
  323. 'hh\\int' => 1,
  324. 'hh\\float' => 1,
  325. 'hh\\num' => 1,
  326. 'hh\\string' => 1,
  327. 'hh\\resource' => 1,
  328. 'hh\\mixed' => 1,
  329. 'hh\\void' => 1,
  330. 'hh\\this' => 1,
  331. 'hh\\arraykey' => 1,
  332. 'hh\\varray' => 1,
  333. 'hh\\darray' => 1,
  334. 'hh\\varray_or_darray' => 1,
  335. 'hh\\arraylike' => 1,
  336. 'hh\\vec' => 1,
  337. 'hh\\dict' => 1,
  338. 'hh\\keyset' => 1,
  339. 'array' => 1,
  340. 'callable' => 1,
  341. ];
  342. if (isset($nonClassTypehints[$ltype])) {
  343. return null;
  344. }
  345. if ($ltype === "self" && ($this->info['class'] ?? false)) {
  346. return $this->getDeclaringClass();
  347. }
  348. return new ReflectionClass($this->info['type']);
  349. }
  350. <<__Rx, __MaybeMutable>>
  351. public function getTypehintText() {
  352. if (isset($this->info['type'])) {
  353. if ($this->info['type'] === 'self' && ($this->info['class'] ?? false)) {
  354. return $this->info['class'];
  355. }
  356. return $this->info['type'];
  357. }
  358. return '';
  359. }
  360. <<__Rx, __MaybeMutable>>
  361. public function getTypeText() {
  362. return isset($this->info['type_hint']) ? $this->info['type_hint'] : '';
  363. }
  364. // This doc comment block generated by idl/sysdoc.php
  365. /**
  366. * ( excerpt from http://php.net/manual/en/reflectionparameter.isarray.php
  367. * )
  368. *
  369. * Checks if the parameter expects an array.
  370. *
  371. * @return mixed TRUE if an array is expected, FALSE otherwise.
  372. */
  373. <<__Rx, __MaybeMutable>>
  374. public function isArray() {
  375. return $this->info['type'] == 'array';
  376. }
  377. // This doc comment block generated by idl/sysdoc.php
  378. /**
  379. * ( excerpt from
  380. * http://php.net/manual/en/reflectionparameter.allowsnull.php )
  381. *
  382. * Checks whether the parameter allows NULL. Warning: This function is
  383. * currently not documented; only its argument list is available.
  384. *
  385. * @return mixed TRUE if NULL is allowed, otherwise FALSE
  386. */
  387. <<__Rx, __MaybeMutable>>
  388. public function allowsNull() {
  389. return isset($this->info['nullable']);
  390. }
  391. // This doc comment block generated by idl/sysdoc.php
  392. /**
  393. * ( excerpt from
  394. * http://php.net/manual/en/reflectionparameter.isoptional.php )
  395. *
  396. * Checks if the parameter is optional.
  397. *
  398. * @return bool TRUE if the parameter is optional, otherwise FALSE
  399. */
  400. <<__Rx, __MaybeMutable>>
  401. public function isOptional(): bool {
  402. return ($this->info['is_optional'] ?? false);
  403. }
  404. /**
  405. * Checks if the parameter is variadic.
  406. *
  407. * @return bool TRUE if the parameter is variadic, otherwise FALSE
  408. */
  409. <<__Rx, __MaybeMutable>>
  410. public function isVariadic(): bool {
  411. return ($this->info['is_variadic'] ?? false);
  412. }
  413. // This doc comment block generated by idl/sysdoc.php
  414. /**
  415. * ( excerpt from
  416. * http://php.net/manual/en/reflectionparameter.isdefaultvalueavailable.php
  417. * )
  418. *
  419. * Checks if a default value for the parameter is available.
  420. *
  421. * @return mixed TRUE if a default value is available, otherwise
  422. * FALSE
  423. */
  424. <<__Rx, __MaybeMutable>>
  425. public function isDefaultValueAvailable() {
  426. if (!array_key_exists('default', $this->info)) {
  427. return false;
  428. }
  429. $defaultValue = $this->info['default'];
  430. return (!$defaultValue is stdClass);
  431. }
  432. /**
  433. * ( excerpt from
  434. * http://php.net/manual/en/reflectionparameter.isdefaultvalueconstant.php )
  435. *
  436. * Returns TRUE if the default value is constant, FALSE if it is not or
  437. * NULL on failure.
  438. *
  439. * @return bool If parameters default value is constant, or NULL if
  440. * a default value does not exist.
  441. */
  442. <<__Rx, __MaybeMutable>>
  443. public function isDefaultValueConstant(): ?bool {
  444. // No default value, return null.
  445. if (!$this->isDefaultValueAvailable()) {
  446. return null;
  447. }
  448. // If there is a default value, then there has to be defaultText
  449. // If the defaultText is a class constant, then classname will be
  450. // associated with the text, so you can use defined for that as well.
  451. // Exception here -- if this is in a closure, then it will return false
  452. // PHP fatals on closures and getting default values, we will just
  453. // return false.
  454. return defined($this->info['defaultText']);
  455. }
  456. // This doc comment block generated by idl/sysdoc.php
  457. /**
  458. * ( excerpt from
  459. * http://php.net/manual/en/reflectionparameter.getdefaultvalue.php )
  460. *
  461. * Gets the default value of the parameter for a user-defined function or
  462. * method. If the parameter is not optional a ReflectionException will be
  463. * thrown.
  464. *
  465. * @return mixed The parameters default value.
  466. */
  467. <<__Rx, __MaybeMutable>>
  468. public function getDefaultValue() {
  469. if (!array_key_exists('default', $this->info)) {
  470. throw new ReflectionException('Parameter is not optional');
  471. }
  472. $defaultValue = $this->info['default'];
  473. if ($defaultValue is stdclass) {
  474. throw new ReflectionException($defaultValue->msg);
  475. }
  476. return $defaultValue;
  477. }
  478. /**
  479. * This is an HHVM only function that gets the raw text associated with
  480. * a default parameter.
  481. *
  482. * For example, for:
  483. * function foo($x = FOO*FOO)
  484. *
  485. * "FOO*FOO" is returned.
  486. *
  487. * getDefaultValue() will return the result of FOO*FOO.
  488. *
  489. * @return string The raw text of a default value, or empty if it does not
  490. * exist.
  491. */
  492. <<__Rx, __MaybeMutable>>
  493. public function getDefaultValueText() {
  494. if (array_key_exists('defaultText', $this->info)) {
  495. return $this->info['defaultText'];
  496. }
  497. return '';
  498. }
  499. /**
  500. * ( excerpt from
  501. * php.net/manual/en/reflectionparameter.getdefaultvalueconstantname.php
  502. * )
  503. *
  504. * Returns the default value's constant name if default value is constant or
  505. * null
  506. *
  507. * @return mixed Returns string on success or NULL on failure.
  508. */
  509. <<__Rx, __MaybeMutable>>
  510. public function getDefaultValueConstantName() {
  511. if ($this->isDefaultValueConstant()) {
  512. return $this->info['defaultText'];
  513. }
  514. return null;
  515. }
  516. // This doc comment block generated by idl/sysdoc.php
  517. /**
  518. * ( excerpt from
  519. * http://php.net/manual/en/reflectionparameter.getposition.php )
  520. *
  521. * Gets the position of the parameter.
  522. *
  523. * @return mixed The position of the parameter, left to right,
  524. * starting at position #0.
  525. */
  526. <<__Rx, __MaybeMutable>>
  527. public function getPosition() {
  528. return $this->info['index'];
  529. }
  530. use ReflectionTypedAttribute;
  531. <<__Rx, __MaybeMutable>>
  532. final public function getAttributesNamespaced() {
  533. return $this->info['attributes'];
  534. }
  535. use ReflectionLegacyAttribute;
  536. // This doc comment block generated by idl/sysdoc.php
  537. /**
  538. * ( excerpt from
  539. * http://php.net/manual/en/reflectionparameter.iscallable.php )
  540. *
  541. * Warning: This function is currently not documented; only its argument
  542. * list is available.
  543. *
  544. * @return mixed Returns TRUE if the parameter is callable, FALSE if
  545. * it is not or NULL on failure.
  546. */
  547. <<__Rx, __MaybeMutable>>
  548. public function isCallable() {
  549. return $this->getTypeText() === 'callable';
  550. }
  551. /**
  552. * ( excerpt from
  553. * http://php.net/manual/en/reflectionparameter.hastype.php )
  554. *
  555. * Checks if the parameter has a type associated with it.
  556. *
  557. * @return bool TRUE if a type is specified, FALSE otherwise.
  558. */
  559. <<__Rx, __MaybeMutable>>
  560. public function hasType(): bool {
  561. return $this->info['type_hint'] !== '';
  562. }
  563. /**
  564. * ( excerpt from
  565. * http://php.net/manual/en/reflectionparameter.gettype.php )
  566. *
  567. * Gets the associated type of a parameter.
  568. *
  569. * @return ?ReflectionType Returns a ReflectionType object if a
  570. * parameter type is specified, NULL otherwise.
  571. */
  572. <<__Rx, __MaybeMutable>>
  573. public function getType(): ?ReflectionType {
  574. if ($this->hasType()) {
  575. return new ReflectionType(
  576. $this,
  577. darray[
  578. 'name' => $this->info['type_hint'],
  579. 'nullable' => $this->info['type_hint_nullable'],
  580. 'builtin' => $this->info['type_hint_builtin'],
  581. ]
  582. );
  583. }
  584. return null;
  585. }
  586. }
  587. ///////////////////////////////////////////////////////////////////////////////
  588. // property
  589. // This doc comment block generated by idl/sysdoc.php
  590. /**
  591. * ( excerpt from http://php.net/manual/en/class.reflectionproperty.php )
  592. *
  593. * The ReflectionProperty class reports information about classes
  594. * properties.
  595. *
  596. */
  597. <<__NativeData('ReflectionPropHandle')>>
  598. class ReflectionProperty implements Reflector {
  599. const IS_STATIC = 1;
  600. const IS_PUBLIC = 256;
  601. const IS_PROTECTED = 512;
  602. const IS_PRIVATE = 1024;
  603. public $name;
  604. public $class;
  605. private $forceAccessible = false;
  606. // This doc comment block generated by idl/sysdoc.php
  607. /**
  608. * ( excerpt from http://php.net/manual/en/reflectionproperty.construct.php
  609. * )
  610. *
  611. * Warning: This function is currently not documented; only its argument
  612. * list is available.
  613. *
  614. * @cls mixed The class name, that contains the property.
  615. * @name mixed The name of the property being reflected.
  616. *
  617. * @return mixed No value is returned.
  618. */
  619. <<__Native, __Rx>>
  620. public function __construct(mixed $cls, string $name): void;
  621. // This doc comment block generated by idl/sysdoc.php
  622. /**
  623. * ( excerpt from http://php.net/manual/en/reflectionproperty.tostring.php
  624. * )
  625. *
  626. * To string. Warning: This function is currently not documented; only its
  627. * argument list is available.
  628. *
  629. */
  630. <<__Rx, __MaybeMutable>>
  631. public function __toString() {
  632. if ($this->isStatic()) {
  633. $def = '';
  634. } elseif ($this->isDefault()) {
  635. $def = '<default> ';
  636. } else {
  637. $def = '<dynamic> ';
  638. }
  639. // FIXME: Implicit public
  640. if ($this->isPrivate()) {
  641. $modifiers = 'private';
  642. } elseif ($this->isProtected()) {
  643. $modifiers = 'protected';
  644. } else {
  645. $modifiers = 'public';
  646. }
  647. if ($this->isStatic()) {
  648. $modifiers .= ' static';
  649. }
  650. return "Property [ {$def}{$modifiers} \${$this->getName()} ]\n";
  651. }
  652. // Prevent cloning
  653. final public function __clone() {
  654. throw new BadMethodCallException(
  655. 'Trying to clone an uncloneable object of class ReflectionProperty'
  656. );
  657. }
  658. // This doc comment block generated by idl/sysdoc.php
  659. /**
  660. * ( excerpt from http://php.net/manual/en/reflectionproperty.export.php )
  661. *
  662. * Exports a reflection. Warning: This function is currently not
  663. * documented; only its argument list is available.
  664. *
  665. * @cls mixed The reflection to export.
  666. * @name mixed The property name.
  667. * @ret mixed Setting to TRUE will return the export, as opposed
  668. * to emitting it. Setting to FALSE (the default) will
  669. * do the opposite.
  670. */
  671. public static function export($cls, $name, $ret=false) {
  672. $obj = new self($cls, $name);
  673. $str = (string) $obj;
  674. if ($ret) {
  675. return $str;
  676. }
  677. print $str;
  678. }
  679. // This doc comment block generated by idl/sysdoc.php
  680. /**
  681. * ( excerpt from http://php.net/manual/en/reflectionproperty.getname.php )
  682. *
  683. * Gets the properties name. Warning: This function is currently not
  684. * documented; only its argument list is available.
  685. *
  686. * @return mixed The name of the reflected property.
  687. */
  688. <<__Rx, __MaybeMutable>>
  689. public function getName() {
  690. return $this->name;
  691. }
  692. // This doc comment block generated by idl/sysdoc.php
  693. /**
  694. * ( excerpt from http://php.net/manual/en/reflectionproperty.ispublic.php
  695. * )
  696. *
  697. * Checks whether the property is public.
  698. *
  699. * @return mixed TRUE if the property is public, FALSE otherwise.
  700. */
  701. <<__Native, __Rx, __MaybeMutable>>
  702. public function isPublic(): bool;
  703. // This doc comment block generated by idl/sysdoc.php
  704. /**
  705. * ( excerpt from http://php.net/manual/en/reflectionproperty.isprivate.php
  706. * )
  707. *
  708. * Checks whether the property is private.
  709. *
  710. * @return mixed TRUE if the property is private, FALSE otherwise.
  711. */
  712. <<__Native, __Rx, __MaybeMutable>>
  713. public function isPrivate(): bool;
  714. // This doc comment block generated by idl/sysdoc.php
  715. /**
  716. * ( excerpt from
  717. * http://php.net/manual/en/reflectionproperty.isprotected.php )
  718. *
  719. * Checks whether the property is protected.
  720. *
  721. * @return mixed TRUE if the property is protected, FALSE otherwise.
  722. */
  723. <<__Native, __Rx, __MaybeMutable>>
  724. public function isProtected(): bool;
  725. // This doc comment block generated by idl/sysdoc.php
  726. /**
  727. * ( excerpt from http://php.net/manual/en/reflectionproperty.isstatic.php
  728. * )
  729. *
  730. * Checks whether the property is static.
  731. *
  732. * @return mixed TRUE if the property is static, FALSE otherwise.
  733. */
  734. <<__Native, __Rx, __MaybeMutable>>
  735. public function isStatic(): bool;
  736. // This doc comment block generated by idl/sysdoc.php
  737. /**
  738. * ( excerpt from http://php.net/manual/en/reflectionproperty.isdefault.php
  739. * )
  740. *
  741. * Checks whether the property is the default.
  742. *
  743. * @return mixed TRUE if the property was declared at compile-time,
  744. * or FALSE if it was created at run-time.
  745. */
  746. <<__Native, __Rx, __MaybeMutable>>
  747. public function isDefault(): bool;
  748. // This doc comment block generated by idl/sysdoc.php
  749. /**
  750. * ( excerpt from
  751. * http://php.net/manual/en/reflectionproperty.setaccessible.php )
  752. *
  753. * Sets a property to be accessible. For example, it may allow protected
  754. * and private properties to be accessed.
  755. *
  756. * @accessible mixed TRUE to allow accessibility, or FALSE.
  757. *
  758. * @return mixed No value is returned.
  759. */
  760. public function setAccessible($accessible) {
  761. $this->forceAccessible = $accessible;
  762. }
  763. // This doc comment block generated by idl/sysdoc.php
  764. /**
  765. * ( excerpt from
  766. * http://php.net/manual/en/reflectionproperty.getmodifiers.php )
  767. *
  768. * Gets the modifiers. Warning: This function is currently not documented;
  769. * only its argument list is available.
  770. *
  771. * @return mixed A numeric representation of the modifiers.
  772. */
  773. <<__Native, __Rx, __MaybeMutable>>
  774. public function getModifiers(): int;
  775. // This doc comment block generated by idl/sysdoc.php
  776. /**
  777. * ( excerpt from http://php.net/manual/en/reflectionproperty.getvalue.php
  778. * )
  779. *
  780. * Gets the properties value.
  781. *
  782. * @obj mixed If the property is non-static an object must be
  783. * provided to fetch the property from. If you want to
  784. * fetch the default property without providing an
  785. * object use ReflectionClass::getDefaultProperties()
  786. * instead.
  787. *
  788. * @return mixed The current value of the property.
  789. */
  790. public function getValue($obj = null) {
  791. if ($this->isStatic()) {
  792. return hphp_get_static_property(
  793. $this->class,
  794. $this->name,
  795. $this->forceAccessible
  796. );
  797. }
  798. if (!is_object($obj)) {
  799. trigger_error('ReflectionProperty::getValue() expects parameter 1'
  800. . ' to be object, ' . gettype($obj) . ' given', E_WARNING);
  801. return null;
  802. }
  803. return hphp_get_property(
  804. $obj,
  805. $this->forceAccessible ? $this->class : '',
  806. $this->name
  807. );
  808. }
  809. // This doc comment block generated by idl/sysdoc.php
  810. /**
  811. * ( excerpt from http://php.net/manual/en/reflectionproperty.setvalue.php
  812. * )
  813. *
  814. * Sets (changes) the property's value.
  815. *
  816. * @obj mixed If the property is non-static an object must be
  817. * provided to change the property on. If the property
  818. * is static this parameter is left out and only value
  819. * needs to be provided.
  820. * @value mixed The new value.
  821. *
  822. * @return mixed No value is returned.
  823. */
  824. public function setValue(mixed ...$args) {
  825. if (!$this->isAccessible()) {
  826. throw new ReflectionException(
  827. "Cannot access non-public member " . $this->class .
  828. "::" . $this->getName()
  829. );
  830. }
  831. switch (count($args)) {
  832. case 0:
  833. $value = null;
  834. $obj = null;
  835. break;
  836. case 1:
  837. $value = $args[0];
  838. $obj = null;
  839. break;
  840. default:
  841. $value = $args[1];
  842. $obj = $args[0];
  843. break;
  844. }
  845. if ($this->isStatic()) {
  846. hphp_set_static_property(
  847. $this->class,
  848. $this->name,
  849. $value,
  850. $this->forceAccessible
  851. );
  852. } else {
  853. if (count($args) != 2) {
  854. trigger_error('ReflectionProperty::setValue() expects exactly 2'.
  855. ' parameters, ' . count($args) . ' given', E_WARNING);
  856. return null;
  857. }
  858. if (!is_object($obj)) {
  859. trigger_error('ReflectionProperty::setValue() expects parameter 1'
  860. . ' to be object, ' . gettype($obj) . ' given', E_WARNING);
  861. return null;
  862. }
  863. hphp_set_property(
  864. $obj,
  865. $this->forceAccessible ? $this->class : '',
  866. $this->name,
  867. $value
  868. );
  869. }
  870. }
  871. // This doc comment block generated by idl/sysdoc.php
  872. /**
  873. * ( excerpt from
  874. * http://php.net/manual/en/reflectionproperty.getdeclaringclass.php )
  875. *
  876. * Gets the declaring class. Warning: This function is currently not
  877. * documented; only its argument list is available.
  878. *
  879. * @return mixed A ReflectionClass object.
  880. */
  881. <<__Rx, __MaybeMutable>>
  882. public function getDeclaringClass() {
  883. return new ReflectionClass($this->class);
  884. }
  885. // This doc comment block generated by idl/sysdoc.php
  886. /**
  887. * ( excerpt from
  888. * http://php.net/manual/en/reflectionproperty.getdoccomment.php )
  889. *
  890. * Gets the doc comment. Warning: This function is currently not
  891. * documented; only its argument list is available.
  892. *
  893. * @return mixed The doc comment.
  894. */
  895. <<__Native, __Rx, __MaybeMutable>>
  896. public function getDocComment(): mixed;
  897. <<__Native, __Rx, __MaybeMutable>>
  898. public function getTypeText(): string;
  899. <<__Rx, __MaybeMutable>>
  900. private function isAccessible() {
  901. return ($this->isPublic() || $this->forceAccessible);
  902. }
  903. /**
  904. * Get the value from the property declaration, which is what's set on the
  905. * property before we enter the constructor. If the property is dynamic,
  906. * you will get null. If the property is static and can't be initialized
  907. * statically, then you will get null instead of the correct value, so do
  908. * not rely on this API for static properties.
  909. */
  910. <<__Native, __Rx, __MaybeMutable>>
  911. public function getDefaultValue(): mixed;
  912. /**
  913. * Get the user defined attributes from the property declaration.
  914. */
  915. <<__Native, __Rx, __MaybeMutable>>
  916. public function getAttributesNamespaced(): dict<string, mixed>;
  917. use ReflectionLegacyAttribute;
  918. }
  919. ///////////////////////////////////////////////////////////////////////////////
  920. // extension
  921. // This doc comment block generated by idl/sysdoc.php
  922. /**
  923. * ( excerpt from http://php.net/manual/en/class.reflectionextension.php )
  924. *
  925. * The ReflectionExtension class reports information about an extension.
  926. *
  927. */
  928. class ReflectionExtension implements Reflector {
  929. private $info;
  930. // $name is the userland property; Using this for initial construction
  931. // only as the "name" property is implemented via native property handler
  932. // (NPH) as a read only propety.
  933. private $__name;
  934. // This doc comment block generated by idl/sysdoc.php
  935. /**
  936. * ( excerpt from
  937. * http://php.net/manual/en/reflectionextension.construct.php )
  938. *
  939. * Construct a ReflectionExtension object.
  940. *
  941. * @name mixed Name of the extension.
  942. *
  943. * @return mixed A ReflectionExtension object.
  944. */
  945. <<__Rx>>
  946. public function __construct($name) {
  947. $this->info = hphp_get_extension_info($name);
  948. $this->__name = $name;
  949. }
  950. // This doc comment block generated by idl/sysdoc.php
  951. /**
  952. * ( excerpt from http://php.net/manual/en/reflectionextension.tostring.php
  953. * )
  954. *
  955. * Exports a reflected extension and returns it as a string. This is the
  956. * same as the ReflectionExtension::export() with the return set to TRUE.
  957. *
  958. * @return mixed Returns the exported extension as a string, in the
  959. * same way as the ReflectionExtension::export().
  960. */
  961. <<__Rx, __MaybeMutable>>
  962. public function __toString() {
  963. /* HHVM extensions don't (currently) track what consts/ini/funcs/classes
  964. * are associated with them (nor do they track a unique number).
  965. * Provide a placeholder string with the data we do have pending
  966. * changes to the Extension registry.
  967. */
  968. return "Extension [ <persistent> extension #0 {$this->getName()} " .
  969. "version {$this->getVersion()} \{\}\n";
  970. }
  971. // Prevent cloning
  972. final public function __clone() {
  973. throw new BadMethodCallException(
  974. 'Trying to clone an uncloneable object of class ReflectionExtension'
  975. );
  976. }
  977. // This doc comment block generated by idl/sysdoc.php
  978. /**
  979. * ( excerpt from http://php.net/manual/en/reflectionextension.export.php )
  980. *
  981. * Exports a reflected extension. The output format of this function is
  982. * the same as the CLI argument --re [extension].
  983. *
  984. * @name mixed The reflection to export.
  985. * @ret mixed Setting to TRUE will return the export, as opposed
  986. * to emitting it. Setting to FALSE (the default) will
  987. * do the opposite.
  988. *
  989. * @return mixed If the return parameter is set to TRUE, then the
  990. * export is returned as a string, otherwise NULL is
  991. * returned.
  992. */
  993. public static function export($name, $ret=false) {
  994. $obj = new ReflectionExtension($name);
  995. $str = (string)$obj;
  996. if ($ret) {
  997. return $str;
  998. }
  999. print $str;
  1000. }
  1001. // This doc comment block generated by idl/sysdoc.php
  1002. /**
  1003. * ( excerpt from http://php.net/manual/en/reflectionextension.getname.php
  1004. * )
  1005. *
  1006. * Gets the extensions name.
  1007. *
  1008. * @return mixed The extensions name.
  1009. */
  1010. <<__Rx, __MaybeMutable>>
  1011. public function getName() {
  1012. return $this->info['name'];
  1013. }
  1014. // This doc comment block generated by idl/sysdoc.php
  1015. /**
  1016. * ( excerpt from
  1017. * http://php.net/manual/en/reflectionextension.getversion.php )
  1018. *
  1019. * Gets the version of the extension.
  1020. *
  1021. * @return mixed The version of the extension.
  1022. */
  1023. <<__Rx, __MaybeMutable>>
  1024. public function getVersion() {
  1025. return $this->info['version'];
  1026. }
  1027. // This doc comment block generated by idl/sysdoc.php
  1028. /**
  1029. * ( excerpt from
  1030. * http://php.net/manual/en/reflectionextension.getfunctions.php )
  1031. *
  1032. * Get defined functions from an extension.
  1033. *
  1034. * @return mixed An associative array of ReflectionFunction objects,
  1035. * for each function defined in the extension with the
  1036. * keys being the function names. If no function are
  1037. * defined, an empty array is returned.
  1038. */
  1039. <<__Rx, __MaybeMutable>>
  1040. public function getFunctions() {
  1041. return $this->info['functions'];
  1042. }
  1043. // This doc comment block generated by idl/sysdoc.php
  1044. /**
  1045. * ( excerpt from
  1046. * http://php.net/manual/en/reflectionextension.getconstants.php )
  1047. *
  1048. * Get defined constants from an extension.
  1049. *
  1050. * @return mixed An associative array with constant names as keys.
  1051. */
  1052. <<__Rx, __MaybeMutable>>
  1053. public function getConstants() {
  1054. return $this->info['constants'];
  1055. }
  1056. // This doc comment block generated by idl/sysdoc.php
  1057. /**
  1058. * ( excerpt from
  1059. * http://php.net/manual/en/reflectionextension.getinientries.php )
  1060. *
  1061. * Get the ini entries for an extension.
  1062. *
  1063. * @return mixed An associative array with the ini entries as keys,
  1064. * with their defined values as values.
  1065. */
  1066. <<__Rx, __MaybeMutable>>
  1067. public function getINIEntries() {
  1068. return $this->info['ini'];
  1069. }
  1070. // This doc comment block generated by idl/sysdoc.php
  1071. /**
  1072. * ( excerpt from
  1073. * http://php.net/manual/en/reflectionextension.getclasses.php )
  1074. *
  1075. * Gets a list of classes from an extension.
  1076. *
  1077. * @return mixed An array of ReflectionClass objects, one for each
  1078. * class within the extension. If no classes are
  1079. * defined, an empty array is returned.
  1080. */
  1081. <<__Rx, __MaybeMutable>>
  1082. public function getClasses() {
  1083. return $this->info['classes'];
  1084. }
  1085. // This doc comment block generated by idl/sysdoc.php
  1086. /**
  1087. * ( excerpt from
  1088. * http://php.net/manual/en/reflectionextension.getclassnames.php )
  1089. *
  1090. * Gets a listing of class names as defined in the extension.
  1091. *
  1092. * @return mixed An array of class names, as defined in the
  1093. * extension. If no classes are defined, an empty array
  1094. * is returned.
  1095. */
  1096. <<__Rx, __MaybeMutable>>
  1097. public function getClassNames() {
  1098. $ret = varray[];
  1099. foreach ($this->info['classes'] as $cls) {
  1100. $ret[] = $cls->getName();
  1101. }
  1102. return $ret;
  1103. }
  1104. // This doc comment block generated by idl/sysdoc.php
  1105. /**
  1106. * ( excerpt from http://php.net/manual/en/reflectionextension.info.php )
  1107. *
  1108. * Prints out the " phpinfo()" snippet for the given extension.
  1109. *
  1110. * @return mixed Information about the extension.
  1111. */
  1112. <<__Rx, __MaybeMutable>>
  1113. public function info() {
  1114. return $this->info['info'];
  1115. }
  1116. }
  1117. /**
  1118. * ( excerpt from http://php.net/manual/en/class.reflectiontype.php )
  1119. *
  1120. * The ReflectionType class reports information about a function's parameters
  1121. * or return type.
  1122. *
  1123. */
  1124. class ReflectionType {
  1125. private $type_hint_info;
  1126. <<__Rx>>
  1127. public function __construct(?Reflector $param_or_ret = null,
  1128. darray $type_hint_info = darray[]) {
  1129. // PHP7 actually allows you to call this constructor from user code
  1130. // successfully, even though it is really meant to only be called from
  1131. // ReflectionParameter::getType() and
  1132. // ReflectionFunctionAbstract::getReturnType(). And if you do call it from
  1133. // user code, it will construct successfully, but you will fail on the
  1134. // *first* method call to the instantiated object.
  1135. // Let's assume that is not going to be an issue in the real world, and
  1136. // make it a simple check and throw instead. If we really need that
  1137. // functionality, we can do something like this instead:
  1138. // https://phabricator.fb.com/P20754288
  1139. if (!($param_or_ret is ReflectionParameter ||
  1140. $param_or_ret is ReflectionFunctionAbstract)) {
  1141. $msg = 'ReflectionType::__construct(): Internal error: Failed to '
  1142. . 'retrieve the reflection object';
  1143. trigger_error($msg, E_ERROR);
  1144. }
  1145. $this->type_hint_info = $type_hint_info;
  1146. }
  1147. /**
  1148. * ( excerpt from http://php.net/manual/en/reflectiontype.allowsnull.php )
  1149. *
  1150. * Checks if null is allowed
  1151. *
  1152. * @return - true if null is allowed for the parameter or return type; false
  1153. * otherwise.
  1154. *
  1155. */
  1156. <<__Rx, __MaybeMutable>>
  1157. public function allowsNull(): bool {
  1158. return idx($this->type_hint_info, 'nullable', false);
  1159. }
  1160. /**
  1161. * ( excerpt from http://php.net/manual/en/reflectiontype.isbuiltin.php )
  1162. *
  1163. * Checks if the type is a builtin in type (e.g., int)
  1164. *
  1165. * @return - true if the type is a builtin type; false otherwise.
  1166. *
  1167. */
  1168. <<__Rx, __MaybeMutable>>
  1169. public function isBuiltin(): bool {
  1170. return idx($this->type_hint_info, 'builtin', false);
  1171. }
  1172. /**
  1173. * ( excerpt from http://php.net/manual/en/reflectiontype.tostring.php )
  1174. *
  1175. * Gets the parameter type name
  1176. *
  1177. * @return - a string representing the type name, if it exists; otherwise,
  1178. * the empty string.
  1179. *
  1180. */
  1181. <<__Rx, __MaybeMutable>>
  1182. public function __toString(): string {
  1183. return idx($this->type_hint_info, 'name', '');
  1184. }
  1185. // Prevent cloning
  1186. final public function __clone() {
  1187. throw new BadMethodCallException(
  1188. 'Trying to clone an uncloneable object of class ReflectionType'
  1189. );
  1190. }
  1191. }
  1192. } // root namespace
  1193. namespace HH {
  1194. /* These enum values correspond to the 'kind' field in the
  1195. * TypeStructure shape returned by type_structure() or
  1196. * ReflectionTypeConstant::getTypeStructure(). The following enum
  1197. * values are replicated in hphp/runtime/base/type-structure.h
  1198. */
  1199. enum TypeStructureKind: int {
  1200. OF_VOID = 0;
  1201. OF_INT = 1;
  1202. OF_BOOL = 2;
  1203. OF_FLOAT = 3;
  1204. OF_STRING = 4;
  1205. OF_RESOURCE = 5;
  1206. OF_NUM = 6;
  1207. OF_ARRAYKEY = 7;
  1208. OF_NORETURN = 8;
  1209. OF_MIXED = 9;
  1210. OF_TUPLE = 10;
  1211. OF_FUNCTION = 11;
  1212. OF_ARRAY = 12;
  1213. OF_GENERIC = 13;
  1214. OF_SHAPE = 14;
  1215. OF_CLASS = 15;
  1216. OF_INTERFACE = 16;
  1217. OF_TRAIT = 17;
  1218. OF_ENUM = 18;
  1219. OF_DICT = 19;
  1220. OF_VEC = 20;
  1221. OF_KEYSET = 21;
  1222. OF_VEC_OR_DICT = 22;
  1223. OF_NONNULL = 23;
  1224. OF_DARRAY = 24;
  1225. OF_VARRAY = 25;
  1226. OF_VARRAY_OR_DARRAY = 26;
  1227. OF_NULL = 28;
  1228. OF_NOTHING = 29;
  1229. OF_DYNAMIC = 30;
  1230. OF_UNRESOLVED = 101; // for type aliases only
  1231. OF_XHP = 103;
  1232. }
  1233. type TypeStructure<T> = shape(
  1234. 'kind' => TypeStructureKind,
  1235. 'nullable' => ?bool,
  1236. // classname for classes, interfaces, enums, or traits
  1237. 'classname' => ?classname<T>,
  1238. // for tuples
  1239. 'elem_types' => ?varray,
  1240. // for functions
  1241. 'param_types' => ?varray,
  1242. 'return_type' => ?darray,
  1243. ?'variadic_type' => darray,
  1244. // for arrays, classes
  1245. 'generic_types' => ?varray,
  1246. // for shapes
  1247. 'fields' => ?darray,
  1248. // name for generics (type variables)
  1249. 'name' => ?string,
  1250. // for type aliases
  1251. 'alias' => ?string,
  1252. // if the type is exact (i.e., not a subtype)
  1253. ?'exact' => bool,
  1254. // if the type is a like-type
  1255. ?'like' => bool,
  1256. );
  1257. /**
  1258. * Retrieves the TypeStructure for a type constant or a type alias.
  1259. *
  1260. * @cls_or_obj mixed An instance of a class or the name of a class. If
  1261. * @cns_name is null or not provided, then this must
  1262. * the name of a type alias.
  1263. *
  1264. * @cns_name ?string If @cls_or_obj references a class, then this is
  1265. * the name of the type constant whose TypeStructure
  1266. * is being retrieved. This is null when retrieving
  1267. * the type constant for a type alias.
  1268. *
  1269. * @return darray The resolved type structure for either a type
  1270. * constant or a type alias.
  1271. */
  1272. <<__Native, __Rx>>
  1273. function type_structure(mixed $cls_or_obj, ?string $cns_name = null): darray;
  1274. /**
  1275. * Retrieves the classname on the the TypeStructure pointed by a type
  1276. * constant or a type alias.
  1277. */
  1278. <<__Native, __Rx>>
  1279. function type_structure_classname(mixed $cls_or_obj,
  1280. ?string $cns_name = null): string;
  1281. /**
  1282. * Retrieves the TypeStructure for a type alias.
  1283. *
  1284. * @cls_or_obj mixed the name of a type alias.
  1285. *
  1286. * @return darray The resolved type structure for a type alias.
  1287. */
  1288. <<__Rx>>
  1289. function type_structure_for_alias(mixed $cls_or_obj): darray {
  1290. return type_structure($cls_or_obj, null);
  1291. }
  1292. }