4 * This file is part of Psy Shell.
6 * (c) 2012-2017 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\CodeCleaner;
15 use PhpParser\Node\Stmt\Class_;
16 use Psy\Exception\FatalErrorException;
19 * The final class pass handles final classes.
21 class FinalClassPass extends CodeCleanerPass
23 private $finalClasses;
28 public function beforeTraverse(array $nodes)
30 $this->finalClasses = array();
34 * @throws RuntimeException if the node is a class that extends a final class
38 public function enterNode(Node $node)
40 if ($node instanceof Class_) {
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());
49 if ($node->isFinal()) {
50 $this->finalClasses[strtolower($node->name)] = true;
56 * @param string $name Class name
60 private function isFinalClass($name)
62 if (!class_exists($name)) {
63 return isset($this->finalClasses[strtolower($name)]);
66 $refl = new \ReflectionClass($name);
68 return $refl->isFinal();