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;
14 use PhpParser\Node\Scalar\LNumber;
15 use PhpParser\Node\Stmt\Declare_;
16 use PhpParser\Node\Stmt\DeclareDeclare;
17 use Psy\Exception\FatalErrorException;
20 * Provide implicit strict types declarations for for subsequent execution.
22 * The strict types pass remembers the last strict types declaration:
24 * declare(strict_types=1);
26 * ... which it then applies implicitly to all future evaluated code, until it
27 * is replaced by a new declaration.
29 class StrictTypesPass extends CodeCleanerPass
31 const EXCEPTION_MESSAGE = 'strict_types declaration must have 0 or 1 as its value';
33 private $strictTypes = false;
36 * If this is a standalone strict types declaration, remember it for later.
38 * Otherwise, apply remembered strict types declaration to to the code until
39 * a new declaration is encountered.
41 * @throws FatalErrorException if an invalid `strict_types` declaration is found
45 public function beforeTraverse(array $nodes)
47 if (version_compare(PHP_VERSION, '7.0', '<')) {
51 $prependStrictTypes = $this->strictTypes;
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());
62 $this->strictTypes = $value->value === 1;
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);