a6a20c00cda57fd0842e6b917c0cc93c0a9da034
[yaffs-website] / vendor / psy / psysh / src / Psy / CodeCleaner / AbstractClassPass.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_ as ClassStmt;
16 use PhpParser\Node\Stmt\ClassMethod;
17 use Psy\Exception\FatalErrorException;
18
19 /**
20  * The abstract class pass handles abstract classes and methods, complaining if there are too few or too many of either.
21  */
22 class AbstractClassPass extends CodeCleanerPass
23 {
24     private $class;
25     private $abstractMethods;
26
27     /**
28      * @throws RuntimeException if the node is an abstract function with a body
29      *
30      * @param Node $node
31      */
32     public function enterNode(Node $node)
33     {
34         if ($node instanceof ClassStmt) {
35             $this->class = $node;
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;
41
42                 if ($node->stmts !== null) {
43                     throw new FatalErrorException(sprintf('Abstract function %s cannot contain body', $name));
44                 }
45             }
46         }
47     }
48
49     /**
50      * @throws RuntimeException if the node is a non-abstract class with abstract methods
51      *
52      * @param Node $node
53      */
54     public function leaveNode(Node $node)
55     {
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)',
61                     $node->name,
62                     $count,
63                     ($count === 0) ? '' : 's',
64                     implode(', ', $this->abstractMethods)
65                 ));
66             }
67         }
68     }
69 }