Upgrade framework

This commit is contained in:
2023-11-14 16:54:35 +01:00
parent 1648a5cd42
commit 4fcf6fffcc
10548 changed files with 693138 additions and 466698 deletions

View File

@@ -1,25 +1,27 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of the GlobalState package.
* This file is part of sebastian/global-state.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\GlobalState;
use const PHP_EOL;
use function is_array;
use function is_scalar;
use function serialize;
use function sprintf;
use function var_export;
/**
* Exports parts of a Snapshot as PHP code.
*/
class CodeExporter
final class CodeExporter
{
/**
* @param Snapshot $snapshot
* @return string
*/
public function constants(Snapshot $snapshot)
public function constants(Snapshot $snapshot): string
{
$result = '';
@@ -35,11 +37,33 @@ class CodeExporter
return $result;
}
/**
* @param Snapshot $snapshot
* @return string
*/
public function iniSettings(Snapshot $snapshot)
public function globalVariables(Snapshot $snapshot): string
{
$result = <<<'EOT'
call_user_func(
function ()
{
foreach (array_keys($GLOBALS) as $key) {
unset($GLOBALS[$key]);
}
}
);
EOT;
foreach ($snapshot->globalVariables() as $name => $value) {
$result .= sprintf(
'$GLOBALS[%s] = %s;' . PHP_EOL,
$this->exportVariable($name),
$this->exportVariable($value)
);
}
return $result;
}
public function iniSettings(Snapshot $snapshot): string
{
$result = '';
@@ -54,13 +78,9 @@ class CodeExporter
return $result;
}
/**
* @param mixed $variable
* @return string
*/
private function exportVariable($variable)
private function exportVariable($variable): string
{
if (is_scalar($variable) || is_null($variable) ||
if (is_scalar($variable) || null === $variable ||
(is_array($variable) && $this->arrayOnlyContainsScalars($variable))) {
return var_export($variable, true);
}
@@ -68,18 +88,14 @@ class CodeExporter
return 'unserialize(' . var_export(serialize($variable), true) . ')';
}
/**
* @param array $array
* @return bool
*/
private function arrayOnlyContainsScalars(array $array)
private function arrayOnlyContainsScalars(array $array): bool
{
$result = true;
foreach ($array as $element) {
if (is_array($element)) {
$result = self::arrayOnlyContainsScalars($element);
} elseif (!is_scalar($element) && !is_null($element)) {
$result = $this->arrayOnlyContainsScalars($element);
} elseif (!is_scalar($element) && null !== $element) {
$result = false;
}

View File

@@ -1,122 +1,92 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of the GlobalState package.
* This file is part of sebastian/global-state.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\GlobalState;
use function in_array;
use function strpos;
use ReflectionClass;
/**
* A blacklist for global state elements that should not be snapshotted.
*/
class Blacklist
final class ExcludeList
{
/**
* @var array
*/
private $globalVariables = array();
private $globalVariables = [];
/**
* @var string[]
*/
private $classes = [];
/**
* @var string[]
*/
private $classNamePrefixes = [];
/**
* @var string[]
*/
private $parentClasses = [];
/**
* @var string[]
*/
private $interfaces = [];
/**
* @var array
*/
private $classes = array();
private $staticAttributes = [];
/**
* @var array
*/
private $classNamePrefixes = array();
/**
* @var array
*/
private $parentClasses = array();
/**
* @var array
*/
private $interfaces = array();
/**
* @var array
*/
private $staticAttributes = array();
/**
* @param string $variableName
*/
public function addGlobalVariable($variableName)
public function addGlobalVariable(string $variableName): void
{
$this->globalVariables[$variableName] = true;
}
/**
* @param string $className
*/
public function addClass($className)
public function addClass(string $className): void
{
$this->classes[] = $className;
}
/**
* @param string $className
*/
public function addSubclassesOf($className)
public function addSubclassesOf(string $className): void
{
$this->parentClasses[] = $className;
}
/**
* @param string $interfaceName
*/
public function addImplementorsOf($interfaceName)
public function addImplementorsOf(string $interfaceName): void
{
$this->interfaces[] = $interfaceName;
}
/**
* @param string $classNamePrefix
*/
public function addClassNamePrefix($classNamePrefix)
public function addClassNamePrefix(string $classNamePrefix): void
{
$this->classNamePrefixes[] = $classNamePrefix;
}
/**
* @param string $className
* @param string $attributeName
*/
public function addStaticAttribute($className, $attributeName)
public function addStaticAttribute(string $className, string $attributeName): void
{
if (!isset($this->staticAttributes[$className])) {
$this->staticAttributes[$className] = array();
$this->staticAttributes[$className] = [];
}
$this->staticAttributes[$className][$attributeName] = true;
}
/**
* @param string $variableName
* @return bool
*/
public function isGlobalVariableBlacklisted($variableName)
public function isGlobalVariableExcluded(string $variableName): bool
{
return isset($this->globalVariables[$variableName]);
}
/**
* @param string $className
* @param string $attributeName
* @return bool
*/
public function isStaticAttributeBlacklisted($className, $attributeName)
public function isStaticAttributeExcluded(string $className, string $attributeName): bool
{
if (in_array($className, $this->classes)) {
if (in_array($className, $this->classes, true)) {
return true;
}

View File

@@ -1,15 +1,23 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of the GlobalState package.
* This file is part of sebastian/global-state.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\GlobalState;
use function array_diff;
use function array_key_exists;
use function array_keys;
use function array_merge;
use function function_exists;
use function get_defined_functions;
use function in_array;
use function is_array;
use ReflectionClass;
use ReflectionProperty;
/**
@@ -20,11 +28,11 @@ class Restorer
/**
* Deletes function definitions that are not defined in a snapshot.
*
* @param Snapshot $snapshot
* @throws RuntimeException when the uopz_delete() function is not available
* @see https://github.com/krakjoe/uopz
*
* @see https://github.com/krakjoe/uopz
*/
public function restoreFunctions(Snapshot $snapshot)
public function restoreFunctions(Snapshot $snapshot): void
{
if (!function_exists('uopz_delete')) {
throw new RuntimeException('The uopz_delete() function is required for this operation');
@@ -39,10 +47,8 @@ class Restorer
/**
* Restores all global and super-global variables from a snapshot.
*
* @param Snapshot $snapshot
*/
public function restoreGlobalVariables(Snapshot $snapshot)
public function restoreGlobalVariables(Snapshot $snapshot): void
{
$superGlobalArrays = $snapshot->superGlobalArrays();
@@ -53,10 +59,10 @@ class Restorer
$globalVariables = $snapshot->globalVariables();
foreach (array_keys($GLOBALS) as $key) {
if ($key != 'GLOBALS' &&
!in_array($key, $superGlobalArrays) &&
!$snapshot->blacklist()->isGlobalVariableBlacklisted($key)) {
if (isset($globalVariables[$key])) {
if ($key !== 'GLOBALS' &&
!in_array($key, $superGlobalArrays, true) &&
!$snapshot->excludeList()->isGlobalVariableExcluded($key)) {
if (array_key_exists($key, $globalVariables)) {
$GLOBALS[$key] = $globalVariables[$key];
} else {
unset($GLOBALS[$key]);
@@ -67,13 +73,12 @@ class Restorer
/**
* Restores all static attributes in user-defined classes from this snapshot.
*
* @param Snapshot $snapshot
*/
public function restoreStaticAttributes(Snapshot $snapshot)
public function restoreStaticAttributes(Snapshot $snapshot): void
{
$current = new Snapshot($snapshot->blacklist(), false, false, false, false, true, false, false, false, false);
$current = new Snapshot($snapshot->excludeList(), false, false, false, false, true, false, false, false, false);
$newClasses = array_diff($current->classes(), $snapshot->classes());
unset($current);
foreach ($snapshot->staticAttributes() as $className => $staticAttributes) {
@@ -85,7 +90,7 @@ class Restorer
}
foreach ($newClasses as $className) {
$class = new \ReflectionClass($className);
$class = new ReflectionClass($className);
$defaults = $class->getDefaultProperties();
foreach ($class->getProperties() as $attribute) {
@@ -95,7 +100,7 @@ class Restorer
$name = $attribute->getName();
if ($snapshot->blacklist()->isStaticAttributeBlacklisted($className, $name)) {
if ($snapshot->excludeList()->isStaticAttributeExcluded($className, $name)) {
continue;
}
@@ -111,11 +116,8 @@ class Restorer
/**
* Restores a super-global variable array from this snapshot.
*
* @param Snapshot $snapshot
* @param $superGlobalArray
*/
private function restoreSuperGlobalArray(Snapshot $snapshot, $superGlobalArray)
private function restoreSuperGlobalArray(Snapshot $snapshot, string $superGlobalArray): void
{
$superGlobalVariables = $snapshot->superGlobalVariables();

View File

@@ -1,17 +1,37 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of the GlobalState package.
* This file is part of sebastian/global-state.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\GlobalState;
use const PHP_VERSION_ID;
use function array_keys;
use function array_merge;
use function array_reverse;
use function func_get_args;
use function get_declared_classes;
use function get_declared_interfaces;
use function get_declared_traits;
use function get_defined_constants;
use function get_defined_functions;
use function get_included_files;
use function in_array;
use function ini_get_all;
use function is_array;
use function is_object;
use function is_resource;
use function is_scalar;
use function serialize;
use function unserialize;
use ReflectionClass;
use Serializable;
use SebastianBergmann\ObjectReflector\ObjectReflector;
use SebastianBergmann\RecursionContext\Context;
use Throwable;
/**
* A snapshot of global state.
@@ -19,86 +39,71 @@ use Serializable;
class Snapshot
{
/**
* @var Blacklist
* @var ExcludeList
*/
private $blacklist;
private $excludeList;
/**
* @var array
*/
private $globalVariables = array();
private $globalVariables = [];
/**
* @var array
*/
private $superGlobalArrays = array();
private $superGlobalArrays = [];
/**
* @var array
*/
private $superGlobalVariables = array();
private $superGlobalVariables = [];
/**
* @var array
*/
private $staticAttributes = array();
private $staticAttributes = [];
/**
* @var array
*/
private $iniSettings = array();
private $iniSettings = [];
/**
* @var array
*/
private $includedFiles = array();
private $includedFiles = [];
/**
* @var array
*/
private $constants = array();
private $constants = [];
/**
* @var array
*/
private $functions = array();
private $functions = [];
/**
* @var array
*/
private $interfaces = array();
private $interfaces = [];
/**
* @var array
*/
private $classes = array();
private $classes = [];
/**
* @var array
*/
private $traits = array();
private $traits = [];
/**
* Creates a snapshot of the current global state.
*
* @param Blacklist $blacklist
* @param bool $includeGlobalVariables
* @param bool $includeStaticAttributes
* @param bool $includeConstants
* @param bool $includeFunctions
* @param bool $includeClasses
* @param bool $includeInterfaces
* @param bool $includeTraits
* @param bool $includeIniSettings
* @param bool $includeIncludedFiles
*/
public function __construct(Blacklist $blacklist = null, $includeGlobalVariables = true, $includeStaticAttributes = true, $includeConstants = true, $includeFunctions = true, $includeClasses = true, $includeInterfaces = true, $includeTraits = true, $includeIniSettings = true, $includeIncludedFiles = true)
public function __construct(ExcludeList $excludeList = null, bool $includeGlobalVariables = true, bool $includeStaticAttributes = true, bool $includeConstants = true, bool $includeFunctions = true, bool $includeClasses = true, bool $includeInterfaces = true, bool $includeTraits = true, bool $includeIniSettings = true, bool $includeIncludedFiles = true)
{
if ($blacklist === null) {
$blacklist = new Blacklist;
}
$this->blacklist = $blacklist;
$this->excludeList = $excludeList ?: new ExcludeList;
if ($includeConstants) {
$this->snapshotConstants();
@@ -133,105 +138,67 @@ class Snapshot
$this->includedFiles = get_included_files();
}
if (function_exists('get_declared_traits')) {
if ($includeTraits) {
$this->traits = get_declared_traits();
}
}
/**
* @return Blacklist
*/
public function blacklist()
public function excludeList(): ExcludeList
{
return $this->blacklist;
return $this->excludeList;
}
/**
* @return array
*/
public function globalVariables()
public function globalVariables(): array
{
return $this->globalVariables;
}
/**
* @return array
*/
public function superGlobalVariables()
public function superGlobalVariables(): array
{
return $this->superGlobalVariables;
}
/**
* Returns a list of all super-global variable arrays.
*
* @return array
*/
public function superGlobalArrays()
public function superGlobalArrays(): array
{
return $this->superGlobalArrays;
}
/**
* @return array
*/
public function staticAttributes()
public function staticAttributes(): array
{
return $this->staticAttributes;
}
/**
* @return array
*/
public function iniSettings()
public function iniSettings(): array
{
return $this->iniSettings;
}
/**
* @return array
*/
public function includedFiles()
public function includedFiles(): array
{
return $this->includedFiles;
}
/**
* @return array
*/
public function constants()
public function constants(): array
{
return $this->constants;
}
/**
* @return array
*/
public function functions()
public function functions(): array
{
return $this->functions;
}
/**
* @return array
*/
public function interfaces()
public function interfaces(): array
{
return $this->interfaces;
}
/**
* @return array
*/
public function classes()
public function classes(): array
{
return $this->classes;
}
/**
* @return array
*/
public function traits()
public function traits(): array
{
return $this->traits;
}
@@ -239,7 +206,7 @@ class Snapshot
/**
* Creates a snapshot user-defined constants.
*/
private function snapshotConstants()
private function snapshotConstants(): void
{
$constants = get_defined_constants(true);
@@ -251,7 +218,7 @@ class Snapshot
/**
* Creates a snapshot user-defined functions.
*/
private function snapshotFunctions()
private function snapshotFunctions(): void
{
$functions = get_defined_functions();
@@ -261,7 +228,7 @@ class Snapshot
/**
* Creates a snapshot user-defined classes.
*/
private function snapshotClasses()
private function snapshotClasses(): void
{
foreach (array_reverse(get_declared_classes()) as $className) {
$class = new ReflectionClass($className);
@@ -279,7 +246,7 @@ class Snapshot
/**
* Creates a snapshot user-defined interfaces.
*/
private function snapshotInterfaces()
private function snapshotInterfaces(): void
{
foreach (array_reverse(get_declared_interfaces()) as $interfaceName) {
$class = new ReflectionClass($interfaceName);
@@ -297,7 +264,7 @@ class Snapshot
/**
* Creates a snapshot of all global and super-global variables.
*/
private function snapshotGlobals()
private function snapshotGlobals(): void
{
$superGlobalArrays = $this->superGlobalArrays();
@@ -306,10 +273,11 @@ class Snapshot
}
foreach (array_keys($GLOBALS) as $key) {
if ($key != 'GLOBALS' &&
!in_array($key, $superGlobalArrays) &&
if ($key !== 'GLOBALS' &&
!in_array($key, $superGlobalArrays, true) &&
$this->canBeSerialized($GLOBALS[$key]) &&
!$this->blacklist->isGlobalVariableBlacklisted($key)) {
!$this->excludeList->isGlobalVariableExcluded($key)) {
/* @noinspection UnserializeExploitsInspection */
$this->globalVariables[$key] = unserialize(serialize($GLOBALS[$key]));
}
}
@@ -317,15 +285,14 @@ class Snapshot
/**
* Creates a snapshot a super-global variable array.
*
* @param $superGlobalArray
*/
private function snapshotSuperGlobalArray($superGlobalArray)
private function snapshotSuperGlobalArray(string $superGlobalArray): void
{
$this->superGlobalVariables[$superGlobalArray] = array();
$this->superGlobalVariables[$superGlobalArray] = [];
if (isset($GLOBALS[$superGlobalArray]) && is_array($GLOBALS[$superGlobalArray])) {
foreach ($GLOBALS[$superGlobalArray] as $key => $value) {
/* @noinspection UnserializeExploitsInspection */
$this->superGlobalVariables[$superGlobalArray][$key] = unserialize(serialize($value));
}
}
@@ -334,24 +301,30 @@ class Snapshot
/**
* Creates a snapshot of all static attributes in user-defined classes.
*/
private function snapshotStaticAttributes()
private function snapshotStaticAttributes(): void
{
foreach ($this->classes as $className) {
$class = new ReflectionClass($className);
$snapshot = array();
$snapshot = [];
foreach ($class->getProperties() as $attribute) {
if ($attribute->isStatic()) {
$name = $attribute->getName();
if ($this->blacklist->isStaticAttributeBlacklisted($className, $name)) {
if ($this->excludeList->isStaticAttributeExcluded($className, $name)) {
continue;
}
$attribute->setAccessible(true);
if (PHP_VERSION_ID >= 70400 && !$attribute->isInitialized()) {
continue;
}
$value = $attribute->getValue();
if ($this->canBeSerialized($value)) {
/* @noinspection UnserializeExploitsInspection */
$snapshot[$name] = unserialize(serialize($value));
}
}
@@ -365,59 +338,106 @@ class Snapshot
/**
* Returns a list of all super-global variable arrays.
*
* @return array
*/
private function setupSuperGlobalArrays()
private function setupSuperGlobalArrays(): void
{
$this->superGlobalArrays = array(
$this->superGlobalArrays = [
'_ENV',
'_POST',
'_GET',
'_COOKIE',
'_SERVER',
'_FILES',
'_REQUEST'
);
if (ini_get('register_long_arrays') == '1') {
$this->superGlobalArrays = array_merge(
$this->superGlobalArrays,
array(
'HTTP_ENV_VARS',
'HTTP_POST_VARS',
'HTTP_GET_VARS',
'HTTP_COOKIE_VARS',
'HTTP_SERVER_VARS',
'HTTP_POST_FILES'
)
);
}
'_REQUEST',
];
}
/**
* @param mixed $variable
* @return bool
* @todo Implement this properly
*/
private function canBeSerialized($variable)
private function canBeSerialized($variable): bool
{
if (!is_object($variable)) {
return !is_resource($variable);
}
if ($variable instanceof \stdClass) {
if (is_scalar($variable) || $variable === null) {
return true;
}
$class = new ReflectionClass($variable);
if (is_resource($variable)) {
return false;
}
do {
if ($class->isInternal()) {
return $variable instanceof Serializable;
foreach ($this->enumerateObjectsAndResources($variable) as $value) {
if (is_resource($value)) {
return false;
}
} while ($class = $class->getParentClass());
if (is_object($value)) {
$class = new ReflectionClass($value);
if ($class->isAnonymous()) {
return false;
}
try {
@serialize($value);
} catch (Throwable $t) {
return false;
}
}
}
return true;
}
private function enumerateObjectsAndResources($variable): array
{
if (isset(func_get_args()[1])) {
$processed = func_get_args()[1];
} else {
$processed = new Context;
}
$result = [];
if ($processed->contains($variable)) {
return $result;
}
$array = $variable;
$processed->add($variable);
if (is_array($variable)) {
foreach ($array as $element) {
if (!is_array($element) && !is_object($element) && !is_resource($element)) {
continue;
}
if (!is_resource($element)) {
/** @noinspection SlowArrayOperationsInLoopInspection */
$result = array_merge(
$result,
$this->enumerateObjectsAndResources($element, $processed)
);
} else {
$result[] = $element;
}
}
} else {
$result[] = $variable;
foreach ((new ObjectReflector)->getAttributes($variable) as $value) {
if (!is_array($value) && !is_object($value) && !is_resource($value)) {
continue;
}
if (!is_resource($value)) {
/** @noinspection SlowArrayOperationsInLoopInspection */
$result = array_merge(
$result,
$this->enumerateObjectsAndResources($value, $processed)
);
} else {
$result[] = $value;
}
}
}
return $result;
}
}

View File

@@ -1,17 +1,16 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of the GlobalState package.
* This file is part of sebastian/global-state.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\GlobalState;
/**
*/
interface Exception
use Throwable;
interface Exception extends Throwable
{
}

View File

@@ -1,17 +1,14 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of the GlobalState package.
* This file is part of sebastian/global-state.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\GlobalState;
/**
*/
class RuntimeException extends \RuntimeException implements Exception
final class RuntimeException extends \RuntimeException implements Exception
{
}