forked from php-stubs/wordpress-stubs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.sh
executable file
·71 lines (61 loc) · 2.67 KB
/
generate.sh
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
#!/usr/bin/env bash
HEADER=$'/**\n * Generated stub declarations for WordPress.\n * @see https://wordpress.org\n * @see https://github.com/php-stubs/wordpress-stubs\n */'
FILE="wordpress-stubs.php"
set -e
test -f "$FILE"
test -d "source/wordpress"
# Download dependencies
if [ ! -d vendor ]; then
composer update
fi
# Exclude globals.
"$(dirname "$0")/vendor/bin/generate-stubs" \
--force \
--finder=finder.php \
--visitor=visitor.php \
--header="$HEADER" \
--functions \
--classes \
--interfaces \
--traits \
--out="$FILE"
# Shim the global $wpdb declaration, since it's actually set up inside a function call.
if grep -qFx 'namespace {' "$FILE"; then
printf '\nnamespace {\n/**\n * WordPress database abstraction object.\n * @var wpdb\n */\n$wpdb = \\null;\n}\n' >>"$FILE"
else
printf '\n/**\n * WordPress database abstraction object.\n * @var wpdb\n */\n$wpdb = \\null;\n' >>"$FILE"
fi
if [ -r source/wordpress/wp-includes/Requests/Cookie/Jar.php ]; then
# Add ReturnTypeWillChange attribute to PHP 8-incompatible methods.
declare -r -a REQUESTS_V1_METHODS=(
"Requests_Cookie_Jar::getIterator"
"Requests_Cookie_Jar::offsetExists"
"Requests_Cookie_Jar::offsetGet"
"Requests_Cookie_Jar::offsetSet"
"Requests_Cookie_Jar::offsetUnset"
"Requests_Utility_CaseInsensitiveDictionary::getIterator"
"Requests_Utility_CaseInsensitiveDictionary::offsetExists"
"Requests_Utility_CaseInsensitiveDictionary::offsetGet"
"Requests_Utility_CaseInsensitiveDictionary::offsetSet"
"Requests_Utility_CaseInsensitiveDictionary::offsetUnset"
"Requests_Utility_FilteredIterator::current"
"Requests_Utility_FilteredIterator::__unserialize"
"Requests_Utility_FilteredIterator::unserialize"
)
for METHOD in "${REQUESTS_V1_METHODS[@]}"; do
# Get the line number where the method is defined.
LINE="$(php -r "require 'wordpress-stubs.php'; print (new ReflectionMethod('${METHOD}'))->getStartLine();")"
if [ -z "${LINE}" ]; then
continue
fi
echo "${METHOD} is defined on line ${LINE}."
# Check the previous line for ReturnTypeWillChange attribute.
if sed -e "$((LINE - 1)) !d" "${FILE}" | grep -q -F '#[ReturnTypeWillChange]'; then
continue
fi
# Grab leading whitespace on the current line so we can indent ReturnTypeWillChange correctly.
LEADING_WHITESPACES="$(sed -e "${LINE} !d;s#^\\(\\s\\+\\).*\$#\\1#" "${FILE}")"
# Insert the ReturnTypeWillChange attribute.
sed -i -e "${LINE} i\\" -e "${LEADING_WHITESPACES}#[ReturnTypeWillChange]" "${FILE}"
done
fi