Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of Psy Shell.
5
 *
6
 * (c) 2012-2022 Justin Hileman
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Psy\CodeCleaner;
13
 
14
use PhpParser\Node;
15
use PhpParser\Node\Identifier;
16
use PhpParser\Node\Stmt\Class_;
17
use PhpParser\Node\Stmt\ClassMethod;
18
use PhpParser\Node\Stmt\Namespace_;
19
use Psy\Exception\FatalErrorException;
20
 
21
/**
22
 * Validate that the constructor method is not static, and does not have a
23
 * return type.
24
 *
25
 * Checks both explicit __construct methods as well as old-style constructor
26
 * methods with the same name as the class (for non-namespaced classes).
27
 *
28
 * As of PHP 5.3.3, methods with the same name as the last element of a
29
 * namespaced class name will no longer be treated as constructor. This change
30
 * doesn't affect non-namespaced classes.
31
 *
32
 * @author Martin Hasoň <martin.hason@gmail.com>
33
 */
34
class ValidConstructorPass extends CodeCleanerPass
35
{
36
    private $namespace;
37
 
38
    /**
39
     * @return Node[]|null Array of nodes
40
     */
41
    public function beforeTraverse(array $nodes)
42
    {
43
        $this->namespace = [];
44
    }
45
 
46
    /**
47
     * Validate that the constructor is not static and does not have a return type.
48
     *
49
     * @throws FatalErrorException the constructor function is static
50
     * @throws FatalErrorException the constructor function has a return type
51
     *
52
     * @param Node $node
53
     *
54
     * @return int|Node|null Replacement node (or special return value)
55
     */
56
    public function enterNode(Node $node)
57
    {
58
        if ($node instanceof Namespace_) {
59
            $this->namespace = isset($node->name) ? $node->name->parts : [];
60
        } elseif ($node instanceof Class_) {
61
            $constructor = null;
62
            foreach ($node->stmts as $stmt) {
63
                if ($stmt instanceof ClassMethod) {
64
                    // If we find a new-style constructor, no need to look for the old-style
65
                    if ('__construct' === \strtolower($stmt->name)) {
66
                        $this->validateConstructor($stmt, $node);
67
 
68
                        return;
69
                    }
70
 
71
                    // We found a possible old-style constructor (unless there is also a __construct method)
72
                    if (empty($this->namespace) && \strtolower($node->name) === \strtolower($stmt->name)) {
73
                        $constructor = $stmt;
74
                    }
75
                }
76
            }
77
 
78
            if ($constructor) {
79
                $this->validateConstructor($constructor, $node);
80
            }
81
        }
82
    }
83
 
84
    /**
85
     * @throws FatalErrorException the constructor function is static
86
     * @throws FatalErrorException the constructor function has a return type
87
     *
88
     * @param Node $constructor
89
     * @param Node $classNode
90
     */
91
    private function validateConstructor(Node $constructor, Node $classNode)
92
    {
93
        if ($constructor->isStatic()) {
94
            // For PHP Parser 4.x
95
            $className = $classNode->name instanceof Identifier ? $classNode->name->toString() : $classNode->name;
96
 
97
            $msg = \sprintf(
98
                'Constructor %s::%s() cannot be static',
99
                \implode('\\', \array_merge($this->namespace, (array) $className)),
100
                $constructor->name
101
            );
102
            throw new FatalErrorException($msg, 0, \E_ERROR, null, $classNode->getLine());
103
        }
104
 
105
        if (\method_exists($constructor, 'getReturnType') && $constructor->getReturnType()) {
106
            // For PHP Parser 4.x
107
            $className = $classNode->name instanceof Identifier ? $classNode->name->toString() : $classNode->name;
108
 
109
            $msg = \sprintf(
110
                'Constructor %s::%s() cannot declare a return type',
111
                \implode('\\', \array_merge($this->namespace, (array) $className)),
112
                $constructor->name
113
            );
114
            throw new FatalErrorException($msg, 0, \E_ERROR, null, $classNode->getLine());
115
        }
116
    }
117
}