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: . ($params = $this->parametersToString())
79: . $this->returnTypeToString()
80: . ($this->abstract || $this->body === false
81: ? ';'
82: : (strpos($params, "\n") === false ? "\n" : ' ')
83: . "{\n"
84: . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1)
85: . '}');
86: }
87:
88:
89: /**
90: * @param string|false
91: * @return static
92: */
93: public function setBody($code, array $args = null)
94: {
95: $this->body = $args === null ? $code : Helpers::formatArgs($code, $args);
96: return $this;
97: }
98:
99:
100: /**
101: * @return string|false
102: */
103: public function getBody()
104: {
105: return $this->body;
106: }
107:
108:
109: /**
110: * @param bool
111: * @return static
112: */
113: public function setStatic($state = true)
114: {
115: $this->static = (bool) $state;
116: return $this;
117: }
118:
119:
120: /**
121: * @return bool
122: */
123: public function isStatic()
124: {
125: return $this->static;
126: }
127:
128:
129: /**
130: * @param bool
131: * @return static
132: */
133: public function setFinal($state = true)
134: {
135: $this->final = (bool) $state;
136: return $this;
137: }
138:
139:
140: /**
141: * @return bool
142: */
143: public function isFinal()
144: {
145: return $this->final;
146: }
147:
148:
149: /**
150: * @param bool
151: * @return static
152: */
153: public function setAbstract($state = true)
154: {
155: $this->abstract = (bool) $state;
156: return $this;
157: }
158:
159:
160: /**
161: * @return bool
162: */
163: public function isAbstract()
164: {
165: return $this->abstract;
166: }
167: }
168: