Method.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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\PhpGenerator;
  8. use Nette;
  9. /**
  10. * Class method.
  11. *
  12. * @property string|null $body
  13. */
  14. final class Method
  15. {
  16. use Nette\SmartObject;
  17. use Traits\FunctionLike;
  18. use Traits\NameAware;
  19. use Traits\VisibilityAware;
  20. use Traits\CommentAware;
  21. use Traits\AttributeAware;
  22. /** @var string|null */
  23. private $body = '';
  24. /** @var bool */
  25. private $static = false;
  26. /** @var bool */
  27. private $final = false;
  28. /** @var bool */
  29. private $abstract = false;
  30. /**
  31. * @param string|array $method
  32. */
  33. public static function from($method): self
  34. {
  35. return (new Factory)->fromMethodReflection(Nette\Utils\Callback::toReflection($method));
  36. }
  37. public function __toString(): string
  38. {
  39. try {
  40. return (new Printer)->printMethod($this);
  41. } catch (\Throwable $e) {
  42. if (PHP_VERSION_ID >= 70400) {
  43. throw $e;
  44. }
  45. trigger_error('Exception in ' . __METHOD__ . "(): {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}", E_USER_ERROR);
  46. return '';
  47. }
  48. }
  49. /** @return static */
  50. public function setBody(?string $code, ?array $args = null): self
  51. {
  52. $this->body = $args === null || $code === null
  53. ? $code
  54. : (new Dumper)->format($code, ...$args);
  55. return $this;
  56. }
  57. public function getBody(): ?string
  58. {
  59. return $this->body;
  60. }
  61. /** @return static */
  62. public function setStatic(bool $state = true): self
  63. {
  64. $this->static = $state;
  65. return $this;
  66. }
  67. public function isStatic(): bool
  68. {
  69. return $this->static;
  70. }
  71. /** @return static */
  72. public function setFinal(bool $state = true): self
  73. {
  74. $this->final = $state;
  75. return $this;
  76. }
  77. public function isFinal(): bool
  78. {
  79. return $this->final;
  80. }
  81. /** @return static */
  82. public function setAbstract(bool $state = true): self
  83. {
  84. $this->abstract = $state;
  85. return $this;
  86. }
  87. public function isAbstract(): bool
  88. {
  89. return $this->abstract;
  90. }
  91. /**
  92. * @param string $name without $
  93. */
  94. public function addPromotedParameter(string $name, $defaultValue = null): PromotedParameter
  95. {
  96. $param = new PromotedParameter($name);
  97. if (func_num_args() > 1) {
  98. $param->setDefaultValue($defaultValue);
  99. }
  100. return $this->parameters[$name] = $param;
  101. }
  102. /** @throws Nette\InvalidStateException */
  103. public function validate(): void
  104. {
  105. if ($this->abstract && ($this->final || $this->visibility === ClassType::VisibilityPrivate)) {
  106. throw new Nette\InvalidStateException("Method $this->name() cannot be abstract and final or private at the same time.");
  107. }
  108. }
  109. }