1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-12-14 18:36:59 +01:00
PHP-Parser/lib/PhpParser/Node/Stmt/ClassMethod.php

152 lines
4.2 KiB
PHP
Raw Normal View History

2017-08-18 22:57:27 +02:00
<?php declare(strict_types=1);
namespace PhpParser\Node\Stmt;
use PhpParser\Node;
use PhpParser\Node\FunctionLike;
class ClassMethod extends Node\Stmt implements FunctionLike
{
/** @var int Flags */
public $flags;
/** @var bool Whether to return by reference */
public $byRef;
/** @var Node\Identifier Name */
public $name;
/** @var Node\Param[] Parameters */
public $params;
/** @var null|Node\Identifier|Node\Name|Node\NullableType Return type */
public $returnType;
/** @var Node\Stmt[]|null Statements */
public $stmts;
private static $magicNames = [
2017-04-22 14:37:53 +02:00
'__construct' => true,
'__destruct' => true,
'__call' => true,
'__callstatic' => true,
'__get' => true,
'__set' => true,
'__isset' => true,
'__unset' => true,
'__sleep' => true,
'__wakeup' => true,
'__tostring' => true,
'__set_state' => true,
'__clone' => true,
'__invoke' => true,
'__debuginfo' => true,
];
2017-04-22 14:37:53 +02:00
/**
* Constructs a class method node.
*
* @param string|Node\Identifier $name Name
* @param array $subNodes Array of the following optional subnodes:
* 'flags => MODIFIER_PUBLIC: Flags
* 'byRef' => false : Whether to return by reference
* 'params' => array() : Parameters
* 'returnType' => null : Return type
* 'stmts' => array() : Statements
* @param array $attributes Additional attributes
*/
public function __construct($name, array $subNodes = [], array $attributes = []) {
2015-05-02 22:17:34 +02:00
parent::__construct($attributes);
2017-04-24 22:32:40 +02:00
$this->flags = $subNodes['flags'] ?? $subNodes['type'] ?? 0;
$this->byRef = $subNodes['byRef'] ?? false;
$this->name = \is_string($name) ? new Node\Identifier($name) : $name;
$this->params = $subNodes['params'] ?? [];
$returnType = $subNodes['returnType'] ?? null;
$this->returnType = \is_string($returnType) ? new Node\Identifier($returnType) : $returnType;
$this->stmts = array_key_exists('stmts', $subNodes) ? $subNodes['stmts'] : [];
}
2017-04-28 21:40:59 +02:00
public function getSubNodeNames() : array {
return ['flags', 'byRef', 'name', 'params', 'returnType', 'stmts'];
}
2017-04-28 21:40:59 +02:00
public function returnsByRef() : bool {
return $this->byRef;
}
2017-04-28 21:40:59 +02:00
public function getParams() : array {
return $this->params;
}
public function getReturnType() {
return $this->returnType;
}
2017-05-07 19:54:32 +02:00
public function getStmts() {
return $this->stmts;
}
2017-01-24 08:38:55 +01:00
/**
* Whether the method is explicitly or implicitly public.
*
2017-01-24 08:38:55 +01:00
* @return bool
*/
2017-04-28 21:40:59 +02:00
public function isPublic() : bool {
return ($this->flags & Class_::MODIFIER_PUBLIC) !== 0
|| ($this->flags & Class_::VISIBILITY_MODIFIER_MASK) === 0;
}
2017-01-24 08:38:55 +01:00
/**
* Whether the method is protected.
*
2017-01-24 08:38:55 +01:00
* @return bool
*/
2017-04-28 21:40:59 +02:00
public function isProtected() : bool {
return (bool) ($this->flags & Class_::MODIFIER_PROTECTED);
}
2017-01-24 08:38:55 +01:00
/**
* Whether the method is private.
*
2017-01-24 08:38:55 +01:00
* @return bool
*/
2017-04-28 21:40:59 +02:00
public function isPrivate() : bool {
return (bool) ($this->flags & Class_::MODIFIER_PRIVATE);
}
2017-01-24 08:38:55 +01:00
/**
* Whether the method is abstract.
*
2017-01-24 08:38:55 +01:00
* @return bool
*/
2017-04-28 21:40:59 +02:00
public function isAbstract() : bool {
return (bool) ($this->flags & Class_::MODIFIER_ABSTRACT);
}
2017-01-24 08:38:55 +01:00
/**
* Whether the method is final.
2017-12-25 14:26:46 +01:00
*
2017-01-24 08:38:55 +01:00
* @return bool
*/
2017-04-28 21:40:59 +02:00
public function isFinal() : bool {
return (bool) ($this->flags & Class_::MODIFIER_FINAL);
}
2017-01-24 08:38:55 +01:00
/**
* Whether the method is static.
*
2017-01-24 08:38:55 +01:00
* @return bool
*/
2017-04-28 21:40:59 +02:00
public function isStatic() : bool {
return (bool) ($this->flags & Class_::MODIFIER_STATIC);
}
2017-04-22 14:37:53 +02:00
/**
* Whether the method is magic.
*
* @return bool
*/
2017-04-28 21:40:59 +02:00
public function isMagic() : bool {
return isset(self::$magicNames[$this->name->toLowerString()]);
2017-04-22 14:37:53 +02:00
}
public function getType() : string {
return 'Stmt_ClassMethod';
}
}