ClassMethod   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 6
Bugs 2 Features 2
Metric Value
wmc 22
c 6
b 2
f 2
lcom 1
cbo 2
dl 0
loc 93
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubNodeNames() 0 3 1
A returnsByRef() 0 3 1
A getParams() 0 3 1
A getReturnType() 0 3 1
A getStmts() 0 3 1
A isPublic() 0 4 2
A isProtected() 0 3 1
A isPrivate() 0 3 1
A isAbstract() 0 3 1
A isFinal() 0 3 1
A isStatic() 0 3 1
C __construct() 0 20 10
1
<?php
2
3
namespace PhpParser\Node\Stmt;
4
5
use PhpParser\Node;
6
use PhpParser\Node\FunctionLike;
7
use PhpParser\Error;
8
9
class ClassMethod extends Node\Stmt implements FunctionLike
10
{
11
    /** @var int Type */
12
    public $type;
13
    /** @var bool Whether to return by reference */
14
    public $byRef;
15
    /** @var string Name */
16
    public $name;
17
    /** @var Node\Param[] Parameters */
18
    public $params;
19
    /** @var null|string|Node\Name Return type */
20
    public $returnType;
21
    /** @var Node[] Statements */
22
    public $stmts;
23
24
    /**
25
     * Constructs a class method node.
26
     *
27
     * @param string      $name       Name
28
     * @param array       $subNodes   Array of the following optional subnodes:
29
     *                                'type'       => MODIFIER_PUBLIC: Type
30
     *                                'byRef'      => false          : Whether to return by reference
31
     *                                'params'     => array()        : Parameters
32
     *                                'returnType' => null           : Return type
33
     *                                'stmts'      => array()        : Statements
34
     * @param array       $attributes Additional attributes
35
     */
36
    public function __construct($name, array $subNodes = array(), array $attributes = array()) {
37
        parent::__construct($attributes);
38
        $this->type = isset($subNodes['type']) ? $subNodes['type'] : 0;
39
        $this->byRef = isset($subNodes['byRef'])  ? $subNodes['byRef']  : false;
40
        $this->name = $name;
41
        $this->params = isset($subNodes['params']) ? $subNodes['params'] : array();
42
        $this->returnType = isset($subNodes['returnType']) ? $subNodes['returnType'] : null;
43
        $this->stmts = array_key_exists('stmts', $subNodes) ? $subNodes['stmts'] : array();
44
45
        if ($this->type & Class_::MODIFIER_STATIC) {
46
            switch (strtolower($this->name)) {
47
                case '__construct':
48
                    throw new Error(sprintf('Constructor %s() cannot be static', $this->name));
49
                case '__destruct':
50
                    throw new Error(sprintf('Destructor %s() cannot be static', $this->name));
51
                case '__clone':
52
                    throw new Error(sprintf('Clone method %s() cannot be static', $this->name));
53
            }
54
        }
55
    }
56
57
    public function getSubNodeNames() {
58
        return array('type', 'byRef', 'name', 'params', 'returnType', 'stmts');
59
    }
60
61
    public function returnsByRef() {
62
        return $this->byRef;
63
    }
64
65
    public function getParams() {
66
        return $this->params;
67
    }
68
69
    public function getReturnType() {
70
        return $this->returnType;
71
    }
72
73
    public function getStmts() {
74
        return $this->stmts;
75
    }
76
77
    public function isPublic() {
78
        return ($this->type & Class_::MODIFIER_PUBLIC) !== 0
79
            || ($this->type & Class_::VISIBILITY_MODIFER_MASK) === 0;
80
    }
81
82
    public function isProtected() {
83
        return (bool) ($this->type & Class_::MODIFIER_PROTECTED);
84
    }
85
86
    public function isPrivate() {
87
        return (bool) ($this->type & Class_::MODIFIER_PRIVATE);
88
    }
89
90
    public function isAbstract() {
91
        return (bool) ($this->type & Class_::MODIFIER_ABSTRACT);
92
    }
93
94
    public function isFinal() {
95
        return (bool) ($this->type & Class_::MODIFIER_FINAL);
96
    }
97
98
    public function isStatic() {
99
        return (bool) ($this->type & Class_::MODIFIER_STATIC);
100
    }
101
}
102