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 PhpParser\Node\Stmt\ClassMethod;
17 use Psy\Exception\FatalErrorException;
20 * The abstract class pass handles abstract classes and methods, complaining if there are too few or too many of either.
22 class AbstractClassPass extends CodeCleanerPass
25 private $abstractMethods;
28 * @throws RuntimeException if the node is an abstract function with a body
32 public function enterNode(Node $node)
34 if ($node instanceof Class_) {
36 $this->abstractMethods = array();
37 } elseif ($node instanceof ClassMethod) {
38 if ($node->isAbstract()) {
39 $name = sprintf('%s::%s', $this->class->name, $node->name);
40 $this->abstractMethods[] = $name;
42 if ($node->stmts !== null) {
43 $msg = sprintf('Abstract function %s cannot contain body', $name);
44 throw new FatalErrorException($msg, 0, E_ERROR, null, $node->getLine());
51 * @throws RuntimeException if the node is a non-abstract class with abstract methods
55 public function leaveNode(Node $node)
57 if ($node instanceof Class_) {
58 $count = count($this->abstractMethods);
59 if ($count > 0 && !$node->isAbstract()) {
61 'Class %s contains %d abstract method%s must therefore be declared abstract or implement the remaining methods (%s)',
64 ($count === 1) ? '' : 's',
65 implode(', ', $this->abstractMethods)
67 throw new FatalErrorException($msg, 0, E_ERROR, null, $node->getLine());