Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | 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
 *
200 lars 6
 * (c) 2012-2023 Justin Hileman
148 lars 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\Stmt\Class_;
16
use Psy\Exception\FatalErrorException;
17
 
18
/**
19
 * The final class pass handles final classes.
20
 */
21
class FinalClassPass extends CodeCleanerPass
22
{
23
    private $finalClasses;
24
 
25
    /**
26
     * @param array $nodes
27
     *
28
     * @return Node[]|null Array of nodes
29
     */
30
    public function beforeTraverse(array $nodes)
31
    {
32
        $this->finalClasses = [];
33
    }
34
 
35
    /**
36
     * @throws FatalErrorException if the node is a class that extends a final class
37
     *
38
     * @param Node $node
39
     *
40
     * @return int|Node|null Replacement node (or special return value)
41
     */
42
    public function enterNode(Node $node)
43
    {
44
        if ($node instanceof Class_) {
45
            if ($node->extends) {
46
                $extends = (string) $node->extends;
47
                if ($this->isFinalClass($extends)) {
48
                    $msg = \sprintf('Class %s may not inherit from final class (%s)', $node->name, $extends);
49
                    throw new FatalErrorException($msg, 0, \E_ERROR, null, $node->getLine());
50
                }
51
            }
52
 
53
            if ($node->isFinal()) {
54
                $this->finalClasses[\strtolower($node->name)] = true;
55
            }
56
        }
57
    }
58
 
59
    /**
60
     * @param string $name Class name
61
     */
62
    private function isFinalClass(string $name): bool
63
    {
64
        if (!\class_exists($name)) {
65
            return isset($this->finalClasses[\strtolower($name)]);
66
        }
67
 
68
        $refl = new \ReflectionClass($name);
69
 
70
        return $refl->isFinal();
71
    }
72
}