Improve script for PHP 7
authorTomas Mysik <tmysik@netbeans.org>
Wed, 04 May 2016 09:38:38 +0200
changeset 63259e44b44935db
parent 6310 f5c395a0c043
child 6330 e15a7f854ef4
Improve script for PHP 7
php/sigfiles/generate.php
     1.1 --- a/php/sigfiles/generate.php	Wed Mar 30 14:57:16 2016 +0200
     1.2 +++ b/php/sigfiles/generate.php	Wed May 04 09:38:38 2016 +0200
     1.3 @@ -20,7 +20,7 @@
     1.4  $splitFiles = true;
     1.5  $phpdocDir = null;
     1.6  
     1.7 -$phpDir = "php5";
     1.8 +$phpDir = "php";
     1.9  
    1.10  // Parse arguments:
    1.11  $argv = $_SERVER["argv"];
    1.12 @@ -793,7 +793,8 @@
    1.13  	if ($functionRef->returnsReference()) {
    1.14  		print "&";
    1.15  	}
    1.16 -	print "{$functionRef->getName()} (";
    1.17 +	$functionName = $functionRef->getName();
    1.18 +	print "$functionName(";
    1.19  	$parameters = @$functionsDoc[$funckey]['parameters'];
    1.20  	if ($parameters) {
    1.21  		print_parameters ($parameters);
    1.22 @@ -801,6 +802,11 @@
    1.23  		print_parameters_ref ($functionRef->getParameters());
    1.24  	}
    1.25  	print ")";
    1.26 +        $returntype = sanitizeType(@$functionsDoc[$funckey]['returntype']);
    1.27 +        if ($returntype
    1.28 +                && $functionName !== '__construct') {
    1.29 +            print ': ' . $returntype;
    1.30 +        }
    1.31          $body = true;
    1.32          if ($classRef != null && $classRef->isInterface()) {
    1.33              $body = false;
    1.34 @@ -832,26 +838,8 @@
    1.35  			if ($i++ > 0) {
    1.36  				print ", ";
    1.37  			}
    1.38 -			$type = $parameter['type'];
    1.39 -                        if (strpos($type, '|') !== false) {
    1.40 -                            // fix 'MyClass|YourClass' cases
    1.41 -                            $type = '';
    1.42 -                        }
    1.43 -			if ($type && !in_array($type, array(
    1.44 -                            'mixed',
    1.45 -                            'string',
    1.46 -                            'int',
    1.47 -                            'bool',
    1.48 -                            'object',
    1.49 -                            'callback',
    1.50 -                            'resource',
    1.51 -                            'string|array',
    1.52 -                            'bitmask',
    1.53 -                            'name',
    1.54 -                            'number',
    1.55 -                            'float',
    1.56 -                            'string|int',
    1.57 -                        ))) {
    1.58 +			$type = sanitizeType($parameter['type']);
    1.59 +			if ($type) {
    1.60  				print "{$type} ";
    1.61  			}
    1.62  			if (@$parameter['isreference']) {
    1.63 @@ -862,10 +850,12 @@
    1.64  			if (@$parameter['isoptional']) {
    1.65  				if (array_key_exists('defaultvalue', $parameter)) {
    1.66  					$value = $parameter['defaultvalue'];
    1.67 -                                        if (is_numeric ($value)
    1.68 -                                                || in_array(strtolower($value), array('true', 'false', 'null'))
    1.69 +                                        if ((is_numeric ($value) && $type != 'string')
    1.70 +                                                || in_array(strtolower($value), array('true', 'false', 'null', 'array()'))
    1.71                                                  || (substr($value, 0, 1) == '\'' && substr($value, -1) == '\'')
    1.72 -                                                || (substr($value, 0, 1) == '"' && substr($value, -1) == '"')) {
    1.73 +                                                || (substr($value, 0, 1) == '"' && substr($value, -1) == '"')
    1.74 +                                                || (substr($value, 0, 2) == '__' && substr($value, -2) == '__')
    1.75 +                                                || isConstant($value)) {
    1.76                                              // no apostrophes
    1.77                                          } else {
    1.78                                              $value = "'{$value}'";
    1.79 @@ -1571,6 +1561,43 @@
    1.80  
    1.81  }
    1.82  
    1.83 +function sanitizeType($type) {
    1.84 +    if (!trim($type)) {
    1.85 +        return '';
    1.86 +    }
    1.87 +    if (strpos($type, '|') !== false) {
    1.88 +        // ignore 'MyClass|YourClass' cases
    1.89 +        return '';
    1.90 +    }
    1.91 +    if (in_array($type, [
    1.92 +        'mixed',
    1.93 +        'object',
    1.94 +        'callback',
    1.95 +        'resource',
    1.96 +        'bitmask',
    1.97 +        'name',
    1.98 +        'number',
    1.99 +        'void',
   1.100 +        'scalar',
   1.101 +    ])) {
   1.102 +        return '';
   1.103 +    }
   1.104 +    $convert = [
   1.105 +        'boolean' => 'bool',
   1.106 +    ];
   1.107 +    return str_replace(array_keys($convert), $convert, $type);
   1.108 +}
   1.109 +
   1.110 +function isConstant($value) {
   1.111 +    $values = explode(' | ', $value);
   1.112 +    foreach ($values as $v) {
   1.113 +        if (!preg_match('/^(\\w+\\:\\:)?[A-Z0-9_]+$/', $v)) {
   1.114 +            return false;
   1.115 +        }
   1.116 +    }
   1.117 +    return true;
   1.118 +}
   1.119 +
   1.120  /**
   1.121   * Prints usage help to the screen, and exits from program
   1.122   */