Yaffs site version 1.1
[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_;
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 Class_) {
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                     $msg = sprintf('Abstract function %s cannot contain body', $name);
44                     throw new FatalErrorException($msg, 0, E_ERROR, null, $node->getLine());
45                 }
46             }
47         }
48     }
49
50     /**
51      * @throws RuntimeException if the node is a non-abstract class with abstract methods
52      *
53      * @param Node $node
54      */
55     public function leaveNode(Node $node)
56     {
57         if ($node instanceof Class_) {
58             $count = count($this->abstractMethods);
59             if ($count > 0 && !$node->isAbstract()) {
60                 $msg = sprintf(
61                     'Class %s contains %d abstract method%s must therefore be declared abstract or implement the remaining methods (%s)',
62                     $node->name,
63                     $count,
64                     ($count === 1) ? '' : 's',
65                     implode(', ', $this->abstractMethods)
66                 );
67                 throw new FatalErrorException($msg, 0, E_ERROR, null, $node->getLine());
68             }
69         }
70     }
71 }