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; } }