| 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 |
declare (strict_types=1); |
| 8 |
namespace Packetery\Nette\PhpGenerator; |
| 9 |
|
| 10 |
use Packetery\Nette; |
| 11 |
/** |
| 12 |
* Class method. |
| 13 |
* |
| 14 |
* @property string|null $body |
| 15 |
*/ |
| 16 |
final class Method |
| 17 |
{ |
| 18 |
use \Packetery\Nette\SmartObject; |
| 19 |
use Traits\FunctionLike; |
| 20 |
use Traits\NameAware; |
| 21 |
use Traits\VisibilityAware; |
| 22 |
use Traits\CommentAware; |
| 23 |
use Traits\AttributeAware; |
| 24 |
/** @var string|null */ |
| 25 |
private $body = ''; |
| 26 |
/** @var bool */ |
| 27 |
private $static = \false; |
| 28 |
/** @var bool */ |
| 29 |
private $final = \false; |
| 30 |
/** @var bool */ |
| 31 |
private $abstract = \false; |
| 32 |
/** |
| 33 |
* @param string|array $method |
| 34 |
*/ |
| 35 |
public static function from($method) : self |
| 36 |
{ |
| 37 |
return (new Factory())->fromMethodReflection(\Packetery\Nette\Utils\Callback::toReflection($method)); |
| 38 |
} |
| 39 |
public function __toString() : string |
| 40 |
{ |
| 41 |
try { |
| 42 |
return (new Printer())->printMethod($this); |
| 43 |
} catch (\Throwable $e) { |
| 44 |
if (\PHP_VERSION_ID >= 70400) { |
| 45 |
throw $e; |
| 46 |
} |
| 47 |
\trigger_error('Exception in ' . __METHOD__ . "(): {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}", \E_USER_ERROR); |
| 48 |
return ''; |
| 49 |
} |
| 50 |
} |
| 51 |
/** @return static */ |
| 52 |
public function setBody(?string $code, array $args = null) : self |
| 53 |
{ |
| 54 |
$this->body = $args === null || $code === null ? $code : (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 \Packetery\Nette\InvalidStateException */ |
| 103 |
public function validate() : void |
| 104 |
{ |
| 105 |
if ($this->abstract && ($this->final || $this->visibility === ClassType::VISIBILITY_PRIVATE)) { |
| 106 |
throw new \Packetery\Nette\InvalidStateException('Method cannot be abstract and final or private.'); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|