These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Parser Reflection API |
||
4 | * |
||
5 | * @copyright Copyright 2016, Lisachenko Alexander <[email protected]> |
||
6 | * |
||
7 | * This source file is subject to the license that is bundled |
||
8 | * with this source code in the file LICENSE. |
||
9 | */ |
||
10 | |||
11 | namespace Go\ParserReflection\NodeVisitor; |
||
12 | |||
13 | use PhpParser\Node\Name\FullyQualified; |
||
14 | use PhpParser\Node\Stmt\Declare_; |
||
15 | use PhpParser\Node\Stmt\Namespace_; |
||
16 | use PhpParser\NodeVisitorAbstract; |
||
17 | |||
18 | /** |
||
19 | * Visitor to normalize the root namespace for the files without the namespace (root namespace) |
||
20 | * |
||
21 | * File->Namespace->Statements |
||
22 | */ |
||
23 | class RootNamespaceNormalizer extends NodeVisitorAbstract |
||
24 | { |
||
25 | /** |
||
26 | * {@inheritdoc} |
||
27 | */ |
||
28 | 8 | public function beforeTraverse(array $nodes) |
|
29 | { |
||
30 | // namespaces can be only top-level nodes, so we can scan them directly |
||
31 | 8 | foreach ($nodes as $topLevelNode) { |
|
32 | 8 | if ($topLevelNode instanceof Namespace_) { |
|
33 | // file has namespace in it, nothing to change, returning null |
||
34 | 8 | return null; |
|
35 | } |
||
36 | } |
||
37 | |||
38 | // if we don't have a namespaces at all, this is global namespace, wrap everything in it, except declares |
||
39 | 1 | $lastDeclareOffset = 0; |
|
40 | 1 | foreach ($nodes as $lastDeclareOffset => $node) { |
|
41 | 1 | if (!$node instanceof Declare_) { |
|
42 | // $declareOffset now stores the position of first non-declare node statement |
||
43 | 1 | break; |
|
44 | } |
||
45 | } |
||
46 | // Wrap all statements into the namespace block |
||
47 | 1 | $globalNamespaceNode = new Namespace_(new FullyQualified(''), array_slice($nodes, $lastDeclareOffset)); |
|
48 | // Replace top-level nodes with namespaced node |
||
49 | 1 | array_splice($nodes, $lastDeclareOffset, count($nodes), [$globalNamespaceNode]); |
|
50 | |||
51 | 1 | return $nodes; |
|
0 ignored issues
–
show
|
|||
52 | } |
||
53 | } |
||
54 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.