From 1747352a5b4995eee390ba9eba2e77ec2c1e4586 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 16 Aug 2026 16:25:37 +0900 Subject: build(linters): forbid direct std::env access PHP keeps three separate environment storages and docs/dev/env-vars- porting.md maps each to its own shim construct. Reaching for std::env silently picks one, so the porting target has to be chosen by reading the PHP source rather than by whichever Rust call is at hand. Detect var/var_os/vars/vars_os/set_var/remove_var. current_dir, args, consts, temp_dir, current_exe and the path split/join helpers are not environment storage and stay allowed. Co-Authored-By: Claude Opus 5 (1M context) --- scripts/linters/src/Linters/NoDirectEnvAccess.php | 75 +++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 scripts/linters/src/Linters/NoDirectEnvAccess.php (limited to 'scripts/linters/src') diff --git a/scripts/linters/src/Linters/NoDirectEnvAccess.php b/scripts/linters/src/Linters/NoDirectEnvAccess.php new file mode 100644 index 00000000..5276279a --- /dev/null +++ b/scripts/linters/src/Linters/NoDirectEnvAccess.php @@ -0,0 +1,75 @@ +findEnvAccesses($path, $relative)); + } + + return $errors; + } + + /** @return list */ + private function findEnvAccesses(string $path, string $relative): array + { + $errors = []; + $names = implode('|', self::BANNED_ENV_FUNCTIONS); + + foreach (file($path) as $idx => $raw) { + $code = explode('//', $raw, 2)[0]; + + if (preg_match_all("/\bstd::env::({$names})\b/", $code, $m)) { + foreach ($m[1] as $name) { + $errors[] = "{$relative}:" . ($idx + 1) . ": use of `std::env::{$name}`"; + } + } + + if (preg_match_all('/\bstd::env::\{([^}]*)\}/', $code, $m)) { + foreach ($m[1] as $group) { + foreach (explode(',', $group) as $entry) { + $name = preg_split('/\s+as\s+/', trim($entry))[0]; + if (!in_array($name, self::BANNED_ENV_FUNCTIONS, true)) { + continue; + } + $errors[] = "{$relative}:" . ($idx + 1) . ": import of `std::env::{$name}`"; + } + } + } + } + + return array_values(array_unique($errors)); + } +} -- cgit v1.3.1-4-g156e