File: check-missing-initializers.php

package info (click to toggle)
php-doc 20241205~git.dfcbb86%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 70,956 kB
  • sloc: xml: 968,269; php: 23,883; javascript: 671; sh: 177; makefile: 37
file content (76 lines) | stat: -rw-r--r-- 2,153 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/*
Introduction:
	- This script checks for optional parameters that do not utilize the <initializer> tag.
	- Pass in a path and it'll check it. The path might include all of phpdoc, or a simple extension
TODO:
	- Determine what initializer values should be as some cases aren't clear
*/

$opts = getopt('p:oh');

if (isset($opts['h'])) {
	usage();
}
if (empty($opts['p'])) {
	echo "\nERROR:\n - A path is required\n";
	usage();
}
if (!is_dir($opts['p'])) {
	echo "\nERROR:\n - Please pass in a real directory, unlike this mysterious '$opts[p]'\n";
	usage();
}

$empty = [];
$count_total = 0;
$count_empty = 0;

foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($opts['p'])) as $file) {

	$filepath = $file->getPathname();
	$filename = $file->getBasename();

	if (!$file->isFile() || pathinfo($filepath, PATHINFO_EXTENSION) !== 'xml') {
		continue;
	}
	
	$contents = file_get_contents($filepath);

	$matches = [];
	preg_match_all('@<methodparam choice="opt"><type>(.*)</type><parameter>(.*)</parameter>(.*)</methodparam>@', $contents, $matches);

	// Check if any optional parameters exist
	if (empty($matches)) {
		continue;
	}

	// Log optional parameters without default values
	// We use the <initializer> DocBook tag for this task.
	foreach ($matches[3] as $index => $match) {
		$count_total++;
		if (empty($match) || (!str_contains($match, '<initializer>'))) {
			$count_empty++;
			// index 2 corresponds to the param, index 1 to the type
			$empty[$filepath][$matches[2][$index]] = $matches[1][$index];
		}
	}
}

// This output could be more useful
if (array_key_exists('o', $opts)) {
    foreach ($empty as $file => $issues) {
        echo $file, ' has ', count($issues), ' optional parameters without initializers:', "\n";
        foreach ($issues as $param => $type) {
            echo '- Param "', $param, '" of type "', $type, "\"\n";
        }
    }
}

print "Found $count_total optional parameters, and $count_empty are empty.\n";

function usage() {
	echo "\nUSAGE:\n";
	echo "$ php {$_SERVER['SCRIPT_FILENAME']} -p /path/to/phpdoc/docs/to/check\n";
	echo "  Optional: Add -o to output the results.\n";
	exit;
}