Issues (92)

Branch: master

src/main/php/PHPMD/Node/MethodNode.php (2 issues)

Labels
Severity
1
<?php
2
/**
3
 * This file is part of PHP Mess Detector.
4
 *
5
 * Copyright (c) Manuel Pichler <[email protected]>.
6
 * All rights reserved.
7
 *
8
 * Licensed under BSD License
9
 * For full copyright and license information, please see the LICENSE file.
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @author Manuel Pichler <[email protected]>
13
 * @copyright Manuel Pichler. All rights reserved.
14
 * @license https://opensource.org/licenses/bsd-license.php BSD License
15
 * @link http://phpmd.org/
16
 */
17
18
namespace PHPMD\Node;
19
20
use PDepend\Source\AST\ASTEnum;
21
use PDepend\Source\AST\ASTMethod;
22
use PDepend\Source\AST\ASTClass;
23
use PDepend\Source\AST\ASTTrait;
24
use PHPMD\Rule;
25
26
/**
27
 * Wrapper around a PHP_Depend method node.
28
 *
29
 * Methods available on $node via PHPMD\AbstractNode::__call
30
 *
31
 * @method bool isPrivate() Returns true if this node is marked as private.
32
 */
33
class MethodNode extends AbstractCallableNode
34
{
35
    /**
36
     * Constructs a new method wrapper.
37
     *
38
     * @param \PDepend\Source\AST\ASTMethod $node
39
     */
40 84
    public function __construct(ASTMethod $node)
41
    {
42 84
        parent::__construct($node);
43 84
    }
44
45
    /**
46
     * Returns the name of the parent package.
47
     *
48
     * @return string
49
     */
50 5
    public function getNamespaceName()
51
    {
52 5
        return $this->getNode()->getParent()->getNamespace()->getName();
0 ignored issues
show
The method getParent() does not exist on PDepend\Source\AST\ASTArtifact. It seems like you code against a sub-type of PDepend\Source\AST\ASTArtifact such as PDepend\Source\AST\ASTEnumCase or PDepend\Source\AST\ASTAnonymousClass or PDepend\Source\AST\ASTMethod. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
        return $this->getNode()->/** @scrutinizer ignore-call */ getParent()->getNamespace()->getName();
Loading history...
53
    }
54
55
    /**
56
     * Returns the name of the parent type or <b>null</b> when this node has no
57
     * parent type.
58
     *
59
     * @return string|null
60
     */
61 2
    public function getParentName()
62
    {
63 2
        return $this->getNode()->getParent()->getName();
64
    }
65
66
    /**
67
     * Returns the full qualified name of a class, an interface, a method or
68
     * a function.
69
     *
70
     * @return string
71
     */
72 1
    public function getFullQualifiedName()
73
    {
74 1
        return sprintf(
75 1
            '%s\\%s::%s()',
76 1
            $this->getNamespaceName(),
77 1
            $this->getParentName(),
78 1
            $this->getName()
79
        );
80
    }
81
82
    /**
83
     * Returns <b>true</b> when the underlying method is declared as abstract or
84
     * is declared as child of an interface.
85
     *
86
     * @return boolean
87
     */
88 2
    public function isAbstract()
89
    {
90 2
        return $this->getNode()->isAbstract();
0 ignored issues
show
The method isAbstract() does not exist on PDepend\Source\AST\ASTArtifact. It seems like you code against a sub-type of PDepend\Source\AST\ASTArtifact such as PDepend\Source\AST\AbstractASTClassOrInterface or PDepend\Source\AST\ASTMethod. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

90
        return $this->getNode()->/** @scrutinizer ignore-call */ isAbstract();
Loading history...
91
    }
92
93
    /**
94
     * Checks if this node has a suppressed annotation for the given rule
95
     * instance.
96
     *
97
     * @param \PHPMD\Rule $rule
98
     * @return boolean
99
     */
100 11
    public function hasSuppressWarningsAnnotationFor(Rule $rule)
101
    {
102 11
        if (parent::hasSuppressWarningsAnnotationFor($rule)) {
103 3
            return true;
104
        }
105
106 8
        return $this->getParentType()->hasSuppressWarningsAnnotationFor($rule);
107
    }
108
109
    /**
110
     * Returns the parent class or interface instance.
111
     *
112
     * @return \PHPMD\Node\AbstractTypeNode
113
     */
114 11
    public function getParentType()
115
    {
116 11
        $parentNode = $this->getNode()->getParent();
117
118 11
        if ($parentNode instanceof ASTTrait) {
119 1
            return new TraitNode($parentNode);
120
        }
121
122 10
        if ($parentNode instanceof ASTClass) {
123 8
            return new ClassNode($parentNode);
124
        }
125
126 2
        if ($parentNode instanceof ASTEnum) {
127
            return new EnumNode($parentNode);
128
        }
129
130 2
        return new InterfaceNode($parentNode);
131
    }
132
133
    /**
134
     * Returns <b>true</b> when this method is the initial method declaration.
135
     * Otherwise this method will return <b>false</b>.
136
     *
137
     * @return boolean
138
     * @since 1.2.1
139
     */
140 8
    public function isDeclaration()
141
    {
142 8
        if ($this->isPrivate()) {
143 1
            return true;
144
        }
145
146 7
        $methodName = strtolower($this->getName());
147
148 7
        $parentNode = $this->getNode()->getParent();
149 7
        foreach ($parentNode->getInterfaces() as $parentType) {
150 1
            $methods = $parentType->getAllMethods();
151 1
            if (isset($methods[$methodName])) {
152 1
                return false;
153
            }
154
        }
155
156 6
        $parentType = $parentNode->getParentClass();
157 6
        if (is_object($parentType)) {
158 3
            $methods = $parentType->getAllMethods();
159 3
            if (isset($methods[$methodName])) {
160 2
                return false;
161
            }
162
        }
163
164 4
        return true;
165
    }
166
}
167