Skip to content

Commit

Permalink
NEW PHP 8.1 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Feb 2, 2022
1 parent 6d90eec commit 9093da8
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 15 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"symfony/config": "^3.2 || ^4",
"symfony/translation": "^2.8 || ^3 || ^4",
"symfony/yaml": "^3.2 || ^4",
"php": "^7.3 || ^8",
"php": "^7.4 || ^8.0",
"ext-ctype": "*",
"ext-dom": "*",
"ext-hash": "*",
Expand Down
2 changes: 1 addition & 1 deletion src/Control/ContentNegotiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static function enabled_for($response)
if (ContentNegotiator::getEnabled()) {
return true;
} else {
return (substr($response->getBody(), 0, 5) == '<' . '?xml');
return (substr($response->getBody() ?: '', 0, 5) == '<' . '?xml');
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/Control/HTTPRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ public function isAjax()
* @param string $offset
* @return bool
*/
public function offsetExists($offset)
public function offsetExists($offset): bool
{
return isset($this->postVars[$offset]) || isset($this->getVars[$offset]);
}
Expand All @@ -445,17 +445,19 @@ public function offsetExists($offset)
* @param string $offset
* @return mixed
*/
# Remove this attribute when PHP 8.0 is the minimum version and set method return type to mixed
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return $this->requestVar($offset);
}

public function offsetSet($offset, $value)
public function offsetSet($offset, $value): void
{
$this->getVars[$offset] = $value;
}

public function offsetUnset($offset)
public function offsetUnset($offset): void
{
unset($this->getVars[$offset]);
unset($this->postVars[$offset]);
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Convert.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public static function raw2xml($val)
return $val;
}

return htmlspecialchars($val, ENT_QUOTES, 'UTF-8');
return htmlspecialchars($val ?: '', ENT_QUOTES, 'UTF-8');
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Injector/Injector.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ public function load($config = [])
}

// okay, actually include it now we know we're going to use it
if (file_exists($file)) {
if ($file && file_exists($file)) {
require_once $file;
}

Expand Down Expand Up @@ -524,7 +524,7 @@ public function convertServiceProperty($value)
}

// Evaluate constants surrounded by back ticks
if (preg_match('/^`(?<name>[^`]+)`$/', $value, $matches)) {
if (preg_match('/^`(?<name>[^`]+)`$/', $value ?: '', $matches)) {
$envValue = Environment::getEnv($matches['name']);
if ($envValue !== false) {
$value = $envValue;
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Manifest/ManifestFileFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public function isDirectoryModule($basename, $pathname, $depth)
protected function upLevels($pathname, $depth)
{
if ($depth < 0) {
return null;
return '';
}
while ($depth--) {
$pathname = dirname($pathname);
Expand Down
34 changes: 32 additions & 2 deletions src/Core/Manifest/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
use Exception;
use InvalidArgumentException;
use RuntimeException;
use Serializable;
use SilverStripe\Core\Path;
use SilverStripe\Dev\Deprecation;

/**
* Abstraction of a PHP Package. Can be used to retrieve information about Silverstripe CMS modules, and other packages
* managed via composer, by reading their `composer.json` file.
*/
class Module implements Serializable
class Module
{
/**
* @deprecated 4.1.0:5.0.0 Use Path::normalise() instead
Expand Down Expand Up @@ -185,11 +184,42 @@ public function getRelativePath()
return substr($this->path, strlen($this->basePath) + 1);
}

public function __serialize(): array
{
return [
'path' => $this->path,
'basePath' => $this->basePath,
'composerData' => $this->composerData
];
}

public function __unserialize(array $data): void
{
$this->path = $data['path'];
$this->basePath = $data['basePath'];
$this->composerData = $data['composerData'];
$this->resources = [];
}

/**
* The __serialize() magic method will be automatically used instead of this
*
* @return string
* @deprecated will be removed in 5.0
*/
public function serialize()
{
return json_encode([$this->path, $this->basePath, $this->composerData]);
}

/**
* The __unserialize() magic method will be automatically used instead of this almost all the time
* This method will be automatically used if existing serialized data was not saved as an associative array
* and the PHP version used in less than PHP 9.0
*
* @param string $serialized
* @deprecated will be removed in 5.0
*/
public function unserialize($serialized)
{
list($this->path, $this->basePath, $this->composerData) = json_decode($serialized, true);
Expand Down
24 changes: 21 additions & 3 deletions src/ORM/ValidationResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,20 +214,38 @@ public function combineAnd(ValidationResult $other)
return $this;
}

public function __serialize(): array
{
return [
'messages' => $this->messages,
'isValid' => $this->isValid()
];
}

public function __unserialize(array $data): void
{
$this->messages = $data['messages'];
$this->isValid = $data['isValid'];
}

/**
* String representation of object
* The __serialize() magic method will be automatically used instead of this
*
* @return string the string representation of the object or null
* @return string
* @deprecated will be removed in 5.0
*/
public function serialize()
{
return json_encode([$this->messages, $this->isValid]);
}

/**
* Constructs the object
* The __unserialize() magic method will be automatically used instead of this almost all the time
* This method will be automatically used if existing serialized data was not saved as an associative array
* and the PHP version used in less than PHP 9.0
*
* @param string $serialized
* @deprecated will be removed in 5.0
*/
public function unserialize($serialized)
{
Expand Down
24 changes: 24 additions & 0 deletions src/i18n/Messages/Symfony/FlushInvalidatedResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,35 @@ public function isFresh($timestamp)
return false;
}

public function __serialize(): array
{
return [];
}

public function __unserialize(array $data): void
{
// no-op
}

/**
* The __serialize() magic method will be automatically used instead of this
*
* @return string
* @deprecated will be removed in 5.0
*/
public function serialize()
{
return '';
}

/**
* The __unserialize() magic method will be automatically used instead of this almost all the time
* This method will be automatically used if existing serialized data was not saved as an associative array
* and the PHP version used in less than PHP 9.0
*
* @param string $serialized
* @deprecated will be removed in 5.0
*/
public function unserialize($serialized)
{
// no-op
Expand Down
2 changes: 1 addition & 1 deletion src/includes/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
$base = Environment::getEnv('SS_BASE_URL');
if ($base) {
// Strip relative path from SS_BASE_URL
return rtrim(parse_url($base, PHP_URL_PATH), '/');
return rtrim(parse_url($base, PHP_URL_PATH) ?: '', '/');
}

// Unless specified, use empty string for base in CLI
Expand Down

0 comments on commit 9093da8

Please sign in to comment.