Skip to content

Commit

Permalink
PHP 8.1 > Compatibility
Browse files Browse the repository at this point in the history
sfYamlInline, backport fix from Symfony1.

Doctrine_Hydrator_Graph fix array_map, rtrim(): Passing null to parameter #1 ($string) of type string is deprecated
I emmit the hypothese that this array_map was broken, because array_map result is not assigned.

Doctrine_Migration_Diff:333, str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated

Doctrine_Migration_Builder:78:, is_dir(): Passing null to parameter #1 ($filename) of type string is deprecated

Doctrine_Validator_Notblank, allow null value
HydrationListener, in HydrateTestCase.php, fix strtoupper(): Passing null to parameter #1 ($string) of type string is deprecated

internal_method_return_types
https://wiki.php.net/rfc/internal_method_return_types
see 2b2d173 for details
Doctrine_Collection_OnDemand
Doctrine_Validator_Exception

PHP 8.1 PDO stringify is now disable by default.

Activate it for Mysql + Sqlite
https://www.php.net/manual/en/migration81.incompatible.php#migration81.incompatible.pdo.mysql

PHP 8.1 Fix: Warning: strtotime() : Epoch doesn't fit in a PHP integer in Doctrine_Record.
This is only happening on 32bit system, because int 32bit could not map the whole strtotime date scope.
Example value: "0000-00-00 00:00:00"
Before 8.1 strtotime returns false, after it return false but also raise a Warning.
@ is slightly lowering performance, it should not trigger any unwanted error, as if format is invalid strtotime should return "false"
As this old project need BC for old system, seems the best fix.

PHP 8.1 > Automatic conversion of false to array is deprecated
Fix Doctrine_Record _invokedSaveHooks cannot assign array value to boolean
Declaration to array instead of boolean

PHP 8.1 > Serializable Phase Out
https://wiki.php.net/rfc/phase_out_serializable

PHP 7.4 add a new Serialize mecanism
PHP 8.1 made old method, "Serializable implementation" deprecated
PHP 9.0 (no release date at this moment) will drop the support.

Temporary Fix: Adding both method serialize/unserialize and __serialize/__unserialize

In order to be compatible with future PHP 9.0, once it will be release, we will have to drop the support to PHP Version before 7.4.

Currently a lot of Unix distribution in LTS are running a PHP Version older than 7.4 so moving to the final solution of "add return type" should break a lot of setup for the moment.

PHP 8.1 > internal_method_return_types
https://wiki.php.net/rfc/internal_method_return_types

PHP 8.0 added return type for abstract methods on Iterator, ArrayAccess, Countable, IteratorAggregate
PHP 8.1 made non implementation as a Deprecated Warning
PHP 9.0 (no release date at this moment) will drop the support.

Temporary Fix : adding this Attribute
#[\ReturnTypeWillChange]
Will drop the Deprecated warning.

Adding return type will break compatibility before PHP 7.4,
Return type has been added on PHP 7.0, but "mixed" special type is required, and it has been added on PHP 7.4.
In order to be compatible with future PHP 9.0, once it will be release, we will have to drop the support to PHP Version before 7.4

Currently a lot of Unix distribution in LTS are running a PHP Version older than 7.4 so moving to the final solution of "add return type" should break a lot of setup for the moment.

Update Travis to PHP up to 8.1

PHP 8.0 > Doctrine_Query:36, uncaught TypeError: Unsupported operand types: string % int

Doctrine_Parser_Xml:89, htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated

https://wiki.php.net/rfc/internal_method_return_types for
Doctrine_Node
Doctrine_Adapter_Mock
Doctrine_EventListener_TestLogger
Doctrine_Parser_Xml

Doctrine_Ticket_1254_TestCase, replace stftime() by date() with format adaptation.
  • Loading branch information
Tybaze authored and thePanz committed Oct 17, 2022
1 parent 27943d7 commit 50cb69e
Show file tree
Hide file tree
Showing 29 changed files with 243 additions and 70 deletions.
20 changes: 14 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
os: linux
dist: xenial
dist: focal
language: php

php:
- 7.0
- 7.1
- 7.2
- 7.3
- 7.4
- 8.0
- 8.1
- nightly

cache:
Expand All @@ -18,15 +16,25 @@ jobs:
fast_finish: true
allow_failures:
- php: nightly
- php: 5.3
include:
- php: 5.3
dist: precise
- php: 5.4
dist: precise
dist: trusty
- php: 5.5
dist: trusty
- php: 5.6
dist: trusty
- php: 7.0
dist: xenial
- php: 7.1
dist: xenial
- php: 7.2
dist: xenial
- php: 7.3
dist: xenial


services:
- mysql
Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/Access.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public function __isset($name)
* @param string $name
* @return void
*/
#[\ReturnTypeWillChange]
public function __unset($name)
{
return $this->remove($name);
Expand All @@ -100,6 +101,7 @@ public function __unset($name)
* @param mixed $offset
* @return boolean Whether or not this object contains $offset
*/
#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
return $this->contains($offset);
Expand All @@ -112,6 +114,7 @@ public function offsetExists($offset)
* @param mixed $offset
* @return mixed
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
// array notation with no index was causing 'undefined variable: $offset' notices in php7,
Expand All @@ -131,6 +134,7 @@ public function offsetGet($offset)
* @param mixed $value
* @return void
*/
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
if ( ! isset($offset)) {
Expand All @@ -146,6 +150,7 @@ public function offsetSet($offset, $value)
* @see set, offsetSet, __set
* @param mixed $offset
*/
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
return $this->remove($offset);
Expand Down
1 change: 1 addition & 0 deletions lib/Doctrine/Adapter/Mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ public function lastInsertId()
*
* @return integer $count
*/
#[\ReturnTypeWillChange]
public function count()
{
return count($this->_queries);
Expand Down
40 changes: 33 additions & 7 deletions lib/Doctrine/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,38 @@ public function setData(array $data)
$this->data = $data;
}


/**
* This method is automatically called when this Doctrine_Collection is serialized
*
* @return array
* @return string
*/
public function serialize()
{
$vars = $this->__serialize();

return serialize($vars);
}

/**
* This method is automatically called everytime a Doctrine_Collection object is unserialized
*
* @return void
*/
public function unserialize($serialized)
{
$array = unserialize($serialized);

$this->__unserialize($array);
}

/**
* Serializes the current instance for php 7.4+
*
* @return array
*/
public function __serialize() {

$vars = get_object_vars($this);

unset($vars['reference']);
Expand All @@ -160,22 +185,21 @@ public function serialize()

$vars['_table'] = $vars['_table']->getComponentName();

return serialize($vars);
return $vars;
}

/**
* This method is automatically called everytime a Doctrine_Collection object is unserialized
* Unserializes a Doctrine_Collection instance for php 7.4+
*
* @return void
* @param string $serialized A serialized Doctrine_Collection instance
*/
public function unserialize($serialized)
public function __unserialize($data)
{
$manager = Doctrine_Manager::getInstance();
$connection = $manager->getCurrentConnection();

$array = unserialize($serialized);

foreach ($array as $name => $values) {
foreach ($data as $name => $values) {
$this->$name = $values;
}

Expand Down Expand Up @@ -432,6 +456,7 @@ public function getKeys()
*
* @return integer
*/
#[\ReturnTypeWillChange]
public function count()
{
return count($this->data);
Expand Down Expand Up @@ -1036,6 +1061,7 @@ public function free($deep = false)
*
* @return Iterator
*/
#[\ReturnTypeWillChange]
public function getIterator()
{
$data = $this->data;
Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/Collection/OnDemand.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ private function _hydrateCurrent()
}
}

#[\ReturnTypeWillChange]
public function rewind()
{
$this->index = 0;
Expand All @@ -73,23 +74,27 @@ public function rewind()
$this->_hydrateCurrent();
}

#[\ReturnTypeWillChange]
public function key()
{
return $this->index;
}

#[\ReturnTypeWillChange]
public function current()
{
return $this->_current;
}

#[\ReturnTypeWillChange]
public function next()
{
$this->_current = null;
$this->index++;
$this->_hydrateCurrent();
}

#[\ReturnTypeWillChange]
public function valid()
{
if ( ! is_null($this->_current) && $this->_current !== false) {
Expand Down
35 changes: 31 additions & 4 deletions lib/Doctrine/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,7 @@ public function getTables()
*
* @return ArrayIterator SPL ArrayIterator object
*/
#[\ReturnTypeWillChange]
public function getIterator()
{
return new ArrayIterator($this->tables);
Expand All @@ -1188,6 +1189,7 @@ public function getIterator()
*
* @return integer
*/
#[\ReturnTypeWillChange]
public function count()
{
return $this->_count;
Expand Down Expand Up @@ -1606,16 +1608,16 @@ public function __toString()
return Doctrine_Lib::getConnectionAsString($this);
}


/**
* Serialize. Remove database connection(pdo) since it cannot be serialized
*
* @return string $serialized
*/
public function serialize()
{
$vars = get_object_vars($this);
$vars['dbh'] = null;
$vars['isConnected'] = false;
$vars = $this->__serialize();

return serialize($vars);
}

Expand All @@ -1629,7 +1631,32 @@ public function unserialize($serialized)
{
$array = unserialize($serialized);

foreach ($array as $name => $values) {
$this->__unserialize($array);
}

/**
* Serialize. Remove database connection(pdo) since it cannot be serialized for PHP 7.4+
*
* @return array
*/
public function __serialize()
{
$vars = get_object_vars($this);
$vars['dbh'] = null;
$vars['isConnected'] = false;

return $vars;
}

/**
* Unserialize. Recreate connection from serialized content PHP 7.4+
*
* @param array $data
* @return void
*/
public function __unserialize($data)
{
foreach ($data as $name => $values) {
$this->$name = $values;
}
}
Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/Connection/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public function __construct(Doctrine_Manager $manager, $adapter)

$this->properties['varchar_max_length'] = 255;

// PHP8.1 require default to true to keep BC
// https://www.php.net/manual/en/migration81.incompatible.php#migration81.incompatible.pdo.mysql
// Can be overwritten by user later
$this->setAttribute(Doctrine_Core::ATTR_STRINGIFY_FETCHES, true);

parent::__construct($manager, $adapter);
}

Expand Down
16 changes: 9 additions & 7 deletions lib/Doctrine/Connection/Profiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public function __construct() {
* @return boolean
*/
public function setFilterQueryType() {
}

}
/**
* method overloader
* this method is used for invoking different listeners, for the full
Expand Down Expand Up @@ -109,7 +109,7 @@ public function __call($m, $a)
* @param mixed $key
* @return Doctrine_Event
*/
public function get($key)
public function get($key)
{
if (isset($this->events[$key])) {
return $this->events[$key];
Expand All @@ -123,7 +123,7 @@ public function get($key)
*
* @return array all events in an array
*/
public function getAll()
public function getAll()
{
return $this->events;
}
Expand All @@ -134,17 +134,19 @@ public function getAll()
*
* @return ArrayIterator
*/
#[\ReturnTypeWillChange]
public function getIterator()
{
return new ArrayIterator($this->events);
}

/**
* count
*
*
* @return integer
*/
public function count()
#[\ReturnTypeWillChange]
public function count()
{
return count($this->events);
}
Expand All @@ -154,7 +156,7 @@ public function count()
*
* @return Doctrine_Event
*/
public function pop()
public function pop()
{
$event = array_pop($this->events);
if ($event !== null)
Expand Down
18 changes: 17 additions & 1 deletion lib/Doctrine/Connection/Sqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,15 @@ public function __construct(Doctrine_Manager $manager, $adapter)
'identifier_quoting' => true,
'pattern_escaping' => false,
);
parent::__construct($manager, $adapter);
parent::__construct($manager, $adapter);

if ($this->isConnected) {

// PHP8.1 require default to true to keep BC
// https://www.php.net/manual/en/migration81.incompatible.php#migration81.incompatible.pdo.sqlite
// Can be overwritten by user later
$this->dbh->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);

$this->dbh->sqliteCreateFunction('mod', array('Doctrine_Expression_Sqlite', 'modImpl'), 2);
$this->dbh->sqliteCreateFunction('concat', array('Doctrine_Expression_Sqlite', 'concatImpl'));
$this->dbh->sqliteCreateFunction('md5', 'md5', 1);
Expand All @@ -87,8 +93,18 @@ public function connect()
return false;
}

// If customer configure it
$hasConfigureStringify = (isset($this->pendingAttributes[Doctrine_Core::ATTR_STRINGIFY_FETCHES]));

parent::connect();

if(!$hasConfigureStringify) {
// PHP8.1 require default to true to keep BC
// https://www.php.net/manual/en/migration81.incompatible.php#migration81.incompatible.pdo.sqlite
// Can be overwritten by user later
$this->dbh->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
}

$this->dbh->sqliteCreateFunction('mod', array('Doctrine_Expression_Sqlite', 'modImpl'), 2);
$this->dbh->sqliteCreateFunction('concat', array('Doctrine_Expression_Sqlite', 'concatImpl'));
$this->dbh->sqliteCreateFunction('md5', 'md5', 1);
Expand Down
Loading

0 comments on commit 50cb69e

Please sign in to comment.