annotate vendor/psy/psysh/src/CodeCleaner/AbstractClassPass.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents
children 129ea1e6d783
rev   line source
Chris@13 1 <?php
Chris@13 2
Chris@13 3 /*
Chris@13 4 * This file is part of Psy Shell.
Chris@13 5 *
Chris@13 6 * (c) 2012-2018 Justin Hileman
Chris@13 7 *
Chris@13 8 * For the full copyright and license information, please view the LICENSE
Chris@13 9 * file that was distributed with this source code.
Chris@13 10 */
Chris@13 11
Chris@13 12 namespace Psy\CodeCleaner;
Chris@13 13
Chris@13 14 use PhpParser\Node;
Chris@13 15 use PhpParser\Node\Stmt\Class_;
Chris@13 16 use PhpParser\Node\Stmt\ClassMethod;
Chris@13 17 use Psy\Exception\FatalErrorException;
Chris@13 18
Chris@13 19 /**
Chris@13 20 * The abstract class pass handles abstract classes and methods, complaining if there are too few or too many of either.
Chris@13 21 */
Chris@13 22 class AbstractClassPass extends CodeCleanerPass
Chris@13 23 {
Chris@13 24 private $class;
Chris@13 25 private $abstractMethods;
Chris@13 26
Chris@13 27 /**
Chris@13 28 * @throws RuntimeException if the node is an abstract function with a body
Chris@13 29 *
Chris@13 30 * @param Node $node
Chris@13 31 */
Chris@13 32 public function enterNode(Node $node)
Chris@13 33 {
Chris@13 34 if ($node instanceof Class_) {
Chris@13 35 $this->class = $node;
Chris@13 36 $this->abstractMethods = [];
Chris@13 37 } elseif ($node instanceof ClassMethod) {
Chris@13 38 if ($node->isAbstract()) {
Chris@13 39 $name = sprintf('%s::%s', $this->class->name, $node->name);
Chris@13 40 $this->abstractMethods[] = $name;
Chris@13 41
Chris@13 42 if ($node->stmts !== null) {
Chris@13 43 $msg = sprintf('Abstract function %s cannot contain body', $name);
Chris@13 44 throw new FatalErrorException($msg, 0, E_ERROR, null, $node->getLine());
Chris@13 45 }
Chris@13 46 }
Chris@13 47 }
Chris@13 48 }
Chris@13 49
Chris@13 50 /**
Chris@13 51 * @throws RuntimeException if the node is a non-abstract class with abstract methods
Chris@13 52 *
Chris@13 53 * @param Node $node
Chris@13 54 */
Chris@13 55 public function leaveNode(Node $node)
Chris@13 56 {
Chris@13 57 if ($node instanceof Class_) {
Chris@13 58 $count = count($this->abstractMethods);
Chris@13 59 if ($count > 0 && !$node->isAbstract()) {
Chris@13 60 $msg = sprintf(
Chris@13 61 'Class %s contains %d abstract method%s must therefore be declared abstract or implement the remaining methods (%s)',
Chris@13 62 $node->name,
Chris@13 63 $count,
Chris@13 64 ($count === 1) ? '' : 's',
Chris@13 65 implode(', ', $this->abstractMethods)
Chris@13 66 );
Chris@13 67 throw new FatalErrorException($msg, 0, E_ERROR, null, $node->getLine());
Chris@13 68 }
Chris@13 69 }
Chris@13 70 }
Chris@13 71 }