Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
add PHP 8 to versions.xml.
<?php
//
// requirement
//
// - PHP 8
// - nikic/PHP-Parser
//
require_once('vendor/autoload.php');
use PhpParser\Error;
use PhpParser\NodeFinder;
use PhpParser\ParserFactory;
$class_and_methods = [];
function parse_extends_or_implemented(
string $classname, string $property,
PhpParser\Node\Stmt $stmt, array &$retval) : void {
global $class_and_methods;
if (!empty($stmt->$property)) {
$parentclass = "";
foreach ($stmt->$property as $entry) {
if ($entry instanceof PhpParser\Node\Name) {
$parentclass = $entry->parts[0];
} else if (is_array($entry)) {
$parentclass = $entry[0];
}
$parentclass = strtolower($parentclass);
if (array_key_exists($parentclass, $class_and_methods)) {
foreach ($class_and_methods[$parentclass] as $parent_method => $value) {
$sig = strtolower($classname) . "::" . strtolower($parent_method);
$class_and_methods[$classname][$parent_method] = true;
$retval[$sig] = true;
}
}
}
}
}
function parse_stub_files(string $stubfile) :array {
$retval = [];
global $class_and_methods;
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
try {
$code = file_get_contents($stubfile);
$stmts = $parser->parse($code);
foreach ($stmts as $stmt) {
if ($stmt instanceof PhpParser\Node\Stmt\Function_) {
$funcname = $stmt->name->name;
$retval[$funcname] = true;
}
if ($stmt instanceof PhpParser\Node\Stmt\Class_
|| $stmt instanceof PhpParser\Node\Stmt\Interface_) {
$classname = strtolower($stmt->name->name);
$retval[$classname] = true;
if (!empty($stmt->extends)) {
parse_extends_or_implemented($classname, "extends", $stmt, $retval);
}
if (!empty($stmt->implements)) {
parse_extends_or_implemented($classname, "implements", $stmt, $retval);
}
foreach ($stmt->stmts as $cls_stmt) {
if ($cls_stmt instanceof PhpParser\Node\Stmt\ClassMethod) {
$methodname = $cls_stmt->name->name;
$sig = strtolower($classname) . "::" . strtolower($methodname);
$class_and_methods[$classname][$methodname] = true;
$retval[$sig] = true;
}
}
}
}
} catch (Error $error) {
echo "Parse error: {$error->getMessage()}\n";
}
return $retval;
}
if (PHP_MAJOR_VERSION !== 8) {
die("Error: this script must run with PHP 8\n");
}
$versions_file_path = "/home/mumumu/work/phpdoc/doc-en-github/reference/reflection/versions.xml";
$stub_file_path = [
"/home/mumumu/build/php-src-github/Zend/zend_interfaces.stub.php",
"/home/mumumu/build/php-src-github/Zend/zend_exceptions.stub.php",
"/home/mumumu/build/php-src-github/./ext/reflection/php_reflection.stub.php",
];
$xml = simplexml_load_file($versions_file_path);
$stub = [];
foreach ($stub_file_path as $path) {
$stub = array_merge($stub, parse_stub_files($path));
}
foreach ($xml as $data) {
$funcname = strtolower($data["name"]);
$from = $data["from"];
$already_in_php8 = str_contains($from, "PHP 8");
$already_in_stub = array_key_exists($funcname, $stub);
$can_add = (!$already_in_php8 && $already_in_stub);
if ($can_add) {
$split_from = explode(",", $data["from"]);
$final_from = "";
for($i = 0; $i < count($split_from); $i++) {
$separator = ($final_from === "") ? "" : ",";
if (str_contains($split_from[$i], "PHP 7")
|| str_contains($split_from[$i], "PHP &gt;= 7")
|| str_contains($split_from[$i], "PHP > 7")
|| str_contains($split_from[$i], "PHP >= 7")) {
$final_from .= $separator . $split_from[$i] . ", PHP 8";
} else {
$final_from .= $separator . $split_from[$i];
}
}
$data["from"] = $final_from;
}
}
file_put_contents($versions_file_path, $xml->asXML());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment