Completed
Push — master ( c4d2d4...eae51e )
by Alexander
9s
created

src/NodeVisitor/RootNamespaceNormalizer.php (1 issue)

Upgrade to new PHP Analysis Engine

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
Bug Best Practice introduced by
The return type of return $nodes; (array) is incompatible with the return type declared by the interface PhpParser\NodeVisitor::beforeTraverse of type null|PhpParser\Node[].

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:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
52
    }
53
}
54