X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Fpsy%2Fpsysh%2Fsrc%2FCodeCleaner%2FStrictTypesPass.php;fp=vendor%2Fpsy%2Fpsysh%2Fsrc%2FCodeCleaner%2FStrictTypesPass.php;h=81bf839f4550a152d31f2b1bc86aebf101130d43;hp=0000000000000000000000000000000000000000;hb=af6d1fb995500ae68849458ee10d66abbdcfb252;hpb=680c79a86e3ed402f263faeac92e89fb6d9edcc0 diff --git a/vendor/psy/psysh/src/CodeCleaner/StrictTypesPass.php b/vendor/psy/psysh/src/CodeCleaner/StrictTypesPass.php new file mode 100644 index 000000000..81bf839f4 --- /dev/null +++ b/vendor/psy/psysh/src/CodeCleaner/StrictTypesPass.php @@ -0,0 +1,87 @@ +atLeastPhp7 = version_compare(PHP_VERSION, '7.0', '>='); + } + + /** + * If this is a standalone strict types declaration, remember it for later. + * + * Otherwise, apply remembered strict types declaration to to the code until + * a new declaration is encountered. + * + * @throws FatalErrorException if an invalid `strict_types` declaration is found + * + * @param array $nodes + */ + public function beforeTraverse(array $nodes) + { + if (!$this->atLeastPhp7) { + return; + } + + $prependStrictTypes = $this->strictTypes; + + foreach ($nodes as $key => $node) { + if ($node instanceof Declare_) { + foreach ($node->declares as $declare) { + // For PHP Parser 4.x + $declareKey = $declare->key instanceof Identifier ? $declare->key->toString() : $declare->key; + if ($declareKey === 'strict_types') { + $value = $declare->value; + if (!$value instanceof LNumber || ($value->value !== 0 && $value->value !== 1)) { + throw new FatalErrorException(self::EXCEPTION_MESSAGE, 0, E_ERROR, null, $node->getLine()); + } + + $this->strictTypes = $value->value === 1; + } + } + } + } + + if ($prependStrictTypes) { + $first = reset($nodes); + if (!$first instanceof Declare_) { + $declare = new Declare_([new DeclareDeclare('strict_types', new LNumber(1))]); + array_unshift($nodes, $declare); + } + } + + return $nodes; + } +}