Last active
September 8, 2020 13:47
-
-
Save paresy/063838339588b1fa0d48c7fa8aadae21 to your computer and use it in GitHub Desktop.
Build PHP functions stubs for PHPStorm inclusion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Copyright: Michael Maroszek | |
* Version: 1.3, 07.08.2019 | |
* Requires: IP-Symcon 5.2 or newer | |
*/ | |
echo "<?php" . PHP_EOL . PHP_EOL; | |
$ipsGlobals = Array( | |
"SELF" => 0, | |
"SENDER" => "", | |
"VALUE" => 0, | |
"OLDVALUE" => 0, | |
"OLDUPDATED" => 0, | |
"OLDCHANGED" => 0, | |
"VARIABLE" => 0, | |
"EVENT" => 0, | |
"TRIGGER" => 0, | |
"TARGET" => 0, | |
"ACTION" => 0, | |
"INSTANCE" => 0, | |
"THREAD" => 0, | |
"FORM" => 0, //Designer | |
"COMPONENT" => "", //Designer | |
"DIRECTION" => 0, //ShutterControl | |
"DURATION" => 0, //ShutterControl | |
"INSTANCE2" => 0, //ShutterControl | |
"STATUS" => 0, //StatusEvent | |
"STATUSTEXT" => "", //StatusEvent | |
"CONNECTION" => 0, //ISDN/VOIP | |
"DATA" => "", //ISDN/VOIP | |
"CONFIGURATOR" => "", //WebFront | |
"INSTANCES" => Array(), //HeatingControl | |
"INVERTS" => Array(), //HeatingControl | |
"CLIENTIP" => "", //ServerSocket | |
"CLIENTPORT" => 0 //ServerSocket | |
); | |
$docTypes = Array("bool", "int", "float", "string", "mixed", "array"); | |
//mixed is not an allowed PHP7 scalar type. lets just assue no specific type | |
$phpTypes = Array("bool", "int", "float", "string", "", "array"); | |
//map types to nicer returns which the IDE will autodetect | |
$phpReturns = Array("return true;", "return 0;", "return 0.0;", "return '';", "return '';", "return Array();"); | |
$php7Returns = Array( | |
"void" => "", | |
"bool" => "return true;", | |
"int" => "return 0;", | |
"double" => "return 0.0;", | |
"string" => "return '';", | |
"array" => "return Array();" | |
); | |
$php7Defaults = Array( | |
"bool" => "false", | |
"int" => "0", | |
"double" => "0.0", | |
"string" => "''", | |
"array" => "Array();" | |
); | |
//Print all global variables | |
echo "\$_IPS = " . var_export($ipsGlobals, true) . ";" . PHP_EOL; | |
echo PHP_EOL; | |
//Print all defined constants | |
$contants = get_defined_constants(true); | |
foreach($contants["IP-Symcon"] as $key => $value) { | |
echo "define(" . var_export($key, true) . ", " . var_export($value, true) . ");" . PHP_EOL; | |
} | |
echo PHP_EOL; | |
//This will fetch all functions that are declared in IP-Symcon | |
//If this is undefined you probably do not run this script in an IP-Symcon environment | |
$functions = IPS_GetFunctionList(0); | |
asort($functions); | |
foreach($functions as $function) { | |
$function = IPS_GetFunction($function); | |
echo "function ".$function['FunctionName']."("; | |
$parameters = Array(); | |
foreach($function['Parameters'] as $functionParameter) { | |
$parameter = "$".$functionParameter['Description']; | |
$type = $phpTypes[$functionParameter['Type_']]; | |
if($type != "") { | |
$parameter = $type . " " . $parameter; | |
} | |
$parameters[] = $parameter; | |
} | |
echo implode(", ", $parameters); | |
echo ") { " . $phpReturns[$function['Result']['Type_']] . " }" . PHP_EOL; | |
} | |
echo PHP_EOL; | |
//Lets build a dummy IPSModule class from reflection information | |
$class=new Reflectionclass('IPSModule'); | |
echo "class IPSModule {" . PHP_EOL; | |
echo "\tprotected \$InstanceID;" . PHP_EOL; | |
foreach($class->getMethods() as $method) { | |
echo "\t"; | |
if($method->isPublic()) | |
echo "public "; | |
if($method->isProtected()) | |
echo "protected "; | |
if($method->isPrivate()) | |
echo "private "; | |
if($method->isStatic()) | |
echo "static "; | |
echo "function " . $method->getName() . "("; | |
$parameters = Array(); | |
foreach($method->getParameters() as $methodParameter) { | |
$parameter = "$".$methodParameter->getName(); | |
if (version_compare(phpversion(), '7', '>=')) { | |
if($methodParameter->hasType()) { | |
$parameter = $methodParameter->getType() . " " . $parameter; | |
if($methodParameter->getPosition() >= $method->getNumberOfRequiredParameters()) { | |
if(array_key_exists($methodParameter->getType()->__toString(), $php7Defaults)) { | |
$parameter .= " = " . $php7Defaults[$methodParameter->getType()->__toString()]; | |
} | |
} | |
} | |
} | |
$parameters[] = $parameter; | |
} | |
echo implode(", ", $parameters); | |
if (version_compare(phpversion(), '7', '>=')) { | |
if($method->hasReturnType()) { | |
if(array_key_exists($method->getReturnType()->__toString(), $php7Returns)) { | |
echo ") { ". $php7Returns[$method->getReturnType()->__toString()] . " }" . PHP_EOL; | |
} else { | |
echo ") { return ''; }" . PHP_EOL; | |
} | |
} else { | |
echo ") {}" . PHP_EOL; | |
} | |
} else { | |
echo ") {}" . PHP_EOL; | |
} | |
} | |
echo "}"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment