Skip to content

Commit

Permalink
PHP8.* Support
Browse files Browse the repository at this point in the history
* Language models, via improved generator.php - #148 #149 #150
* Enum validation, indexing and CA - #149
* Improved CA for class constants
* Improved inferencer for class constans
* Remove false positive on function/method declaration
* Enum icons
  • Loading branch information
zulus committed May 14, 2023
1 parent 3f505c7 commit ab64293
Show file tree
Hide file tree
Showing 345 changed files with 342,242 additions and 190 deletions.
80 changes: 73 additions & 7 deletions plugins/org.eclipse.php.core/Resources/language/generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,21 @@ function rewrite_phpdoc_return_types($funckey, $returnTypes)
foreach ($intClasses as $intClass) {
$intClassLower = strtolower($intClass);
if (! isset($processedClasses[$intClassLower]) || ! $processedClasses[$intClassLower]) {
print_class(new ReflectionClass($intClass));
if (version_compare(phpversion(), "8.1.0") >= 0 && enum_exists($intClassLower)) {
print_class(new ReflectionEnum($intClass));
} else {
print_class(new ReflectionClass($intClass));
}

}
}
if (version_compare(phpversion(), "5.4.0") >= 0) {
$intTraits = array_merge(get_declared_traits());
foreach ($intTraits as $intTrait) {
$intTraitLower = strtolower($intTrait);
if (! isset($processedClasses[$intTraitLower]) || ! $processedClasses[$intTraitLower]) {
print_class(new ReflectionClass($intTrait));
}
}
}

Expand All @@ -155,6 +169,9 @@ function rewrite_phpdoc_return_types($funckey, $returnTypes)
$intConstants['__DIR__'] = null;
$intConstants['__NAMESPACE__'] = null;
}
if (version_compare(phpversion(), "5.4.0") >= 0) {
$intConstants['__TRAIT__'] = null;
}
foreach ($intConstants as $name => $value) {
if (! isset($processedConstants[$name]) || ! $processedConstants[$name]) {
print_constant($name, $value);
Expand Down Expand Up @@ -240,13 +257,13 @@ function clean_php_identifier($name, $isInPhpdoc = false, $removeDollars = false
if ($isInPhpdoc) {
$name = preg_replace('/[^?\\\\$\\w_|[\\]]+/', '_', $name);
} else {
$name = preg_replace('/[^?\\\\$\\w_]+/', '_', $name);
$name = preg_replace('/[^?\\\\$\\w_|&?]+/', '_', $name);
}
} else {
if ($isInPhpdoc) {
$name = preg_replace('/[^$\\w_|[\\]]+/', '_', $name);
} else {
$name = preg_replace('/[^$\\w_]+/', '_', $name);
$name = preg_replace('/[^$\\w_|&?]+/', '_', $name);
}
}
if ($removeDollars) {
Expand Down Expand Up @@ -366,6 +383,9 @@ function parse_phpdoc_functions($phpdocDir)
$functionsDoc = array();
foreach ($xml_files as $xml_file) {
$xml = load_xml($xml_file);




if (preg_match('@<refentry.*?xml:id=["\'](.*?)["\'].*?>.*?<refname(?:\s(?:[^>]*?[^/>])?)?>(.*?)</refname>.*?<refpurpose(?:\s(?:[^>]*?[^/>])?)?>(.*?)</refpurpose>@s', $xml, $match)) {

Expand Down Expand Up @@ -406,6 +426,7 @@ function parse_phpdoc_functions($phpdocDir)
$functionsDoc[$refname]['returntype'] = trim($match[1]);
}
} else {

$functionsDoc[$refname]['returntype'] = trim($match[1]);
}
$functionsDoc[$refname]['methodname'] = trim($match[2]);
Expand Down Expand Up @@ -689,6 +710,7 @@ function print_class($classRef, $tabs = 0)
$classRefName = $classRef->getName();
$className = strtolower($classRefName);
$processedClasses[$className] = true;


print "\n";
if ($handleNamespaces) {
Expand All @@ -707,13 +729,42 @@ function print_class($classRef, $tabs = 0)
$lastBackslashIdx = false;
}
print_doccomment($classRef, $tabs);

if (version_compare(phpversion(), "8.0.0") >= 0) {
foreach ($classRef->getAttributes() as $attr) {
print_tabs($tabs);
print '#[';
print $attr->getName();
if (count($attr->getArguments())) {
print '(';
foreach ($attr->getArguments() as $arg) {
var_export($arg);
print ', ';
}
print ')';
}

print "]\n";
}
}
print_tabs($tabs);
if ($classRef->isFinal())
print "final ";
if (version_compare(phpversion(), "8.2.0") >= 0 && $classRef->isReadOnly()) {
print 'readonly ';
}
if (! $classRef->isInterface() && $classRef->isAbstract())
print "abstract ";
if ($classRef->isInterface()) {
print 'interface ';
} elseif ($classRef->isTrait()) {
print 'trait ';
} else if ($classRef instanceof ReflectionEnum) {
print 'enum ';
} else {
print 'class ';
}

print $classRef->isInterface() ? "interface " : "class ";
if ($lastBackslashIdx !== false) {
print clean_php_identifier(substr($classRef->getName(), $lastBackslashIdx + 1)) . " ";
} else {
Expand Down Expand Up @@ -893,16 +944,22 @@ function print_function($functionRef, $tabs = 0, $isMethod = false)
} else {
print "{$functionRef->getName()} (";
}

$parameters = isset($functionsDoc[$funckey]['parameters']) ? $functionsDoc[$funckey]['parameters'] : null;
if ($parameters) {
print_parameters($parameters);
} else {
print_parameters_ref($functionRef->getParameters());
}
print ')';
if ($functionRef->getReturnType() != null) {
print ': ' . ($functionRef->getReturnType()->allowsNull() ? '?' : '') . $functionRef->getReturnType()->__toString();

}
if ($functionRef instanceof ReflectionMethod && $functionRef->isAbstract()) {
print ");\n";
print "\n";
} else {
print ") {}\n";
print " {}\n";
}
}

Expand Down Expand Up @@ -973,6 +1030,8 @@ function print_parameters_ref($paramsRef)
$realType = build_php_type($className, $addGlobalNSPrefix, true, true);
$printType = build_php_type($className, $addGlobalNSPrefix === '\\' && class_exists($realType) ? '\\' : '', true);
print "{$printType} ";
} else if ($paramRef->getType() != null) {
print ($paramRef->getType()->allowsNull() ? '?' : '') . $paramRef->getType()->__toString() . ' ';
}
}
$name = $paramRef->getName() ? $paramRef->getName() : "var" . ($i + 1);
Expand Down Expand Up @@ -1027,7 +1086,9 @@ function print_constant($name, $value = null, $tabs = 0)
$processedConstants[$name] = true;

if ($value === null) {
$value = @constant($name);
try {
$value = @constant($name);
} catch(Throwable $e) {}
}
$value = escape_const_value($value);

Expand Down Expand Up @@ -1182,6 +1243,7 @@ function print_doccomment($ref, $tabs = 0)
if ($parameters) {
foreach ($parameters as $parameter) {
print_tabs($tabs);

if (clean_php_identifier($parameter['type'], true) === $parameter['type']) {
$realType = build_php_type($parameter['type'], $addGlobalNSPrefix, true, true);
$printType = build_php_type($parameter['type'], $addGlobalNSPrefix === '\\' && class_exists($realType) ? '\\' : '', true);
Expand Down Expand Up @@ -1305,6 +1367,10 @@ function print_tabs($tabs)
*/
function get_parameter_classname(ReflectionParameter $paramRef)
{
if ($paramRef->getType() != null) {
return ($paramRef->getType()->allowsNull() ? '?' :'') . $paramRef->getType()->__toString();

}
try {
if ($classRef = $paramRef->getClass()) {
return $classRef->getName();
Expand Down
104 changes: 104 additions & 0 deletions plugins/org.eclipse.php.core/Resources/language/php8.0/.list
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
Core.php
FFI.php
PDO.php
PDO_Firebird.php
PDO_ODBC.php
Phar.php
Reflection.php
SPL.php
SimpleXML.php
Zend Debugger.php
Zend OPcache.php
amf.php
amqp.php
apc.php
apcu.php
basic.php
bbcode.php
bcmath.php
big_int.php
bz2.php
cairo.php
calendar.php
com_dotnet.php
ctype.php
curl.php
date.php
dba.php
discount.php
dom.php
enchant.php
exif.php
fileinfo.php
filter.php
ftp.php
gd.php
gettext.php
gmp.php
hash.php
ibm_db2.php
iconv.php
imagick.php
imap.php
interbase.php
intl.php
json.php
ldap.php
libevent.php
libxml.php
magickwand.php
mbstring.php
mcrypt.php
memcache.php
memcached.php
mysqli.php
mysqlnd.php
oci8.php
odbc.php
openssl.php
pcntl.php
pcre.php
pdo_dblib.php
pdo_mysql.php
pdo_pgsql.php
pdo_sqlite.php
pgsql.php
phpdbg_webhelper.php
phpiredis.php
posix.php
pspell.php
pthreads.php
readline.php
recode.php
redis.php
session.php
shmop.php
snmp.php
soap.php
sockets.php
sodium.php
solr.php
sqlite3.php
sqlsrv.php
ssh2.php
standard.php
sysvmsg.php
sysvsem.php
sysvshm.php
tidy.php
timezonedb.php
tokenizer.php
twig.php
uuid.php
wddx.php
win32service.php
xdebug.php
xhprof.php
xml.php
xmlreader.php
xmlrpc.php
xmlwriter.php
xsl.php
yaml.php
zip.php
zlib.php

0 comments on commit ab64293

Please sign in to comment.