2011-04-18 19:02:30 +02:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser\Node\Stmt;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Error;
|
|
|
|
|
2011-05-29 12:20:47 +02:00
|
|
|
/**
|
2014-02-06 14:44:16 +01:00
|
|
|
* @property int $type Type
|
|
|
|
* @property string $name Name
|
|
|
|
* @property null|Node\Name $extends Name of extended class
|
|
|
|
* @property Node\Name[] $implements Names of implemented interfaces
|
|
|
|
* @property Node[] $stmts Statements
|
2011-05-29 12:20:47 +02:00
|
|
|
*/
|
2014-02-06 14:44:16 +01:00
|
|
|
class Class_ extends Node\Stmt
|
2011-04-18 19:02:30 +02:00
|
|
|
{
|
|
|
|
const MODIFIER_PUBLIC = 1;
|
|
|
|
const MODIFIER_PROTECTED = 2;
|
|
|
|
const MODIFIER_PRIVATE = 4;
|
|
|
|
const MODIFIER_STATIC = 8;
|
|
|
|
const MODIFIER_ABSTRACT = 16;
|
|
|
|
const MODIFIER_FINAL = 32;
|
|
|
|
|
2014-11-13 20:18:49 +01:00
|
|
|
const VISIBILITY_MODIFER_MASK = 7; // 1 | 2 | 4
|
|
|
|
|
2011-11-27 12:53:48 +01:00
|
|
|
protected static $specialNames = array(
|
2011-10-28 19:06:24 +02:00
|
|
|
'self' => true,
|
|
|
|
'parent' => true,
|
|
|
|
'static' => true,
|
|
|
|
);
|
2011-08-04 12:03:34 +02:00
|
|
|
|
2011-10-28 19:06:24 +02:00
|
|
|
/**
|
|
|
|
* Constructs a class node.
|
|
|
|
*
|
|
|
|
* @param string $name Name
|
|
|
|
* @param array $subNodes Array of the following optional subnodes:
|
|
|
|
* 'type' => 0 : Type
|
|
|
|
* 'extends' => null : Name of extended class
|
|
|
|
* 'implements' => array(): Names of implemented interfaces
|
|
|
|
* 'stmts' => array(): Statements
|
2012-04-29 23:32:09 +02:00
|
|
|
* @param array $attributes Additional attributes
|
2011-10-28 19:06:24 +02:00
|
|
|
*/
|
2012-04-29 23:32:09 +02:00
|
|
|
public function __construct($name, array $subNodes = array(), array $attributes = array()) {
|
2011-10-28 19:06:24 +02:00
|
|
|
parent::__construct(
|
2014-03-22 14:49:56 +01:00
|
|
|
array(
|
|
|
|
'type' => isset($subNodes['type']) ? $subNodes['type'] : 0,
|
|
|
|
'name' => $name,
|
|
|
|
'extends' => isset($subNodes['extends']) ? $subNodes['extends'] : null,
|
|
|
|
'implements' => isset($subNodes['implements']) ? $subNodes['implements'] : array(),
|
|
|
|
'stmts' => isset($subNodes['stmts']) ? $subNodes['stmts'] : array(),
|
2011-10-28 19:06:24 +02:00
|
|
|
),
|
2012-04-29 23:32:09 +02:00
|
|
|
$attributes
|
2011-10-28 19:06:24 +02:00
|
|
|
);
|
|
|
|
|
2011-11-27 12:53:48 +01:00
|
|
|
if (isset(self::$specialNames[(string) $this->name])) {
|
2014-02-12 20:23:12 +01:00
|
|
|
throw new Error(sprintf('Cannot use \'%s\' as class name as it is reserved', $this->name));
|
2011-08-04 12:03:34 +02:00
|
|
|
}
|
|
|
|
|
2011-11-27 12:53:48 +01:00
|
|
|
if (isset(self::$specialNames[(string) $this->extends])) {
|
2014-02-12 20:23:12 +01:00
|
|
|
throw new Error(sprintf('Cannot use \'%s\' as class name as it is reserved', $this->extends));
|
2011-08-04 12:03:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->implements as $interface) {
|
2011-11-27 12:53:48 +01:00
|
|
|
if (isset(self::$specialNames[(string) $interface])) {
|
2014-02-12 20:23:12 +01:00
|
|
|
throw new Error(sprintf('Cannot use \'%s\' as interface name as it is reserved', $interface));
|
2011-08-04 12:03:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-07 16:08:37 +02:00
|
|
|
public function isAbstract() {
|
|
|
|
return (bool) ($this->type & self::MODIFIER_ABSTRACT);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isFinal() {
|
|
|
|
return (bool) ($this->type & self::MODIFIER_FINAL);
|
|
|
|
}
|
|
|
|
|
2012-07-07 16:24:07 +02:00
|
|
|
public function getMethods() {
|
|
|
|
$methods = array();
|
|
|
|
foreach ($this->stmts as $stmt) {
|
2014-02-06 14:44:16 +01:00
|
|
|
if ($stmt instanceof ClassMethod) {
|
2012-07-07 16:24:07 +02:00
|
|
|
$methods[] = $stmt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $methods;
|
|
|
|
}
|
|
|
|
|
2014-09-30 20:23:25 +02:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2011-04-18 19:02:30 +02:00
|
|
|
public static function verifyModifier($a, $b) {
|
2014-11-13 20:18:49 +01:00
|
|
|
if ($a & self::VISIBILITY_MODIFER_MASK && $b & self::VISIBILITY_MODIFER_MASK) {
|
2014-02-06 14:44:16 +01:00
|
|
|
throw new Error('Multiple access type modifiers are not allowed');
|
2011-05-29 12:20:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($a & self::MODIFIER_ABSTRACT && $b & self::MODIFIER_ABSTRACT) {
|
2014-02-06 14:44:16 +01:00
|
|
|
throw new Error('Multiple abstract modifiers are not allowed');
|
2011-05-29 12:20:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($a & self::MODIFIER_STATIC && $b & self::MODIFIER_STATIC) {
|
2014-02-06 14:44:16 +01:00
|
|
|
throw new Error('Multiple static modifiers are not allowed');
|
2011-05-29 12:20:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($a & self::MODIFIER_FINAL && $b & self::MODIFIER_FINAL) {
|
2014-02-06 14:44:16 +01:00
|
|
|
throw new Error('Multiple final modifiers are not allowed');
|
2011-05-29 12:20:47 +02:00
|
|
|
}
|
|
|
|
|
2011-06-03 17:44:23 +02:00
|
|
|
if ($a & 48 && $b & 48) {
|
2014-02-12 20:23:12 +01:00
|
|
|
throw new Error('Cannot use the final modifier on an abstract class member');
|
2011-05-29 12:20:47 +02:00
|
|
|
}
|
2011-04-18 19:02:30 +02:00
|
|
|
}
|
2014-01-23 13:33:02 +01:00
|
|
|
}
|