Skip to content

Commit a1dfa9f

Browse files
janw-meszepeviktor
andauthored
Add extra php attribute to methods for 8+ Fixes #52 (#53)
* Add extra php attribute to methods for 8+ These functions are giving notices on php 8+. The fix is adding a extra php attribute to the methods: #[ReturnTypeWillChange] * Improve Requests v1 processing * Fix spaces for Requests v1 * Fix escaping Co-authored-by: Viktor Szépe <viktor@szepe.net>
1 parent df4eef7 commit a1dfa9f

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

generate.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,35 @@ if grep -qFx 'namespace {' "$FILE"; then
3535
else
3636
printf '\n/**\n * WordPress database abstraction object.\n * @var wpdb\n */\n$wpdb = \\null;\n' >>"$FILE"
3737
fi
38+
39+
# Add ReturnTypeWillChange attribute to PHP 8-incompatible methods.
40+
declare -r -a REQUESTS_V1_METHODS=(
41+
'Requests_Utility_FilteredIterator::unserialize'
42+
'Requests_Utility_FilteredIterator::__unserialize'
43+
'Requests_Utility_FilteredIterator::current'
44+
'Requests_Cookie_Jar::offsetExists'
45+
'Requests_Cookie_Jar::offsetGet'
46+
'Requests_Cookie_Jar::offsetSet'
47+
'Requests_Cookie_Jar::offsetUnset'
48+
'Requests_Cookie_Jar::getIterator'
49+
'Requests_Utility_CaseInsensitiveDictionary::offsetExists'
50+
'Requests_Utility_CaseInsensitiveDictionary::offsetGet'
51+
'Requests_Utility_CaseInsensitiveDictionary::offsetSet'
52+
'Requests_Utility_CaseInsensitiveDictionary::offsetUnset'
53+
'Requests_Utility_CaseInsensitiveDictionary::getIterator'
54+
)
55+
for METHOD in "${REQUESTS_V1_METHODS[@]}"; do
56+
# Get the line number where the method is defined.
57+
LINE="$(php -r "require 'wordpress-stubs.php'; print (new ReflectionMethod('${METHOD}'))->getStartLine();")"
58+
echo "${METHOD} is defined on line ${LINE}."
59+
60+
# Check the previous line forr ReturnTypeWillChange attribute.
61+
if sed -e "$((LINE - 1)) !d" "${FILE}" | grep -q -F '#[ReturnTypeWillChange]'; then
62+
continue
63+
fi
64+
65+
# Grab leading whitespace on the current line so we can indent ReturnTypeWillChange correctly.
66+
LEADING_WHITESPACES="$(sed -e "${LINE} !d;s#^\\(\\s\\+\\).*\$#\\1#" "${FILE}")"
67+
# Insert the ReturnTypeWillChange attribute.
68+
sed -i -e "${LINE} i\\" -e "${LEADING_WHITESPACES}#[ReturnTypeWillChange]" "${FILE}"
69+
done

wordpress-stubs.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18963,6 +18963,7 @@ public function normalizeCookie($cookie, $key = \null)
1896318963
* @param string $key Item key
1896418964
* @return boolean Does the item exist?
1896518965
*/
18966+
#[ReturnTypeWillChange]
1896618967
public function offsetExists($key)
1896718968
{
1896818969
}
@@ -18972,6 +18973,7 @@ public function offsetExists($key)
1897218973
* @param string $key Item key
1897318974
* @return string|null Item value (null if offsetExists is false)
1897418975
*/
18976+
#[ReturnTypeWillChange]
1897518977
public function offsetGet($key)
1897618978
{
1897718979
}
@@ -18983,6 +18985,7 @@ public function offsetGet($key)
1898318985
* @param string $key Item name
1898418986
* @param string $value Item value
1898518987
*/
18988+
#[ReturnTypeWillChange]
1898618989
public function offsetSet($key, $value)
1898718990
{
1898818991
}
@@ -18991,6 +18994,7 @@ public function offsetSet($key, $value)
1899118994
*
1899218995
* @param string $key
1899318996
*/
18997+
#[ReturnTypeWillChange]
1899418998
public function offsetUnset($key)
1899518999
{
1899619000
}
@@ -18999,6 +19003,7 @@ public function offsetUnset($key)
1899919003
*
1900019004
* @return ArrayIterator
1900119005
*/
19006+
#[ReturnTypeWillChange]
1900219007
public function getIterator()
1900319008
{
1900419009
}
@@ -20954,6 +20959,7 @@ public function __construct(array $data = array())
2095420959
* @param string $key Item key
2095520960
* @return boolean Does the item exist?
2095620961
*/
20962+
#[ReturnTypeWillChange]
2095720963
public function offsetExists($key)
2095820964
{
2095920965
}
@@ -20963,6 +20969,7 @@ public function offsetExists($key)
2096320969
* @param string $key Item key
2096420970
* @return string|null Item value (null if offsetExists is false)
2096520971
*/
20972+
#[ReturnTypeWillChange]
2096620973
public function offsetGet($key)
2096720974
{
2096820975
}
@@ -20974,6 +20981,7 @@ public function offsetGet($key)
2097420981
* @param string $key Item name
2097520982
* @param string $value Item value
2097620983
*/
20984+
#[ReturnTypeWillChange]
2097720985
public function offsetSet($key, $value)
2097820986
{
2097920987
}
@@ -20982,6 +20990,7 @@ public function offsetSet($key, $value)
2098220990
*
2098320991
* @param string $key
2098420992
*/
20993+
#[ReturnTypeWillChange]
2098520994
public function offsetUnset($key)
2098620995
{
2098720996
}
@@ -20990,6 +20999,7 @@ public function offsetUnset($key)
2099020999
*
2099121000
* @return ArrayIterator
2099221001
*/
21002+
#[ReturnTypeWillChange]
2099321003
public function getIterator()
2099421004
{
2099521005
}
@@ -21721,12 +21731,14 @@ public function __construct($data, $callback)
2172121731
*
2172221732
* @return string
2172321733
*/
21734+
#[ReturnTypeWillChange]
2172421735
public function current()
2172521736
{
2172621737
}
2172721738
/**
2172821739
* @inheritdoc
2172921740
*/
21741+
#[ReturnTypeWillChange]
2173021742
public function unserialize($serialized)
2173121743
{
2173221744
}
@@ -21735,6 +21747,7 @@ public function unserialize($serialized)
2173521747
*
2173621748
* @phpcs:disable PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.MethodDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.NewMagicMethods.__unserializeFound
2173721749
*/
21750+
#[ReturnTypeWillChange]
2173821751
public function __unserialize($serialized)
2173921752
{
2174021753
}

0 commit comments

Comments
 (0)