1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-12-15 02:47:04 +01:00
PHP-Parser/lib/PhpParser/Builder/Class_.php

123 lines
3.1 KiB
PHP
Raw Normal View History

2017-08-18 22:57:27 +02:00
<?php declare(strict_types=1);
2012-03-10 17:56:56 +01:00
namespace PhpParser\Builder;
use PhpParser;
use PhpParser\BuilderHelpers;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
class Class_ extends Declaration
2012-03-10 17:56:56 +01:00
{
protected $name;
protected $extends = null;
protected $implements = [];
protected $flags = 0;
2012-03-10 17:56:56 +01:00
protected $uses = [];
protected $constants = [];
protected $properties = [];
protected $methods = [];
2012-03-10 17:56:56 +01:00
/**
* Creates a class builder.
*
* @param string $name Name of the class
*/
2017-04-28 21:40:59 +02:00
public function __construct(string $name) {
2012-03-10 17:56:56 +01:00
$this->name = $name;
}
/**
* Extends a class.
*
* @param Name|string $class Name of class to extend
2012-03-10 17:56:56 +01:00
*
* @return $this The builder instance (for fluid interface)
2012-03-10 17:56:56 +01:00
*/
public function extend($class) {
$this->extends = BuilderHelpers::normalizeName($class);
2012-03-10 17:56:56 +01:00
return $this;
}
/**
* Implements one or more interfaces.
*
2016-02-20 17:06:09 +01:00
* @param Name|string ...$interfaces Names of interfaces to implement
2012-03-10 17:56:56 +01:00
*
* @return $this The builder instance (for fluid interface)
2012-03-10 17:56:56 +01:00
*/
public function implement(...$interfaces) {
foreach ($interfaces as $interface) {
$this->implements[] = BuilderHelpers::normalizeName($interface);
2012-03-10 17:56:56 +01:00
}
return $this;
}
/**
* Makes the class abstract.
*
* @return $this The builder instance (for fluid interface)
2012-03-10 17:56:56 +01:00
*/
public function makeAbstract() {
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_ABSTRACT);
2012-03-10 17:56:56 +01:00
return $this;
}
/**
* Makes the class final.
*
* @return $this The builder instance (for fluid interface)
2012-03-10 17:56:56 +01:00
*/
public function makeFinal() {
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_FINAL);
2012-03-10 17:56:56 +01:00
return $this;
}
/**
* Adds a statement.
*
* @param Stmt|PhpParser\Builder $stmt The statement to add
2012-03-10 17:56:56 +01:00
*
* @return $this The builder instance (for fluid interface)
2012-03-10 17:56:56 +01:00
*/
public function addStmt($stmt) {
$stmt = BuilderHelpers::normalizeNode($stmt);
2012-03-10 17:56:56 +01:00
$targets = [
Stmt\TraitUse::class => &$this->uses,
Stmt\ClassConst::class => &$this->constants,
Stmt\Property::class => &$this->properties,
Stmt\ClassMethod::class => &$this->methods,
];
2012-03-10 17:56:56 +01:00
$class = \get_class($stmt);
if (!isset($targets[$class])) {
throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
2012-03-10 17:56:56 +01:00
}
$targets[$class][] = $stmt;
2012-03-10 17:56:56 +01:00
return $this;
}
/**
* Returns the built class node.
*
* @return Stmt\Class_ The built class node
2012-03-10 17:56:56 +01:00
*/
2017-04-28 21:40:59 +02:00
public function getNode() : PhpParser\Node {
return new Stmt\Class_($this->name, [
'flags' => $this->flags,
2012-03-10 17:56:56 +01:00
'extends' => $this->extends,
'implements' => $this->implements,
'stmts' => array_merge($this->uses, $this->constants, $this->properties, $this->methods),
], $this->attributes);
2012-03-10 17:56:56 +01:00
}
2018-01-10 18:04:06 +01:00
}