Completed
Push — master ( a1c8e7...7d2da4 )
by Thomas
04:30
created

AbstractPhpStructVisitor   F

Complexity

Total Complexity 49

Size/Duplication

Total Lines 247
Duplicated Lines 4.05 %

Coupling/Cohesion

Components 1
Dependencies 25

Test Coverage

Coverage 99.39%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 49
c 2
b 1
f 1
lcom 1
cbo 25
dl 10
loc 247
ccs 163
cts 164
cp 0.9939
rs 3.0323

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getStruct() 0 3 1
D enterNode() 0 45 10
A visitStruct() 0 10 2
A visitClass() 0 1 1
A visitInterface() 0 1 1
A visitTrait() 0 1 1
A visitTraitUse() 0 5 2
A visitConstants() 0 6 2
A visitConstant() 0 8 1
A visitProperty() 0 3 1
A visitNamespace() 0 5 2
A visitUseStatement() 0 6 2
C visitMethod() 10 63 10
A getProperty() 0 17 2
A parseMemberDocblock() 0 14 3
A getValue() 0 14 4
A getVisibility() 0 11 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like AbstractPhpStructVisitor often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AbstractPhpStructVisitor, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace gossi\codegen\parser\visitor;
3
4
use gossi\codegen\model\AbstractPhpMember;
5
use gossi\codegen\model\AbstractPhpStruct;
6
use gossi\codegen\model\PhpConstant;
7
use gossi\codegen\model\PhpMethod;
8
use gossi\codegen\model\PhpParameter;
9
use gossi\codegen\model\PhpProperty;
10
use PhpParser\Node;
11
use PhpParser\Node\Const_;
12
use PhpParser\Node\Expr\ConstFetch;
13
use PhpParser\Node\Name;
14
use PhpParser\Node\Param;
15
use PhpParser\Node\Scalar\String_;
16
use PhpParser\Node\Stmt\ClassLike;
17
use PhpParser\Node\Stmt\ClassMethod;
18
use PhpParser\Node\Stmt\Namespace_;
19
use PhpParser\Node\Stmt\Property;
20
use PhpParser\Node\Stmt\TraitUse;
21
use PhpParser\Node\Stmt\UseUse;
22
use PhpParser\NodeTraverser;
23
use PhpParser\NodeVisitorAbstract;
24
use PhpParser\PrettyPrinter\Standard;
25
use PhpParser\Node\Stmt\Class_;
26
use PhpParser\Node\Stmt\Interface_;
27
use PhpParser\Node\Stmt\Trait_;
28
use PhpParser\Node\Stmt\ClassConst;
29
use PhpParser\Comment\Doc;
30
use gossi\docblock\tags\ParamTag;
31
32
abstract class AbstractPhpStructVisitor extends NodeVisitorAbstract {
33
	
34
	private $constMap = [
35
		'false' => false,
36
		'true' => true
37
	];
38
	
39
	protected $struct;
40
	
41 7
	public function __construct(AbstractPhpStruct $struct) {
42 7
		$this->struct = $struct;
43 7
	}
44
	
45
	/**
46
	 * @return AbstractPhpStruct
47
	 */
48 7
	public function getStruct() {
49 7
		return $this->struct;
50
	}
51
	
52 7
	public function enterNode(Node $node) {
53 7
		switch ($node->getType()) {
54 7
			case 'Stmt_Namespace':
55 7
				$this->visitNamespace($node);
0 ignored issues
show
Compatibility introduced by
$node of type object<PhpParser\Node> is not a sub-type of object<PhpParser\Node\Stmt\Namespace_>. It seems like you assume a concrete implementation of the interface PhpParser\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
56 7
				break;
57
				
58 7
			case 'Stmt_UseUse':
59 2
				$this->visitUseStatement($node);
0 ignored issues
show
Compatibility introduced by
$node of type object<PhpParser\Node> is not a sub-type of object<PhpParser\Node\Stmt\UseUse>. It seems like you assume a concrete implementation of the interface PhpParser\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
60 2
				break;
61
				
62 7
			case 'Stmt_Class':
63 5
				$this->visitStruct($node);
0 ignored issues
show
Compatibility introduced by
$node of type object<PhpParser\Node> is not a sub-type of object<PhpParser\Node\Stmt\ClassLike>. It seems like you assume a concrete implementation of the interface PhpParser\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
64 5
				$this->visitClass($node);
0 ignored issues
show
Compatibility introduced by
$node of type object<PhpParser\Node> is not a sub-type of object<PhpParser\Node\Stmt\Class_>. It seems like you assume a concrete implementation of the interface PhpParser\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
65 5
				break;
66
				
67 7
			case 'Stmt_Interface':
68 1
				$this->visitStruct($node);
0 ignored issues
show
Compatibility introduced by
$node of type object<PhpParser\Node> is not a sub-type of object<PhpParser\Node\Stmt\ClassLike>. It seems like you assume a concrete implementation of the interface PhpParser\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
69 1
				$this->visitInterface($node);
0 ignored issues
show
Compatibility introduced by
$node of type object<PhpParser\Node> is not a sub-type of object<PhpParser\Node\Stmt\Interface_>. It seems like you assume a concrete implementation of the interface PhpParser\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
70 1
				break;
71
				
72 7
			case 'Stmt_Trait':
73 1
				$this->visitStruct($node);
0 ignored issues
show
Compatibility introduced by
$node of type object<PhpParser\Node> is not a sub-type of object<PhpParser\Node\Stmt\ClassLike>. It seems like you assume a concrete implementation of the interface PhpParser\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
74 1
				$this->visitTrait($node);
0 ignored issues
show
Compatibility introduced by
$node of type object<PhpParser\Node> is not a sub-type of object<PhpParser\Node\Stmt\Trait_>. It seems like you assume a concrete implementation of the interface PhpParser\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
75 1
				break;
76
				
77 7
			case 'Stmt_TraitUse':
78 2
				$this->visitTraitUse($node);
0 ignored issues
show
Compatibility introduced by
$node of type object<PhpParser\Node> is not a sub-type of object<PhpParser\Node\Stmt\TraitUse>. It seems like you assume a concrete implementation of the interface PhpParser\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
79 2
				break;
80
				
81 7
			case 'Stmt_ClassConst':
82 2
				$this->visitConstants($node);
0 ignored issues
show
Compatibility introduced by
$node of type object<PhpParser\Node> is not a sub-type of object<PhpParser\Node\Stmt\ClassConst>. It seems like you assume a concrete implementation of the interface PhpParser\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
83 2
				break;
84
				
85 7
			case 'Stmt_Property':
86 3
				$this->visitProperty($node);
0 ignored issues
show
Compatibility introduced by
$node of type object<PhpParser\Node> is not a sub-type of object<PhpParser\Node\Stmt\Property>. It seems like you assume a concrete implementation of the interface PhpParser\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
87 3
				break;
88
				
89 7
			case 'Stmt_ClassMethod':
90 5
				$this->visitMethod($node);
0 ignored issues
show
Compatibility introduced by
$node of type object<PhpParser\Node> is not a sub-type of object<PhpParser\Node\Stmt\ClassMethod>. It seems like you assume a concrete implementation of the interface PhpParser\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
91 5
				break;
92 7
		}
93
// 		echo $node->getType() . "\n";
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
94
		
95
// 		return NodeTraverser::DONT_TRAVERSE_CHILDREN;
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
96 7
	}
97
	
98 7
	protected function visitStruct(ClassLike $node) {
99 7
		$this->struct->setName($node->name);
100
	
101 7
		if (($doc = $node->getDocComment()) !== null) {
102 4
			$this->struct->setDocblock($doc->getReformattedText());
103 4
			$docblock = $this->struct->getDocblock();
104 4
			$this->struct->setDescription($docblock->getShortDescription());
105 4
			$this->struct->setLongDescription($docblock->getLongDescription());
106 4
		}
107 7
	}
108
	
109
	protected function visitClass(Class_ $node) {}
110
	
111
	protected function visitInterface(Interface_ $node) {}
112
	
113
	protected function visitTrait(Trait_ $node) {}
0 ignored issues
show
Unused Code introduced by
The parameter $node is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
114
	
115 2
	protected function visitTraitUse(TraitUse $node) {
116 2
		foreach ($node->traits as $trait) {
117 2
			$this->struct->addTrait(implode('\\', $trait->parts));
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class gossi\codegen\model\AbstractPhpStruct as the method addTrait() does only exist in the following sub-classes of gossi\codegen\model\AbstractPhpStruct: gossi\codegen\model\PhpClass, gossi\codegen\model\PhpTrait. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
118 2
		}
119 2
	}
120
	
121 2
	protected function visitConstants(ClassConst $node) {
122 2
		$doc = $node->getDocComment();
123 2
		foreach ($node->consts as $const) {
124 2
			$this->visitConstant($const, $doc);
125 2
		}
126 2
	}
127
	
128 2
	protected function visitConstant(Const_ $node, Doc $doc = null) {
129 2
		$const = new PhpConstant($node->name, $this->getValue($node));
130 2
		$const->setValue($this->getValue($node->value));
131
132 2
		$this->parseMemberDocblock($const, $doc);
133
		
134 2
		$this->struct->setConstant($const);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class gossi\codegen\model\AbstractPhpStruct as the method setConstant() does only exist in the following sub-classes of gossi\codegen\model\AbstractPhpStruct: gossi\codegen\model\PhpClass, gossi\codegen\model\PhpInterface. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
135 2
	}
136
	
137 3
	protected function visitProperty(Property $node) {
138 3
		$this->struct->setProperty($this->getProperty($node));
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class gossi\codegen\model\AbstractPhpStruct as the method setProperty() does only exist in the following sub-classes of gossi\codegen\model\AbstractPhpStruct: gossi\codegen\model\PhpClass, gossi\codegen\model\PhpTrait. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
139 3
	}
140
	
141 7
	protected function visitNamespace(Namespace_ $node) {
142 7
		if ($node->name !== null) {
143 7
			$this->struct->setNamespace(implode('\\', $node->name->parts));
144 7
		}
145 7
	}
146
147 2
	protected function visitUseStatement(UseUse $node) {
148 2
		$name = implode('\\', $node->name->parts);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
149 2
		$alias = !empty($node->alias) ? $node->alias : null;
150
		
151 2
		$this->struct->addUseStatement($name, $alias);
152 2
	}
153
	
154 5
	protected function visitMethod(ClassMethod $node) {
155 5
		$m = new PhpMethod($node->name);
156 5
		$m->setAbstract($node->isAbstract());
157 5
		$m->setFinal($node->isFinal());
158 5
		$m->setVisibility($this->getVisibility($node));
159 5
		$m->setStatic($node->isStatic());
160 5
		$m->setReferenceReturned($node->returnsByRef());
161
		
162
		// docblock
163 5 View Code Duplication
		if (($doc = $node->getDocComment()) !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164 3
			$m->setDocblock($doc->getReformattedText());
165 3
			$docblock = $m->getDocblock();
166 3
			$m->setDescription($docblock->getShortDescription());
167 3
			$m->setLongDescription($docblock->getLongDescription());
168 3
		}
169
		
170
		// params
171 5
		$params = $m->getDocblock()->getTags('param');
172 5
		foreach ($node->params as $param) {
173
			/* @var $param Param */
174
			
175 2
			$p = new PhpParameter();
176 2
			$p->setName($param->name);
177 2
			$p->setPassedByReference($param->byRef);
178
			
179 2
			if (is_string($param->type)) {
180 1
				$p->setType($param->type);
181 2
			} else if ($param->type instanceof Name) {
182 1
				$p->setType(implode('\\', $param->type->parts));
183 1
			}
184
185 2
			$default = $param->default;
186 2
			if ($default !== null) {
187 2
				$p->setDefaultValue($this->getValue($default));
188 2
			}
189
			
190 2
			$tag = $params->find($p, function (ParamTag $t, $p) {
191 2
				return $t->getVariable() == '$' . $p->getName();
192 2
			});
193
			
194 2
			if ($tag !== null) {
195 2
				$p->setType($tag->getType(), $tag->getDescription());
196 2
			}
197
			
198 2
			$m->addParameter($p);
199 5
		}
200
		
201
		// return type and description
202 5
		$returns = $m->getDocblock()->getTags('return');
203 5 View Code Duplication
		if ($returns->size() > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
204 1
			$return = $returns->get(0);
205 1
			$m->setType($return->getType(), $return->getDescription());
206 1
		}
207
		
208
		// body
209 5
		$stmts = $node->getStmts();
210 5
		if (is_array($stmts) && count($stmts)) {
211 1
			$prettyPrinter = new Standard();
212 1
			$m->setBody($prettyPrinter->prettyPrint($stmts));
213 1
		}
214
215 5
		$this->struct->setMethod($m);
216 5
	}
217
	
218 3
	protected function getProperty(Property $node) {
219 3
		$prop = $node->props[0];
220
221 3
		$p = new PhpProperty($prop->name);
222
	
223 3
		$default = $prop->default;
224 3
		if ($default !== null) {
225 2
			$p->setDefaultValue($this->getValue($default));
226 2
		}
227
	
228 3
		$p->setStatic($node->isStatic());
229 3
		$p->setVisibility($this->getVisibility($node));
230
		
231 3
		$this->parseMemberDocblock($p, $node->getDocComment());
232
	
233 3
		return $p;
234
	}
235
	
236 4
	private function parseMemberDocblock($member, Doc $doc = null) {
237 4
		if ($doc !== null) {
238 2
			$member->setDocblock($doc->getReformattedText());
239 2
			$docblock = $member->getDocblock();
240 2
			$member->setDescription($docblock->getShortDescription());
241 2
			$member->setLongDescription($docblock->getLongDescription());
242
	
243 2
			$vars = $docblock->getTags('var');
244 2
			if ($vars->size() > 0) {
245 2
				$var = $vars->get(0);
246 2
				$member->setType($var->getType(), $var->getDescription());
247 2
			}
248 2
		}
249 4
	}
250
	
251 3
	private function getValue(Node $node) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
252 3
		if ($node instanceof ConstFetch) {
253 1
			$const = $node->name->parts[0];
254 1
			if (isset($this->constMap[$const])) {
255 1
				return $this->constMap[$const];
256
			}
257
			
258
			return $const;
259
		}
260
		
261 3
		if ($node instanceof String_) {
262 3
			return $node->value;
263
		}
264 2
	}
265
	
266 5
	private function getVisibility(Node $node) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
267 5
		if ($node->isPrivate()) {
1 ignored issue
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface PhpParser\Node as the method isPrivate() does only exist in the following implementations of said interface: PhpParser\Node\Stmt\ClassMethod, PhpParser\Node\Stmt\Property.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
268 3
			return AbstractPhpMember::VISIBILITY_PRIVATE;
269
		}
270
		
271 5
		if ($node->isProtected()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface PhpParser\Node as the method isProtected() does only exist in the following implementations of said interface: PhpParser\Node\Stmt\ClassMethod, PhpParser\Node\Stmt\Property.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
272 1
			return AbstractPhpMember::VISIBILITY_PROTECTED;
273
		}
274
		
275 5
		return AbstractPhpMember::VISIBILITY_PUBLIC;
276
	}
277
	
278
}
279