0, true); // The ones that matter most, so a change to the regex above that quietly // matched nothing would show up here rather than as a passing empty scan. foreach (array('Debug', 'Info', 'Warning', 'Error') as $expected) { check($expected.' is among them', in_array($expected, $names, true), true); } /** * Is this file's code in namespace ZM? * @param array $tokens the file's tokens * @return bool */ function is_namespace_zm($tokens) { foreach ($tokens as $i => $token) { if (!is_array($token) or $token[0] !== T_NAMESPACE) continue; for ($j = $i + 1; $j < count($tokens); $j++) { $next = $tokens[$j]; if (is_array($next) and $next[0] === T_WHITESPACE) continue; return (is_array($next) and $next[1] === 'ZM'); } } return false; } /** * The unqualified logger calls in one file. * @param string $src the file's contents * @param array $names the logger function names * @return array one "line Name()" string per call */ function unqualified_calls($src, $names) { $tokens = @token_get_all($src); if (!$tokens) return array(); // A file in namespace ZM may call them bare: php resolves a function name // against the current namespace before falling back to the global one. if (is_namespace_zm($tokens)) return array(); $significant = array(); foreach ($tokens as $token) { if (is_array($token) and in_array($token[0], array(T_WHITESPACE, T_COMMENT, T_DOC_COMMENT), true)) continue; $significant[] = $token; } $found = array(); for ($i = 0; $i < count($significant); $i++) { $token = $significant[$i]; if (!is_array($token) or $token[0] !== T_STRING) continue; if (!in_array($token[1], $names, true)) continue; // A call, not a mention. if (!isset($significant[$i + 1]) or $significant[$i + 1] !== '(') continue; // Not a method, a static call, a definition, or already qualified. A // qualified name arrives as T_NAME_QUALIFIED or T_NAME_FULLY_QUALIFIED on // php 8 and so never reaches here as a bare T_STRING, but the separator // check keeps this honest on php 7 too. $prev = isset($significant[$i - 1]) ? $significant[$i - 1] : null; if ($prev === '->' or $prev === '::') continue; if (is_array($prev) and in_array($prev[0], array( T_OBJECT_OPERATOR, T_DOUBLE_COLON, T_FUNCTION, T_NEW, T_NS_SEPARATOR), true)) continue; $found[] = $token[2].' '.$token[1].'()'; } return $found; } echo "every logger call under web/ resolves\n"; $offenders = array(); $scanned = 0; $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($root.'/web')); foreach ($iterator as $file) { if ($file->isDir() or $file->getExtension() !== 'php') continue; $path = $file->getPathname(); // web/api is cakephp with its own logging, and assets are third party. if (strpos($path, '/api/') !== false) continue; if (strpos($path, '/assets/') !== false) continue; $scanned++; $calls = unqualified_calls(file_get_contents($path), $names); foreach ($calls as $call) { $offenders[] = substr($path, strlen($root) + 1).':'.$call; } } check('a useful number of files were scanned', $scanned > 100, true); check('none of them calls a logger function unqualified', $offenders, array()); echo "and the scan can tell the difference\n"; // A scan that found nothing because it looks for nothing would pass the check // above, so it is given one of each to classify. check('a bare call in the global namespace is caught', unqualified_calls('Error("x");', $names), array()); check('nor is a static one', unqualified_calls('