159 lines
2.8 KiB
PHP
Raw Normal View History

2020-11-19 09:42:54 +08:00
<?php
2020-11-19 17:31:31 +08:00
namespace catcher\generate\build\classes;
2020-11-19 09:42:54 +08:00
2020-11-19 17:31:31 +08:00
use catcher\generate\build\traits\CatchMethodReturn;
use PhpParser\BuilderFactory;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Expression;
class Methods
2020-11-19 09:42:54 +08:00
{
2020-11-19 17:31:31 +08:00
use CatchMethodReturn;
2020-11-19 09:42:54 +08:00
2020-11-19 17:31:31 +08:00
protected $methodBuild;
2020-11-19 09:42:54 +08:00
2020-11-19 17:31:31 +08:00
public function __construct(string $name)
2020-11-19 09:42:54 +08:00
{
2020-11-19 17:31:31 +08:00
$this->methodBuild = (new BuilderFactory())->method($name);
2020-11-19 09:42:54 +08:00
}
2020-11-19 17:31:31 +08:00
public function public()
2020-11-19 09:42:54 +08:00
{
2020-11-19 17:31:31 +08:00
$this->methodBuild->makePublic();
2020-11-19 09:42:54 +08:00
return $this;
}
2020-11-19 17:31:31 +08:00
public function protected()
2020-11-19 09:42:54 +08:00
{
2020-11-19 17:31:31 +08:00
$this->methodBuild->makeProtected();
2020-11-19 09:42:54 +08:00
return $this;
}
2020-11-19 17:31:31 +08:00
public function private()
2020-11-19 09:42:54 +08:00
{
2020-11-19 17:31:31 +08:00
$this->methodBuild->makePrivate();
2020-11-19 09:42:54 +08:00
return $this;
}
/**
* set params
*
* @time 2020年11月16日
* @param $type
* @param $param
* @param $default
* @return $this
*/
public function param($param, $type = null, $default = null)
{
2020-11-19 17:31:31 +08:00
$param = (new BuilderFactory())->param($param);
if ($type) {
$param = $param->setType($type);
}
if ($default) {
$param = $param->setDefault($default);
}
$this->methodBuild->addParam(
$param
);
2020-11-19 09:42:54 +08:00
return $this;
}
/**
2020-11-19 17:31:31 +08:00
* 定义
2020-11-19 09:42:54 +08:00
*
2020-11-19 17:31:31 +08:00
* @time 2020年11月18日
* @param $variable
* @param $value
2020-11-19 09:42:54 +08:00
* @return $this
*/
2020-11-19 17:31:31 +08:00
public function declare($variable, $value)
2020-11-19 09:42:54 +08:00
{
2020-11-19 17:31:31 +08:00
$smt = new Expression(
new Assign(
new PropertyFetch(
new Variable('this'),
new Identifier($variable)
),
new Variable($value)
)
);
$this->methodBuild->addStmt($smt);
2020-11-19 09:42:54 +08:00
return $this;
}
/**
* 返回值
*
* @time 2020年11月16日
2020-11-19 17:31:31 +08:00
* @param $returnType
2020-11-19 09:42:54 +08:00
* @return $this
*/
2020-11-19 17:31:31 +08:00
public function returnType($returnType)
2020-11-19 09:42:54 +08:00
{
2020-11-19 17:31:31 +08:00
$this->methodBuild->setReturnType($returnType);
2020-11-19 09:42:54 +08:00
return $this;
}
/**
* 注释
*
* @time 2020年11月16日
* @param $comment
* @return $this
*/
2020-11-19 17:31:31 +08:00
public function docComment(string $comment)
2020-11-19 09:42:54 +08:00
{
2020-11-19 17:31:31 +08:00
$this->methodBuild->setDocComment($comment);
2020-11-19 09:42:54 +08:00
return $this;
}
/**
* 抽象
*
* @time 2020年11月17日
* @return $this
*/
public function toAbstract()
{
2020-11-19 17:31:31 +08:00
$this->methodBuild->makeAbstract();
2020-11-19 09:42:54 +08:00
return $this;
}
/**
* final
*
* @time 2020年11月17日
* @return $this
*/
public function toFinal()
{
2020-11-19 17:31:31 +08:00
$this->methodBuild->makeFinal();
2020-11-19 09:42:54 +08:00
return $this;
}
2020-11-19 17:31:31 +08:00
public function build()
{
return $this->methodBuild;
2020-11-19 09:42:54 +08:00
}
}