create(ParserFactory::PREFER_PHP7); $result = []; class SinceExtractor extends NodeVisitorAbstract { private $file; private $result; public function __construct($file, &$result) { $this->file = $file; $this->result = &$result; } public function enterNode(Node $node) { $doc = $node->getDocComment(); $docText = $doc ? $doc->getText() : null; $since = $this->extractTag($docText, '@since'); $deprecated = $this->extractTag($docText, '@deprecated'); if ($node instanceof Node\Stmt\Function_) { $this->addResult($node->name->toString(), 'function', $since, $deprecated); } elseif ($node instanceof Node\Stmt\Class_ || $node instanceof Node\Stmt\Interface_ || $node instanceof Node\Stmt\Trait_) { $type = $node instanceof Node\Stmt\Class_ ? 'class' : ($node instanceof Node\Stmt\Interface_ ? 'interface' : 'trait'); $this->addResult($node->name->toString(), $type, $since, $deprecated); } elseif ($node instanceof Node\Stmt\ClassMethod && !$node->isPrivate()) { $class = $node->getAttribute('parent'); $className = $class instanceof Node\Stmt\Class_ && $class->name ? $class->name->toString() : 'Anonymous'; $methodName = $node->name->toString(); $methodSince = $since ?: ($class && $class->getDocComment() ? $this->extractTag($class->getDocComment()->getText(), '@since') : null); if ($className !== 'Anonymous' && $methodSince) { $this->addResult("$className::$methodName", 'method', $methodSince, $deprecated); } } elseif ( $node instanceof Node\Expr\FuncCall && $node->name instanceof Node\Name && in_array($node->name->toString(), ['do_action', 'apply_filters'], true) ) { $hookNameNode = $node->args[0]->value ?? null; if ($hookNameNode instanceof Node\Scalar\String_) { $hookName = $hookNameNode->value; $this->addResult($hookName, 'hook', $since, $deprecated); } } } private function extractTag($docText, $tag) { if ($docText && preg_match('/' . preg_quote($tag) . '\s+([0-9.]+)/', $docText, $matches)) { $version = $matches[1]; if ($version === 'MU') return '3.0.0'; if (preg_match('/^\d+\.\d+(\.\d+)?$/', $version)) return $version; } return null; } private function addResult($name, $type, $since, $deprecated) { if ($since) { $this->result[$name] = array_filter([ 'type' => $type, 'since' => $since, 'deprecated' => $deprecated, 'file' => $this->file ]); } } } $rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($sourceDir)); foreach ($rii as $file) { if ($file->isDir() || $file->getExtension() !== 'php') continue; $relativePath = str_replace($sourceDir . '/', '', $file->getPathname()); foreach ($excludedPaths as $excluded) { if (strpos($relativePath, $excluded) === 0) continue 2; } try { $code = file_get_contents($file->getPathname()); $ast = $parser->parse($code); $traverser = new NodeTraverser(); $traverser->addVisitor(new ParentConnectingVisitor()); $traverser->addVisitor(new SinceExtractor($relativePath, $result)); $traverser->traverse($ast); } catch (Error $e) { echo "Processing error {$relativePath}: {$e->getMessage()}\n"; } } file_put_contents($outputPath, json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); echo "✅ File generated in: {$outputPath}\n";