php-parser/lib/PhpParser/Builder/Method.php

129 lines
3.1 KiB
PHP
Raw Normal View History

2012-03-10 23:25:26 +01:00
<?php
namespace PhpParser\Builder;
use PhpParser;
use PhpParser\Node;
use PhpParser\Node\Stmt;
class Method extends FunctionLike
2012-03-10 23:25:26 +01:00
{
protected $name;
protected $flags = 0;
/** @var array|null */
protected $stmts = array();
2012-03-10 23:25:26 +01:00
/**
* Creates a method builder.
*
* @param string $name Name of the method
*/
public function __construct($name) {
$this->name = $name;
}
/**
* Makes the method public.
*
* @return $this The builder instance (for fluid interface)
2012-03-10 23:25:26 +01:00
*/
public function makePublic() {
$this->flags = $this->addModifier($this->flags, Stmt\Class_::MODIFIER_PUBLIC);
2012-03-10 23:25:26 +01:00
return $this;
}
/**
* Makes the method protected.
*
* @return $this The builder instance (for fluid interface)
2012-03-10 23:25:26 +01:00
*/
public function makeProtected() {
$this->flags = $this->addModifier($this->flags, Stmt\Class_::MODIFIER_PROTECTED);
2012-03-10 23:25:26 +01:00
return $this;
}
/**
* Makes the method private.
*
* @return $this The builder instance (for fluid interface)
2012-03-10 23:25:26 +01:00
*/
public function makePrivate() {
$this->flags = $this->addModifier($this->flags, Stmt\Class_::MODIFIER_PRIVATE);
2012-03-10 23:25:26 +01:00
return $this;
}
/**
* Makes the method static.
*
* @return $this The builder instance (for fluid interface)
2012-03-10 23:25:26 +01:00
*/
public function makeStatic() {
$this->flags = $this->addModifier($this->flags, Stmt\Class_::MODIFIER_STATIC);
2012-03-10 23:25:26 +01:00
return $this;
}
/**
* Makes the method abstract.
*
* @return $this The builder instance (for fluid interface)
2012-03-10 23:25:26 +01:00
*/
public function makeAbstract() {
if (!empty($this->stmts)) {
throw new \LogicException('Cannot make method with statements abstract');
2012-03-10 23:25:26 +01:00
}
$this->flags = $this->addModifier($this->flags, Stmt\Class_::MODIFIER_ABSTRACT);
2012-03-10 23:25:26 +01:00
$this->stmts = null; // abstract methods don't have statements
return $this;
}
/**
* Makes the method final.
*
* @return $this The builder instance (for fluid interface)
2012-03-10 23:25:26 +01:00
*/
public function makeFinal() {
$this->flags = $this->addModifier($this->flags, Stmt\Class_::MODIFIER_FINAL);
2012-03-10 23:25:26 +01:00
return $this;
}
/**
* Adds a statement.
*
* @param Node|PhpParser\Builder $stmt The statement to add
2012-03-10 23:25:26 +01:00
*
* @return $this The builder instance (for fluid interface)
2012-03-10 23:25:26 +01:00
*/
public function addStmt($stmt) {
if (null === $this->stmts) {
throw new \LogicException('Cannot add statements to an abstract method');
2012-03-10 23:25:26 +01:00
}
$this->stmts[] = $this->normalizeStmt($stmt);
2012-03-10 23:25:26 +01:00
return $this;
}
/**
* Returns the built method node.
*
* @return Stmt\ClassMethod The built method node
2012-03-10 23:25:26 +01:00
*/
public function getNode() {
return new Stmt\ClassMethod($this->name, array(
'flags' => $this->flags,
'byRef' => $this->returnByRef,
'params' => $this->params,
'returnType' => $this->returnType,
'stmts' => $this->stmts,
), $this->attributes);
2012-03-10 23:25:26 +01:00
}
}