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_ as ClassStmt;
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 ClassStmt) {
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 throw new FatalErrorException(sprintf('Abstract function %s cannot contain body', $name));
50 * @throws RuntimeException if the node is a non-abstract class with abstract methods
54 public function leaveNode(Node $node)
56 if ($node instanceof ClassStmt) {
57 $count = count($this->abstractMethods);
58 if ($count > 0 && !$node->isAbstract()) {
59 throw new FatalErrorException(sprintf(
60 'Class %s contains %d abstract method%s must therefore be declared abstract or implement the remaining methods (%s)',
63 ($count === 0) ? '' : 's',
64 implode(', ', $this->abstractMethods)