1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6: */
7:
8: namespace Nette\PhpGenerator;
9:
10: use Nette;
11:
12:
13: /**
14: * Class method.
15: *
16: * @property string|false $body
17: */
18: class Method
19: {
20: use Nette\SmartObject;
21: use Traits\FunctionLike;
22: use Traits\NameAware;
23: use Traits\VisibilityAware;
24: use Traits\CommentAware;
25:
26: /** @var bool */
27: private $static = false;
28:
29: /** @var bool */
30: private $final = false;
31:
32: /** @var bool */
33: private $abstract = false;
34:
35:
36: /**
37: * @param callable
38: * @return static
39: */
40: public static function from($method)
41: {
42: $method = $method instanceof \ReflectionFunctionAbstract ? $method : Nette\Utils\Callback::toReflection($method);
43: if ($method instanceof \ReflectionFunction) {
44: trigger_error('For global functions or closures use Nette\PhpGenerator\GlobalFunction or Nette\PhpGenerator\Closure.', E_USER_DEPRECATED);
45: return (new Factory)->fromFunctionReflection($method);
46: }
47: return (new Factory)->fromMethodReflection($method);
48: }
49:
50:
51: /**
52: * @param string
53: */
54: public function __construct($name)
55: {
56: if ($name === null) {
57: throw new Nette\DeprecatedException('For closures use Nette\PhpGenerator\Closure instead of Nette\PhpGenerator\Method.');
58: } elseif (!Helpers::isIdentifier($name)) {
59: throw new Nette\InvalidArgumentException("Value '$name' is not valid name.");
60: }
61: $this->name = $name;
62: }
63:
64:
65: /**
66: * @return string PHP code
67: */
68: public function __toString()
69: {
70: return Helpers::formatDocComment($this->comment . "\n")
71: . ($this->abstract ? 'abstract ' : '')
72: . ($this->final ? 'final ' : '')
73: . ($this->visibility ? $this->visibility . ' ' : '')
74: . ($this->static ? 'static ' : '')
75: . 'function '
76: . ($this->returnReference ? '&' : '')
77: . $this->name
78: . $this->parametersToString()
79: . $this->returnTypeToString()
80: . ($this->abstract || $this->body === false
81: ? ';'
82: : "\n{\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1) . '}');
83: }
84:
85:
86: /**
87: * @param string|false
88: * @return static
89: */
90: public function setBody($code, array $args = null)
91: {
92: $this->body = $args === null ? $code : Helpers::formatArgs($code, $args);
93: return $this;
94: }
95:
96:
97: /**
98: * @return string|false
99: */
100: public function getBody()
101: {
102: return $this->body;
103: }
104:
105:
106: /**
107: * @param bool
108: * @return static
109: */
110: public function setStatic($state = true)
111: {
112: $this->static = (bool) $state;
113: return $this;
114: }
115:
116:
117: /**
118: * @return bool
119: */
120: public function isStatic()
121: {
122: return $this->static;
123: }
124:
125:
126: /**
127: * @param bool
128: * @return static
129: */
130: public function setFinal($state = true)
131: {
132: $this->final = (bool) $state;
133: return $this;
134: }
135:
136:
137: /**
138: * @return bool
139: */
140: public function isFinal()
141: {
142: return $this->final;
143: }
144:
145:
146: /**
147: * @param bool
148: * @return static
149: */
150: public function setAbstract($state = true)
151: {
152: $this->abstract = (bool) $state;
153: return $this;
154: }
155:
156:
157: /**
158: * @return bool
159: */
160: public function isAbstract()
161: {
162: return $this->abstract;
163: }
164: }
165: