Yaffs site version 1.1
[yaffs-website] / vendor / psy / psysh / src / Psy / CodeCleaner / FinalClassPass.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2017 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\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     public function beforeTraverse(array $nodes)
29     {
30         $this->finalClasses = array();
31     }
32
33     /**
34      * @throws RuntimeException if the node is a class that extends a final class
35      *
36      * @param Node $node
37      */
38     public function enterNode(Node $node)
39     {
40         if ($node instanceof Class_) {
41             if ($node->extends) {
42                 $extends = (string) $node->extends;
43                 if ($this->isFinalClass($extends)) {
44                     $msg = sprintf('Class %s may not inherit from final class (%s)', $node->name, $extends);
45                     throw new FatalErrorException($msg, 0, E_ERROR, null, $node->getLine());
46                 }
47             }
48
49             if ($node->isFinal()) {
50                 $this->finalClasses[strtolower($node->name)] = true;
51             }
52         }
53     }
54
55     /**
56      * @param string $name Class name
57      *
58      * @return bool
59      */
60     private function isFinalClass($name)
61     {
62         if (!class_exists($name)) {
63             return isset($this->finalClasses[strtolower($name)]);
64         }
65
66         $refl = new \ReflectionClass($name);
67
68         return $refl->isFinal();
69     }
70 }