Skip to content

Commit

Permalink
Fix #19041: Fix PHP 8.1 issues
Browse files Browse the repository at this point in the history
  • Loading branch information
samdark authored Jan 14, 2022
1 parent 4bd551d commit 1271bc4
Show file tree
Hide file tree
Showing 49 changed files with 162 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
php: ['5.4', '5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0']
php: ['5.4', '5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1']

steps:
- name: Generate french locale
Expand Down
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,13 @@
"phpunit/phpunit-mock-objects": {
"Fix PHP 7 and 8 compatibility": "https://yiisoft.github.io/phpunit-patches/phpunit_mock_objects.patch"
},
"phpunit/php-file-iterator": {
"Fix PHP 8.1 compatibility": "https://yiisoft.github.io/phpunit-patches/phpunit_path_file_iterator.patch"
},
"phpunit/phpunit": {
"Fix PHP 7 compatibility": "https://yiisoft.github.io/phpunit-patches/phpunit_php7.patch",
"Fix PHP 8 compatibility": "https://yiisoft.github.io/phpunit-patches/phpunit_php8.patch"
"Fix PHP 8 compatibility": "https://yiisoft.github.io/phpunit-patches/phpunit_php8.patch",
"Fix PHP 8.1 compatibility": "https://yiisoft.github.io/phpunit-patches/phpunit_php81.patch"
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion framework/BaseYii.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public static function getVersion()
*/
public static function getAlias($alias, $throwException = true)
{
if (strncmp($alias, '@', 1) !== 0) {
if (strncmp((string)$alias, '@', 1) !== 0) {
// not an alias
return $alias;
}
Expand Down
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Yii Framework 2 Change Log
------------------------

- Bug #19138: Allow digits in language code (ntesic)
- Bug #19041: Fix PHP 8.1 issues (longthanhtran, samdark, pamparam83, sartor, githubjeka)


2.0.44 December 30, 2021
Expand Down
6 changes: 6 additions & 0 deletions framework/base/ArrayAccessTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ trait ArrayAccessTrait
* It will be implicitly called when you use `foreach` to traverse the collection.
* @return \ArrayIterator an iterator for traversing the cookies in the collection.
*/
#[\ReturnTypeWillChange]
public function getIterator()
{
return new \ArrayIterator($this->data);
Expand All @@ -36,6 +37,7 @@ public function getIterator()
* This method is required by Countable interface.
* @return int number of data elements.
*/
#[\ReturnTypeWillChange]
public function count()
{
return count($this->data);
Expand All @@ -46,6 +48,7 @@ public function count()
* @param mixed $offset the offset to check on
* @return bool
*/
#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
return isset($this->data[$offset]);
Expand All @@ -56,6 +59,7 @@ public function offsetExists($offset)
* @param int $offset the offset to retrieve element.
* @return mixed the element at the offset, null if no element is found at the offset
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return isset($this->data[$offset]) ? $this->data[$offset] : null;
Expand All @@ -66,6 +70,7 @@ public function offsetGet($offset)
* @param int $offset the offset to set element
* @param mixed $item the element value
*/
#[\ReturnTypeWillChange]
public function offsetSet($offset, $item)
{
$this->data[$offset] = $item;
Expand All @@ -75,6 +80,7 @@ public function offsetSet($offset, $item)
* This method is required by the interface [[\ArrayAccess]].
* @param mixed $offset the offset to unset element
*/
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
unset($this->data[$offset]);
Expand Down
6 changes: 5 additions & 1 deletion framework/base/ExitException.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class ExitException extends \Exception
public function __construct($status = 0, $message = null, $code = 0, $previous = null)
{
$this->statusCode = $status;
parent::__construct($message, $code, $previous);
if ($previous === null) {
parent::__construct((string)$message, $code);
} else {
parent::__construct((string)$message, $code, $previous);
}
}
}
5 changes: 5 additions & 0 deletions framework/base/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,7 @@ public function fields()
* This method is required by the interface [[\IteratorAggregate]].
* @return ArrayIterator an iterator for traversing the items in the list.
*/
#[\ReturnTypeWillChange]
public function getIterator()
{
$attributes = $this->getAttributes();
Expand All @@ -1008,6 +1009,7 @@ public function getIterator()
* @param string $offset the offset to check on.
* @return bool whether or not an offset exists.
*/
#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
return isset($this->$offset);
Expand All @@ -1020,6 +1022,7 @@ public function offsetExists($offset)
* @param string $offset the offset to retrieve element.
* @return mixed the element at the offset, null if no element is found at the offset
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return $this->$offset;
Expand All @@ -1032,6 +1035,7 @@ public function offsetGet($offset)
* @param string $offset the offset to set element
* @param mixed $value the element value
*/
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
$this->$offset = $value;
Expand All @@ -1043,6 +1047,7 @@ public function offsetSet($offset, $value)
* It is implicitly called when you use something like `unset($model[$offset])`.
* @param string $offset the offset to unset element
*/
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
$this->$offset = null;
Expand Down
2 changes: 1 addition & 1 deletion framework/base/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ protected function decrypt($data, $passwordBased, $secret, $info)
public function hkdf($algo, $inputKey, $salt = null, $info = null, $length = 0)
{
if (function_exists('hash_hkdf')) {
$outputKey = hash_hkdf($algo, $inputKey, $length, $info, $salt);
$outputKey = hash_hkdf((string)$algo, (string)$inputKey, $length, (string)$info, (string)$salt);
if ($outputKey === false) {
throw new InvalidArgumentException('Invalid parameters to hash_hkdf()');
}
Expand Down
4 changes: 4 additions & 0 deletions framework/caching/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ protected function addValues($data, $duration)
* @param string $key a key identifying the cached value
* @return bool
*/
#[\ReturnTypeWillChange]
public function offsetExists($key)
{
return $this->get($key) !== false;
Expand All @@ -537,6 +538,7 @@ public function offsetExists($key)
* @param string $key a key identifying the cached value
* @return mixed the value stored in cache, false if the value is not in the cache or expired.
*/
#[\ReturnTypeWillChange]
public function offsetGet($key)
{
return $this->get($key);
Expand All @@ -550,6 +552,7 @@ public function offsetGet($key)
* @param string $key the key identifying the value to be cached
* @param mixed $value the value to be cached
*/
#[\ReturnTypeWillChange]
public function offsetSet($key, $value)
{
$this->set($key, $value);
Expand All @@ -560,6 +563,7 @@ public function offsetSet($key, $value)
* This method is required by the interface [[\ArrayAccess]].
* @param string $key the key of the value to be deleted
*/
#[\ReturnTypeWillChange]
public function offsetUnset($key)
{
$this->delete($key);
Expand Down
4 changes: 2 additions & 2 deletions framework/console/controllers/HelpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,8 @@ protected function getSubCommandHelp($controller, $actionID)
*/
protected function formatOptionHelp($name, $required, $type, $defaultValue, $comment)
{
$comment = trim($comment);
$type = trim($type);
$comment = trim((string)$comment);
$type = trim((string)$type);
if (strncmp($type, 'bool', 4) === 0) {
$type = 'boolean, 0 or 1';
}
Expand Down
6 changes: 6 additions & 0 deletions framework/db/ArrayExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public function getDimension()
* The return value will be casted to boolean if non-boolean was returned.
* @since 2.0.14
*/
#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
return isset($this->value[$offset]);
Expand All @@ -119,6 +120,7 @@ public function offsetExists($offset)
* @return mixed Can return all value types.
* @since 2.0.14
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return $this->value[$offset];
Expand All @@ -137,6 +139,7 @@ public function offsetGet($offset)
* @return void
* @since 2.0.14
*/
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
$this->value[$offset] = $value;
Expand All @@ -152,6 +155,7 @@ public function offsetSet($offset, $value)
* @return void
* @since 2.0.14
*/
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
unset($this->value[$offset]);
Expand All @@ -167,6 +171,7 @@ public function offsetUnset($offset)
* The return value is cast to an integer.
* @since 2.0.14
*/
#[\ReturnTypeWillChange]
public function count()
{
return count($this->value);
Expand All @@ -181,6 +186,7 @@ public function count()
* @since 2.0.14.1
* @throws InvalidConfigException when ArrayExpression contains QueryInterface object
*/
#[\ReturnTypeWillChange]
public function getIterator()
{
$value = $this->getValue();
Expand Down
1 change: 1 addition & 0 deletions framework/db/BaseActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,7 @@ public static function instantiate($row)
* @param mixed $offset the offset to check on
* @return bool whether there is an element at the specified offset.
*/
#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
return $this->__isset($offset);
Expand Down
5 changes: 5 additions & 0 deletions framework/db/BatchQueryResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public function reset()
* Resets the iterator to the initial state.
* This method is required by the interface [[\Iterator]].
*/
#[\ReturnTypeWillChange]
public function rewind()
{
$this->reset();
Expand All @@ -124,6 +125,7 @@ public function rewind()
* Moves the internal pointer to the next dataset.
* This method is required by the interface [[\Iterator]].
*/
#[\ReturnTypeWillChange]
public function next()
{
if ($this->_batch === null || !$this->each || $this->each && next($this->_batch) === false) {
Expand Down Expand Up @@ -197,6 +199,7 @@ protected function getRows()
* This method is required by the interface [[\Iterator]].
* @return int the index of the current row.
*/
#[\ReturnTypeWillChange]
public function key()
{
return $this->_key;
Expand All @@ -207,6 +210,7 @@ public function key()
* This method is required by the interface [[\Iterator]].
* @return mixed the current dataset.
*/
#[\ReturnTypeWillChange]
public function current()
{
return $this->_value;
Expand All @@ -217,6 +221,7 @@ public function current()
* This method is required by the interface [[\Iterator]].
* @return bool whether there is a valid dataset at the current position.
*/
#[\ReturnTypeWillChange]
public function valid()
{
return !empty($this->_batch);
Expand Down
7 changes: 6 additions & 1 deletion framework/db/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,11 @@ protected function initConnection()
$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->emulatePrepare);
}
}

if (PHP_VERSION_ID >= 80100 && $this->getDriverName() === 'sqlite') {
$this->pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
}

if (!$this->isSybase && in_array($this->getDriverName(), ['mssql', 'dblib'], true)) {
$this->pdo->exec('SET ANSI_NULL_DFLT_ON ON');
}
Expand Down Expand Up @@ -1004,7 +1009,7 @@ function ($matches) {
public function getDriverName()
{
if ($this->_driverName === null) {
if (($pos = strpos($this->dsn, ':')) !== false) {
if (($pos = strpos((string)$this->dsn, ':')) !== false) {
$this->_driverName = strtolower(substr($this->dsn, 0, $pos));
} else {
$this->_driverName = strtolower($this->getSlavePdo()->getAttribute(PDO::ATTR_DRIVER_NAME));
Expand Down
6 changes: 6 additions & 0 deletions framework/db/DataReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ public function getRowCount()
* In this case, use "SELECT COUNT(*) FROM tableName" to obtain the number of rows.
* @return int number of rows contained in the result.
*/
#[\ReturnTypeWillChange]
public function count()
{
return $this->getRowCount();
Expand All @@ -216,6 +217,7 @@ public function getColumnCount()
* This method is required by the interface [[\Iterator]].
* @throws InvalidCallException if this method is invoked twice
*/
#[\ReturnTypeWillChange]
public function rewind()
{
if ($this->_index < 0) {
Expand All @@ -231,6 +233,7 @@ public function rewind()
* This method is required by the interface [[\Iterator]].
* @return int the index of the current row.
*/
#[\ReturnTypeWillChange]
public function key()
{
return $this->_index;
Expand All @@ -241,6 +244,7 @@ public function key()
* This method is required by the interface [[\Iterator]].
* @return mixed the current row.
*/
#[\ReturnTypeWillChange]
public function current()
{
return $this->_row;
Expand All @@ -250,6 +254,7 @@ public function current()
* Moves the internal pointer to the next row.
* This method is required by the interface [[\Iterator]].
*/
#[\ReturnTypeWillChange]
public function next()
{
$this->_row = $this->_statement->fetch();
Expand All @@ -261,6 +266,7 @@ public function next()
* This method is required by the interface [[\Iterator]].
* @return bool whether there is a row of data at current position.
*/
#[\ReturnTypeWillChange]
public function valid()
{
return $this->_row !== false;
Expand Down
Loading

0 comments on commit 1271bc4

Please sign in to comment.