Upgraded drupal core with security updates
[yaffs-website] / vendor / psy / psysh / src / Psy / CodeCleaner / StrictTypesPass.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\Scalar\LNumber;
15 use PhpParser\Node\Stmt\Declare_;
16 use PhpParser\Node\Stmt\DeclareDeclare;
17 use Psy\Exception\FatalErrorException;
18
19 /**
20  * Provide implicit strict types declarations for for subsequent execution.
21  *
22  * The strict types pass remembers the last strict types declaration:
23  *
24  *     declare(strict_types=1);
25  *
26  * ... which it then applies implicitly to all future evaluated code, until it
27  * is replaced by a new declaration.
28  */
29 class StrictTypesPass extends CodeCleanerPass
30 {
31     const EXCEPTION_MESSAGE = 'strict_types declaration must have 0 or 1 as its value';
32
33     private $strictTypes = false;
34
35     /**
36      * If this is a standalone strict types declaration, remember it for later.
37      *
38      * Otherwise, apply remembered strict types declaration to to the code until
39      * a new declaration is encountered.
40      *
41      * @throws FatalErrorException if an invalid `strict_types` declaration is found
42      *
43      * @param array $nodes
44      */
45     public function beforeTraverse(array $nodes)
46     {
47         if (version_compare(PHP_VERSION, '7.0', '<')) {
48             return;
49         }
50
51         $prependStrictTypes = $this->strictTypes;
52
53         foreach ($nodes as $key => $node) {
54             if ($node instanceof Declare_) {
55                 foreach ($node->declares as $declare) {
56                     if ($declare->key === 'strict_types') {
57                         $value = $declare->value;
58                         if (!$value instanceof LNumber || ($value->value !== 0 && $value->value !== 1)) {
59                             throw new FatalErrorException(self::EXCEPTION_MESSAGE, 0, E_ERROR, null, $node->getLine());
60                         }
61
62                         $this->strictTypes = $value->value === 1;
63                     }
64                 }
65             }
66         }
67
68         if ($prependStrictTypes) {
69             $first = reset($nodes);
70             if (!$first instanceof Declare_) {
71                 $declare = new Declare_(array(new DeclareDeclare('strict_types', new LNumber(1))));
72                 array_unshift($nodes, $declare);
73             }
74         }
75
76         return $nodes;
77     }
78 }