Passed
Push — master ( f66da6...7a87af )
by
unknown
02:36 queued 15s
created

fetchAndGenerateStubs()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 18
nc 3
nop 1
dl 0
loc 34
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
function fetchAndGenerateStubs(string $url): string {
4
	// Fetch the remote file
5
	$stubFileContents = file_get_contents($url);
6
	if ($stubFileContents === false) {
7
		throw new Exception("Unable to fetch the file from {$url}");
8
	}
9
10
	// Correcting the stub file contents for the specific error
11
	$stubFileContents = str_replace('res ', 'resource ', $stubFileContents);
12
13
	// Extract function definitions
14
	preg_match_all('/function\s+(\w+)\s*\(([^)]*)\)\s*:\s*([\w|?\\\\]+)\s*{}/', $stubFileContents, $matches, PREG_SET_ORDER);
15
16
	$stubFunctions = "";
17
	$stubFunctions .= "class Resource {}\n\n";
18
19
	foreach ($matches as $match) {
20
		$functionName = $match[1];
21
		$parameters = str_replace('resource', 'Resource', $match[2]);
22
		$returnType = str_replace('resource', 'Resource', $match[3]);
23
24
		// Generate the return value based on the return type
25
		$returnValue = generateReturnValue($returnType);
26
27
		// Generate PHPDoc for function
28
		$phpDoc = generatePHPDoc($parameters, $returnType);
29
30
		$stubFunctions .= "{$phpDoc}\n";
31
		$stubFunctions .= "function {$functionName}({$parameters}): {$returnType} {\n";
32
		$stubFunctions .= "\treturn {$returnValue};\n";
33
		$stubFunctions .= "}\n\n";
34
	}
35
36
	return $stubFunctions;
37
}
38
39
function generateReturnValue(string $returnType) {
40
	// Determine the return value based on the type hint
41
	switch ($returnType) {
42
		case 'void':
43
			return '';
44
45
		case 'int':
46
		case 'int|false':
47
			return '0';
48
49
		case 'bool':
50
		case 'bool|false':
51
			return 'false';
52
53
		case 'string':
54
		case 'string|false':
55
			return "''";
56
57
		case 'array':
58
		case 'array|false':
59
			return '[]';
60
61
		case 'mixed':
62
			return 'null';
63
64
		case 'Resource':
65
		case 'Resource|false':
66
			return 'null';
67
68
		default:
69
			if (strpos($returnType, '|') !== false) {
70
				$types = explode('|', $returnType);
71
72
				return generateReturnValue($types[0]);
73
			}
74
75
			return 'null';
76
	}
77
}
78
79
function generatePHPDoc(string $parameters, string $returnType): string {
80
	$paramDocs = [];
81
	if ($parameters) {
82
		$params = explode(',', $parameters);
83
		foreach ($params as $param) {
84
			$param = trim($param);
85
			if (strpos($param, ' ') !== false) {
86
				[$type, $name] = explode(' ', $param);
87
				$paramDocs[] = " * @param {$type} {$name}";
88
			}
89
			else {
90
				$paramDocs[] = " * @param mixed {$param}";
91
			}
92
		}
93
	}
94
95
	$paramDocs[] = " * @return {$returnType}";
96
97
	return "/**\n" . implode("\n", $paramDocs) . "\n */";
98
}
99
100
// URL of the mapi.stub.php file
101
$url = 'https://raw.githubusercontent.com/grommunio/gromox/master/php_mapi/mapi.stub.php';
102
103
try {
104
	$stubFunctions = fetchAndGenerateStubs($url);
105
106
	// Generate autoloader.php content
107
	$autoloaderContent = "<?php\n\n";
108
	$autoloaderContent .= $stubFunctions;
109
110
	if (extension_loaded('mapi')) {
111
		mapi_load_mapidefs(1);
112
		$constants = get_defined_constants(true);
113
114
		// Filter the relevant constants
115
		$relevant_prefixes = ['PR_', 'PidLid', 'MAPI', 'ec', 'RPC_', 'SYNC_'];
116
		$relevant_constants = array_filter($constants['Core'], function ($key) use ($relevant_prefixes) {
117
			foreach ($relevant_prefixes as $prefix) {
118
				if (strpos($key, $prefix) === 0) {
119
					return true;
120
				}
121
			}
122
123
			return false;
124
		}, ARRAY_FILTER_USE_KEY);
125
126
		foreach ($relevant_constants as $name => $value) {
127
			$autoloaderContent .= "if (!defined('{$name}')) {\n";
128
			$autoloaderContent .= "\tdefine('{$name}', {$value});\n";
129
			$autoloaderContent .= "}\n";
130
		}
131
	}
132
	else {
133
		echo "The mapi module is not loaded.\n";
134
	}
135
136
	$autoloaderContent .= "?>";
137
138
	file_put_contents('autoloader.php', $autoloaderContent);
139
140
	echo "autoloader.php has been generated successfully.\n";
141
}
142
catch (Exception $e) {
143
	echo "Error: " . $e->getMessage() . "\n";
144
}
145