├── src ├── exceptions │ ├── Exception.php │ └── RuntimeException.php ├── CodeExporter.php ├── ExcludeList.php ├── Restorer.php └── Snapshot.php ├── README.md ├── composer.json ├── LICENSE ├── SECURITY.md └── ChangeLog.md /src/exceptions/Exception.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | namespace SebastianBergmann\GlobalState; 11 | 12 | use Throwable; 13 | 14 | interface Exception extends Throwable 15 | { 16 | } 17 | -------------------------------------------------------------------------------- /src/exceptions/RuntimeException.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | namespace SebastianBergmann\GlobalState; 11 | 12 | final class RuntimeException extends \RuntimeException implements Exception 13 | { 14 | } 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Latest Stable Version](https://poser.pugx.org/sebastian/global-state/v)](https://packagist.org/packages/sebastian/global-state) 2 | [![CI Status](https://github.com/sebastianbergmann/global-state/workflows/CI/badge.svg)](https://github.com/sebastianbergmann/global-state/actions) 3 | [![codecov](https://codecov.io/gh/sebastianbergmann/global-state/branch/main/graph/badge.svg)](https://codecov.io/gh/sebastianbergmann/global-state) 4 | 5 | # sebastian/global-state 6 | 7 | Snapshotting of global state, factored out of PHPUnit into a stand-alone component. 8 | 9 | ## Installation 10 | 11 | You can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/): 12 | 13 | ``` 14 | composer require sebastian/global-state 15 | ``` 16 | 17 | If you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency: 18 | 19 | ``` 20 | composer require --dev sebastian/global-state 21 | ``` 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sebastian/global-state", 3 | "description": "Snapshotting of global state", 4 | "keywords": ["global state"], 5 | "homepage": "https://www.github.com/sebastianbergmann/global-state", 6 | "license": "BSD-3-Clause", 7 | "authors": [ 8 | { 9 | "name": "Sebastian Bergmann", 10 | "email": "sebastian@phpunit.de" 11 | } 12 | ], 13 | "support": { 14 | "issues": "https://github.com/sebastianbergmann/global-state/issues", 15 | "security": "https://github.com/sebastianbergmann/global-state/security/policy" 16 | }, 17 | "prefer-stable": true, 18 | "config": { 19 | "platform": { 20 | "php": "8.3.0" 21 | }, 22 | "optimize-autoloader": true, 23 | "sort-packages": true 24 | }, 25 | "require": { 26 | "php": ">=8.3", 27 | "sebastian/object-reflector": "^5.0", 28 | "sebastian/recursion-context": "^7.0" 29 | }, 30 | "require-dev": { 31 | "ext-dom": "*", 32 | "phpunit/phpunit": "^12.0" 33 | }, 34 | "autoload": { 35 | "classmap": [ 36 | "src/" 37 | ] 38 | }, 39 | "autoload-dev": { 40 | "classmap": [ 41 | "tests/_fixture/" 42 | ], 43 | "files": [ 44 | "tests/_fixture/SnapshotFunctions.php" 45 | ] 46 | }, 47 | "extra": { 48 | "branch-alias": { 49 | "dev-main": "8.0-dev" 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2001-2025, Sebastian Bergmann 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | 3. Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | If you believe you have found a security vulnerability in the library that is developed in this repository, please report it to us through coordinated disclosure. 4 | 5 | **Please do not report security vulnerabilities through public GitHub issues, discussions, or pull requests.** 6 | 7 | Instead, please email `sebastian@phpunit.de`. 8 | 9 | Please include as much of the information listed below as you can to help us better understand and resolve the issue: 10 | 11 | * The type of issue 12 | * Full paths of source file(s) related to the manifestation of the issue 13 | * The location of the affected source code (tag/branch/commit or direct URL) 14 | * Any special configuration required to reproduce the issue 15 | * Step-by-step instructions to reproduce the issue 16 | * Proof-of-concept or exploit code (if possible) 17 | * Impact of the issue, including how an attacker might exploit the issue 18 | 19 | This information will help us triage your report more quickly. 20 | 21 | ## Web Context 22 | 23 | The library that is developed in this repository was either extracted from [PHPUnit](https://github.com/sebastianbergmann/phpunit) or developed specifically as a dependency for PHPUnit. 24 | 25 | The library is developed with a focus on development environments and the command-line. No specific testing or hardening with regard to using the library in an HTTP or web context or with untrusted input data is performed. The library might also contain functionality that intentionally exposes internal application data for debugging purposes. 26 | 27 | If the library is used in a web application, the application developer is responsible for filtering inputs or escaping outputs as necessary and for verifying that the used functionality is safe for use within the intended context. 28 | 29 | Vulnerabilities specific to the use outside a development context will be fixed as applicable, provided that the fix does not have an averse effect on the primary use case for development purposes. 30 | 31 | -------------------------------------------------------------------------------- /src/CodeExporter.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | namespace SebastianBergmann\GlobalState; 11 | 12 | use const PHP_EOL; 13 | use function is_array; 14 | use function is_scalar; 15 | use function serialize; 16 | use function sprintf; 17 | use function var_export; 18 | 19 | final class CodeExporter 20 | { 21 | public function constants(Snapshot $snapshot): string 22 | { 23 | $result = ''; 24 | 25 | foreach ($snapshot->constants() as $name => $value) { 26 | $result .= sprintf( 27 | 'if (!defined(\'%s\')) define(\'%s\', %s);' . "\n", 28 | $name, 29 | $name, 30 | $this->exportVariable($value), 31 | ); 32 | } 33 | 34 | return $result; 35 | } 36 | 37 | public function globalVariables(Snapshot $snapshot): string 38 | { 39 | $result = <<<'EOT' 40 | call_user_func( 41 | function () 42 | { 43 | foreach (array_keys($GLOBALS) as $key) { 44 | unset($GLOBALS[$key]); 45 | } 46 | } 47 | ); 48 | 49 | 50 | EOT; 51 | 52 | foreach ($snapshot->globalVariables() as $name => $value) { 53 | $result .= sprintf( 54 | '$GLOBALS[%s] = %s;' . PHP_EOL, 55 | $this->exportVariable($name), 56 | $this->exportVariable($value), 57 | ); 58 | } 59 | 60 | return $result; 61 | } 62 | 63 | public function iniSettings(Snapshot $snapshot): string 64 | { 65 | $result = ''; 66 | 67 | foreach ($snapshot->iniSettings() as $key => $value) { 68 | $result .= sprintf( 69 | '@ini_set(%s, %s);' . "\n", 70 | $this->exportVariable($key), 71 | $this->exportVariable($value), 72 | ); 73 | } 74 | 75 | return $result; 76 | } 77 | 78 | private function exportVariable(mixed $variable): string 79 | { 80 | if (is_scalar($variable) || null === $variable || 81 | (is_array($variable) && $this->arrayOnlyContainsScalars($variable))) { 82 | return var_export($variable, true); 83 | } 84 | 85 | return 'unserialize(' . var_export(serialize($variable), true) . ')'; 86 | } 87 | 88 | /** 89 | * @param array $array 90 | */ 91 | private function arrayOnlyContainsScalars(array $array): bool 92 | { 93 | $result = true; 94 | 95 | foreach ($array as $element) { 96 | if (is_array($element)) { 97 | $result = $this->arrayOnlyContainsScalars($element); 98 | } elseif (!is_scalar($element) && null !== $element) { 99 | $result = false; 100 | } 101 | 102 | if ($result === false) { 103 | break; 104 | } 105 | } 106 | 107 | return $result; 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /src/ExcludeList.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | namespace SebastianBergmann\GlobalState; 11 | 12 | use function in_array; 13 | use function str_starts_with; 14 | use ReflectionClass; 15 | 16 | final class ExcludeList 17 | { 18 | /** 19 | * @var array 20 | */ 21 | private array $globalVariables = []; 22 | 23 | /** 24 | * @var list 25 | */ 26 | private array $classes = []; 27 | 28 | /** 29 | * @var list 30 | */ 31 | private array $classNamePrefixes = []; 32 | 33 | /** 34 | * @var list 35 | */ 36 | private array $parentClasses = []; 37 | 38 | /** 39 | * @var list 40 | */ 41 | private array $interfaces = []; 42 | 43 | /** 44 | * @var array> 45 | */ 46 | private array $staticProperties = []; 47 | 48 | /** 49 | * @param non-empty-string $variableName 50 | */ 51 | public function addGlobalVariable(string $variableName): void 52 | { 53 | $this->globalVariables[$variableName] = true; 54 | } 55 | 56 | /** 57 | * @param non-empty-string $className 58 | */ 59 | public function addClass(string $className): void 60 | { 61 | $this->classes[] = $className; 62 | } 63 | 64 | /** 65 | * @param non-empty-string $className 66 | */ 67 | public function addSubclassesOf(string $className): void 68 | { 69 | $this->parentClasses[] = $className; 70 | } 71 | 72 | /** 73 | * @param non-empty-string $interfaceName 74 | */ 75 | public function addImplementorsOf(string $interfaceName): void 76 | { 77 | $this->interfaces[] = $interfaceName; 78 | } 79 | 80 | /** 81 | * @param non-empty-string $classNamePrefix 82 | */ 83 | public function addClassNamePrefix(string $classNamePrefix): void 84 | { 85 | $this->classNamePrefixes[] = $classNamePrefix; 86 | } 87 | 88 | /** 89 | * @param non-empty-string $className 90 | * @param non-empty-string $propertyName 91 | */ 92 | public function addStaticProperty(string $className, string $propertyName): void 93 | { 94 | if (!isset($this->staticProperties[$className])) { 95 | $this->staticProperties[$className] = []; 96 | } 97 | 98 | $this->staticProperties[$className][$propertyName] = true; 99 | } 100 | 101 | public function isGlobalVariableExcluded(string $variableName): bool 102 | { 103 | return isset($this->globalVariables[$variableName]); 104 | } 105 | 106 | /** 107 | * @param class-string $className 108 | * @param non-empty-string $propertyName 109 | */ 110 | public function isStaticPropertyExcluded(string $className, string $propertyName): bool 111 | { 112 | if (in_array($className, $this->classes, true)) { 113 | return true; 114 | } 115 | 116 | foreach ($this->classNamePrefixes as $prefix) { 117 | if (str_starts_with($className, $prefix)) { 118 | return true; 119 | } 120 | } 121 | 122 | $class = new ReflectionClass($className); 123 | 124 | foreach ($this->parentClasses as $type) { 125 | if ($class->isSubclassOf($type)) { 126 | return true; 127 | } 128 | } 129 | 130 | foreach ($this->interfaces as $type) { 131 | if ($class->implementsInterface($type)) { 132 | return true; 133 | } 134 | } 135 | 136 | return isset($this->staticProperties[$className][$propertyName]); 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /src/Restorer.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | namespace SebastianBergmann\GlobalState; 11 | 12 | use function array_diff; 13 | use function array_key_exists; 14 | use function array_keys; 15 | use function array_merge; 16 | use function assert; 17 | use function in_array; 18 | use function is_array; 19 | use ReflectionClass; 20 | use ReflectionProperty; 21 | 22 | final class Restorer 23 | { 24 | public function restoreGlobalVariables(Snapshot $snapshot): void 25 | { 26 | $superGlobalArrays = $snapshot->superGlobalArrays(); 27 | 28 | foreach ($superGlobalArrays as $superGlobalArray) { 29 | $this->restoreSuperGlobalArray($snapshot, $superGlobalArray); 30 | } 31 | 32 | $globalVariables = $snapshot->globalVariables(); 33 | 34 | foreach (array_keys($GLOBALS) as $key) { 35 | if ($key !== 'GLOBALS' && 36 | !in_array($key, $superGlobalArrays, true) && 37 | !$snapshot->excludeList()->isGlobalVariableExcluded((string) $key)) { 38 | if (array_key_exists($key, $globalVariables)) { 39 | $GLOBALS[$key] = $globalVariables[$key]; 40 | } else { 41 | unset($GLOBALS[$key]); 42 | } 43 | } 44 | } 45 | } 46 | 47 | public function restoreStaticProperties(Snapshot $snapshot): void 48 | { 49 | $current = new Snapshot($snapshot->excludeList(), false, false, false, false, true, false, false, false, false); 50 | $newClasses = array_diff($current->classes(), $snapshot->classes()); 51 | 52 | unset($current); 53 | 54 | foreach ($snapshot->staticProperties() as $className => $staticProperties) { 55 | foreach ($staticProperties as $name => $value) { 56 | $reflector = new ReflectionProperty($className, $name); 57 | $reflector->setValue(null, $value); 58 | } 59 | } 60 | 61 | foreach ($newClasses as $className) { 62 | $class = new ReflectionClass($className); 63 | $defaults = $class->getDefaultProperties(); 64 | 65 | foreach ($class->getProperties() as $property) { 66 | if (!$property->isStatic()) { 67 | continue; 68 | } 69 | 70 | $name = $property->getName(); 71 | 72 | if ($snapshot->excludeList()->isStaticPropertyExcluded($className, $name)) { 73 | continue; 74 | } 75 | 76 | if (!isset($defaults[$name])) { 77 | continue; 78 | } 79 | 80 | $property->setValue(null, $defaults[$name]); 81 | } 82 | } 83 | } 84 | 85 | private function restoreSuperGlobalArray(Snapshot $snapshot, string $superGlobalArray): void 86 | { 87 | $superGlobalVariables = $snapshot->superGlobalVariables(); 88 | 89 | if (isset($GLOBALS[$superGlobalArray], $superGlobalVariables[$superGlobalArray]) && 90 | is_array($GLOBALS[$superGlobalArray])) { 91 | $keys = array_keys( 92 | array_merge( 93 | $GLOBALS[$superGlobalArray], 94 | $superGlobalVariables[$superGlobalArray], 95 | ), 96 | ); 97 | 98 | foreach ($keys as $key) { 99 | assert(isset($GLOBALS[$superGlobalArray]) && is_array($GLOBALS[$superGlobalArray])); 100 | 101 | if (array_key_exists($key, $superGlobalVariables[$superGlobalArray])) { 102 | $GLOBALS[$superGlobalArray][$key] = $superGlobalVariables[$superGlobalArray][$key]; 103 | } else { 104 | unset($GLOBALS[$superGlobalArray][$key]); 105 | } 106 | } 107 | } 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /ChangeLog.md: -------------------------------------------------------------------------------- 1 | # Changes in sebastian/global-state 2 | 3 | All notable changes in `sebastian/global-state` are documented in this file using the [Keep a CHANGELOG](https://keepachangelog.com/) principles. 4 | 5 | ## [8.0.2] - 2025-08-29 6 | 7 | ### Changed 8 | 9 | * [#39](https://github.com/sebastianbergmann/global-state/pull/39): Restore nullable variables in super-global arrays 10 | 11 | ## [8.0.1] - 2025-08-28 12 | 13 | ### Changed 14 | 15 | * [#38](https://github.com/sebastianbergmann/global-state/pull/38): Improve performance of `Snapshot::snapshotSuperGlobalArray()` 16 | 17 | ## [8.0.0] - 2025-02-07 18 | 19 | ### Removed 20 | 21 | * This component is no longer supported on PHP 8.2 22 | 23 | ## [7.0.2] - 2024-07-03 24 | 25 | ### Changed 26 | 27 | * This project now uses PHPStan instead of Psalm for static analysis 28 | 29 | ## [7.0.1] - 2024-03-02 30 | 31 | ### Changed 32 | 33 | * Do not use implicitly nullable parameters 34 | 35 | ## [7.0.0] - 2024-02-02 36 | 37 | ### Removed 38 | 39 | * This component is no longer supported on PHP 8.1 40 | 41 | ## [6.0.2] - 2024-03-02 42 | 43 | ### Changed 44 | 45 | * Do not use implicitly nullable parameters 46 | 47 | ## [6.0.1] - 2023-07-19 48 | 49 | ### Changed 50 | 51 | * Changed usage of `ReflectionProperty::setValue()` to be compatible with PHP 8.3 52 | 53 | ## [6.0.0] - 2023-02-03 54 | 55 | ### Changed 56 | 57 | * Renamed `SebastianBergmann\GlobalState\ExcludeList::addStaticAttribute()` to `SebastianBergmann\GlobalState\ExcludeList::addStaticProperty()` 58 | * Renamed `SebastianBergmann\GlobalState\ExcludeList::isStaticAttributeExcluded()` to `SebastianBergmann\GlobalState\ExcludeList::isStaticPropertyExcluded()` 59 | * Renamed `SebastianBergmann\GlobalState\Restorer::restoreStaticAttributes()` to `SebastianBergmann\GlobalState\Restorer::restoreStaticProperties()` 60 | * Renamed `SebastianBergmann\GlobalState\Snapshot::staticAttributes()` to `SebastianBergmann\GlobalState\Snapshot::staticProperties()` 61 | 62 | ### Removed 63 | 64 | * Removed `SebastianBergmann\GlobalState\Restorer::restoreFunctions()` 65 | * This component is no longer supported on PHP 7.3, PHP 7.4 and PHP 8.0 66 | 67 | ## [5.0.5] - 2022-02-14 68 | 69 | ### Fixed 70 | 71 | * [#34](https://github.com/sebastianbergmann/global-state/pull/34): Uninitialised typed static properties are not handled correctly 72 | 73 | ## [5.0.4] - 2022-02-10 74 | 75 | ### Fixed 76 | 77 | * The `$includeTraits` parameter of `SebastianBergmann\GlobalState\Snapshot::__construct()` is not respected 78 | 79 | ## [5.0.3] - 2021-06-11 80 | 81 | ### Changed 82 | 83 | * `SebastianBergmann\GlobalState\CodeExporter::globalVariables()` now generates code that is compatible with PHP 8.1 84 | 85 | ## [5.0.2] - 2020-10-26 86 | 87 | ### Fixed 88 | 89 | * `SebastianBergmann\GlobalState\Exception` now correctly extends `\Throwable` 90 | 91 | ## [5.0.1] - 2020-09-28 92 | 93 | ### Changed 94 | 95 | * Changed PHP version constraint in `composer.json` from `^7.3 || ^8.0` to `>=7.3` 96 | 97 | ## [5.0.0] - 2020-08-07 98 | 99 | ### Changed 100 | 101 | * The `SebastianBergmann\GlobalState\Blacklist` class has been renamed to `SebastianBergmann\GlobalState\ExcludeList` 102 | 103 | ## [4.0.0] - 2020-02-07 104 | 105 | ### Removed 106 | 107 | * This component is no longer supported on PHP 7.2 108 | 109 | ## [3.0.2] - 2022-02-10 110 | 111 | ### Fixed 112 | 113 | * The `$includeTraits` parameter of `SebastianBergmann\GlobalState\Snapshot::__construct()` is not respected 114 | 115 | ## [3.0.1] - 2020-11-30 116 | 117 | ### Changed 118 | 119 | * Changed PHP version constraint in `composer.json` from `^7.2` to `>=7.2` 120 | 121 | ## [3.0.0] - 2019-02-01 122 | 123 | ### Changed 124 | 125 | * `Snapshot::canBeSerialized()` now recursively checks arrays and object graphs for variables that cannot be serialized 126 | 127 | ### Removed 128 | 129 | * This component is no longer supported on PHP 7.0 and PHP 7.1 130 | 131 | [8.0.2]: https://github.com/sebastianbergmann/global-state/compare/8.0.1...8.0.2 132 | [8.0.1]: https://github.com/sebastianbergmann/global-state/compare/8.0.0...8.0.1 133 | [8.0.0]: https://github.com/sebastianbergmann/global-state/compare/7.0...8.0.0 134 | [7.0.2]: https://github.com/sebastianbergmann/global-state/compare/7.0.1...7.0.2 135 | [7.0.1]: https://github.com/sebastianbergmann/global-state/compare/7.0.0...7.0.1 136 | [7.0.0]: https://github.com/sebastianbergmann/global-state/compare/6.0...7.0.0 137 | [6.0.2]: https://github.com/sebastianbergmann/global-state/compare/6.0.1...6.0.2 138 | [6.0.1]: https://github.com/sebastianbergmann/global-state/compare/6.0.0...6.0.1 139 | [6.0.0]: https://github.com/sebastianbergmann/global-state/compare/5.0.5...6.0.0 140 | [5.0.5]: https://github.com/sebastianbergmann/global-state/compare/5.0.4...5.0.5 141 | [5.0.4]: https://github.com/sebastianbergmann/global-state/compare/5.0.3...5.0.4 142 | [5.0.3]: https://github.com/sebastianbergmann/global-state/compare/5.0.2...5.0.3 143 | [5.0.2]: https://github.com/sebastianbergmann/global-state/compare/5.0.1...5.0.2 144 | [5.0.1]: https://github.com/sebastianbergmann/global-state/compare/5.0.0...5.0.1 145 | [5.0.0]: https://github.com/sebastianbergmann/global-state/compare/4.0.0...5.0.0 146 | [4.0.0]: https://github.com/sebastianbergmann/global-state/compare/3.0.2...4.0.0 147 | [3.0.2]: https://github.com/sebastianbergmann/phpunit/compare/3.0.1...3.0.2 148 | [3.0.1]: https://github.com/sebastianbergmann/phpunit/compare/3.0.0...3.0.1 149 | [3.0.0]: https://github.com/sebastianbergmann/phpunit/compare/2.0.0...3.0.0 150 | 151 | -------------------------------------------------------------------------------- /src/Snapshot.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | namespace SebastianBergmann\GlobalState; 11 | 12 | use function array_keys; 13 | use function array_merge; 14 | use function array_reverse; 15 | use function assert; 16 | use function get_declared_classes; 17 | use function get_declared_interfaces; 18 | use function get_declared_traits; 19 | use function get_defined_constants; 20 | use function get_defined_functions; 21 | use function get_included_files; 22 | use function in_array; 23 | use function ini_get_all; 24 | use function is_array; 25 | use function is_object; 26 | use function is_resource; 27 | use function is_scalar; 28 | use function serialize; 29 | use function unserialize; 30 | use ReflectionClass; 31 | use SebastianBergmann\ObjectReflector\ObjectReflector; 32 | use SebastianBergmann\RecursionContext\Context; 33 | use Throwable; 34 | 35 | /** 36 | * A snapshot of global state. 37 | */ 38 | final class Snapshot 39 | { 40 | private ExcludeList $excludeList; 41 | 42 | /** 43 | * @var array 44 | */ 45 | private array $globalVariables = []; 46 | 47 | /** 48 | * @var list 49 | */ 50 | private array $superGlobalArrays = []; 51 | 52 | /** 53 | * @var array> 54 | */ 55 | private array $superGlobalVariables = []; 56 | 57 | /** 58 | * @var array> 59 | */ 60 | private array $staticProperties = []; 61 | 62 | /** 63 | * @var array 64 | */ 65 | private array $iniSettings = []; 66 | 67 | /** 68 | * @var list 69 | */ 70 | private array $includedFiles = []; 71 | 72 | /** 73 | * @var array 74 | */ 75 | private array $constants = []; 76 | 77 | /** 78 | * @var list 79 | */ 80 | private array $functions = []; 81 | 82 | /** 83 | * @var list 84 | */ 85 | private array $interfaces = []; 86 | 87 | /** 88 | * @var list 89 | */ 90 | private array $classes = []; 91 | 92 | /** 93 | * @var list 94 | */ 95 | private array $traits = []; 96 | 97 | public function __construct(?ExcludeList $excludeList = null, bool $includeGlobalVariables = true, bool $includeStaticProperties = true, bool $includeConstants = true, bool $includeFunctions = true, bool $includeClasses = true, bool $includeInterfaces = true, bool $includeTraits = true, bool $includeIniSettings = true, bool $includeIncludedFiles = true) 98 | { 99 | if ($excludeList === null) { 100 | $excludeList = new ExcludeList; 101 | } 102 | 103 | $this->excludeList = $excludeList; 104 | 105 | if ($includeConstants) { 106 | $this->snapshotConstants(); 107 | } 108 | 109 | if ($includeFunctions) { 110 | $this->snapshotFunctions(); 111 | } 112 | 113 | if ($includeClasses || $includeStaticProperties) { 114 | $this->snapshotClasses(); 115 | } 116 | 117 | if ($includeInterfaces) { 118 | $this->snapshotInterfaces(); 119 | } 120 | 121 | if ($includeGlobalVariables) { 122 | $this->setupSuperGlobalArrays(); 123 | $this->snapshotGlobals(); 124 | } 125 | 126 | if ($includeStaticProperties) { 127 | $this->snapshotStaticProperties(); 128 | } 129 | 130 | if ($includeIniSettings) { 131 | $iniSettings = ini_get_all(null, false); 132 | 133 | assert($iniSettings !== false); 134 | 135 | /* @phpstan-ignore assign.propertyType */ 136 | $this->iniSettings = $iniSettings; 137 | } 138 | 139 | if ($includeIncludedFiles) { 140 | $this->includedFiles = get_included_files(); 141 | } 142 | 143 | if ($includeTraits) { 144 | $this->traits = get_declared_traits(); 145 | } 146 | } 147 | 148 | public function excludeList(): ExcludeList 149 | { 150 | return $this->excludeList; 151 | } 152 | 153 | /** 154 | * @return array 155 | */ 156 | public function globalVariables(): array 157 | { 158 | return $this->globalVariables; 159 | } 160 | 161 | /** 162 | * @return array> 163 | */ 164 | public function superGlobalVariables(): array 165 | { 166 | return $this->superGlobalVariables; 167 | } 168 | 169 | /** 170 | * @return list 171 | */ 172 | public function superGlobalArrays(): array 173 | { 174 | return $this->superGlobalArrays; 175 | } 176 | 177 | /** 178 | * @return array> 179 | */ 180 | public function staticProperties(): array 181 | { 182 | return $this->staticProperties; 183 | } 184 | 185 | /** 186 | * @return array 187 | */ 188 | public function iniSettings(): array 189 | { 190 | return $this->iniSettings; 191 | } 192 | 193 | /** 194 | * @return list 195 | */ 196 | public function includedFiles(): array 197 | { 198 | return $this->includedFiles; 199 | } 200 | 201 | /** 202 | * @return array 203 | */ 204 | public function constants(): array 205 | { 206 | return $this->constants; 207 | } 208 | 209 | /** 210 | * @return list 211 | */ 212 | public function functions(): array 213 | { 214 | return $this->functions; 215 | } 216 | 217 | /** 218 | * @return list 219 | */ 220 | public function interfaces(): array 221 | { 222 | return $this->interfaces; 223 | } 224 | 225 | /** 226 | * @return list 227 | */ 228 | public function classes(): array 229 | { 230 | return $this->classes; 231 | } 232 | 233 | /** 234 | * @return list 235 | */ 236 | public function traits(): array 237 | { 238 | return $this->traits; 239 | } 240 | 241 | private function snapshotConstants(): void 242 | { 243 | $constants = get_defined_constants(true); 244 | 245 | if (isset($constants['user'])) { 246 | $this->constants = $constants['user']; 247 | } 248 | } 249 | 250 | private function snapshotFunctions(): void 251 | { 252 | $functions = get_defined_functions(); 253 | 254 | $this->functions = $functions['user']; 255 | } 256 | 257 | private function snapshotClasses(): void 258 | { 259 | foreach (array_reverse(get_declared_classes()) as $className) { 260 | $class = new ReflectionClass($className); 261 | 262 | if (!$class->isUserDefined()) { 263 | break; 264 | } 265 | 266 | $this->classes[] = $className; 267 | } 268 | 269 | $this->classes = array_reverse($this->classes); 270 | } 271 | 272 | private function snapshotInterfaces(): void 273 | { 274 | foreach (array_reverse(get_declared_interfaces()) as $interfaceName) { 275 | $class = new ReflectionClass($interfaceName); 276 | 277 | if (!$class->isUserDefined()) { 278 | break; 279 | } 280 | 281 | $this->interfaces[] = $interfaceName; 282 | } 283 | 284 | $this->interfaces = array_reverse($this->interfaces); 285 | } 286 | 287 | private function snapshotGlobals(): void 288 | { 289 | $superGlobalArrays = $this->superGlobalArrays(); 290 | 291 | foreach ($superGlobalArrays as $superGlobalArray) { 292 | $this->snapshotSuperGlobalArray($superGlobalArray); 293 | } 294 | 295 | foreach (array_keys($GLOBALS) as $key) { 296 | if ($key !== 'GLOBALS' && 297 | !in_array($key, $superGlobalArrays, true) && 298 | !$this->excludeList->isGlobalVariableExcluded($key) && 299 | $this->canBeSerialized($GLOBALS[$key]) 300 | ) { 301 | /* @phpstan-ignore assign.propertyType */ 302 | $this->globalVariables[$key] = $this->copyWithSerialize($GLOBALS[$key]); 303 | } 304 | } 305 | } 306 | 307 | private function snapshotSuperGlobalArray(string $superGlobalArray): void 308 | { 309 | $this->superGlobalVariables[$superGlobalArray] = []; 310 | 311 | if (isset($GLOBALS[$superGlobalArray]) && is_array($GLOBALS[$superGlobalArray])) { 312 | foreach ($GLOBALS[$superGlobalArray] as $key => $value) { 313 | /* @phpstan-ignore assign.propertyType */ 314 | $this->superGlobalVariables[$superGlobalArray][$key] = $this->copyWithSerialize($value); 315 | } 316 | } 317 | } 318 | 319 | private function snapshotStaticProperties(): void 320 | { 321 | foreach ($this->classes as $className) { 322 | $class = new ReflectionClass($className); 323 | $snapshot = []; 324 | 325 | foreach ($class->getProperties() as $property) { 326 | if ($property->isStatic()) { 327 | $name = $property->getName(); 328 | 329 | if ($this->excludeList->isStaticPropertyExcluded($className, $name)) { 330 | continue; 331 | } 332 | 333 | if (!$property->isInitialized()) { 334 | continue; 335 | } 336 | 337 | $value = $property->getValue(); 338 | 339 | if ($this->canBeSerialized($value)) { 340 | $snapshot[$name] = $this->copyWithSerialize($value); 341 | } 342 | } 343 | } 344 | 345 | if ($snapshot !== []) { 346 | $this->staticProperties[$className] = $snapshot; 347 | } 348 | } 349 | } 350 | 351 | private function setupSuperGlobalArrays(): void 352 | { 353 | $this->superGlobalArrays = [ 354 | '_ENV', 355 | '_POST', 356 | '_GET', 357 | '_COOKIE', 358 | '_SERVER', 359 | '_FILES', 360 | '_REQUEST', 361 | ]; 362 | } 363 | 364 | private function copyWithSerialize(mixed $variable): mixed 365 | { 366 | if (is_scalar($variable) || $variable === null) { 367 | return $variable; 368 | } 369 | 370 | /* @noinspection UnserializeExploitsInspection */ 371 | return unserialize(serialize($variable)); 372 | } 373 | 374 | private function canBeSerialized(mixed $variable): bool 375 | { 376 | if (is_scalar($variable) || $variable === null) { 377 | return true; 378 | } 379 | 380 | if (is_resource($variable)) { 381 | return false; 382 | } 383 | 384 | foreach ($this->enumerateObjectsAndResources($variable) as $value) { 385 | if (is_resource($value)) { 386 | return false; 387 | } 388 | 389 | if (is_object($value)) { 390 | $class = new ReflectionClass($value); 391 | 392 | if ($class->isAnonymous()) { 393 | return false; 394 | } 395 | 396 | try { 397 | @serialize($value); 398 | } catch (Throwable $t) { 399 | return false; 400 | } 401 | } 402 | } 403 | 404 | return true; 405 | } 406 | 407 | /** 408 | * @return array 409 | */ 410 | private function enumerateObjectsAndResources(mixed $variable, Context $processed = new Context): array 411 | { 412 | $result = []; 413 | 414 | /* @phpstan-ignore argument.type */ 415 | if ($processed->contains($variable) !== false) { 416 | return $result; 417 | } 418 | 419 | $array = $variable; 420 | 421 | /* @noinspection UnusedFunctionResultInspection */ 422 | $processed->add($variable); 423 | 424 | if (is_array($variable)) { 425 | /** @phpstan-ignore foreach.nonIterable */ 426 | foreach ($array as $element) { 427 | if (!is_array($element) && !is_object($element) && !is_resource($element)) { 428 | continue; 429 | } 430 | 431 | if (!is_resource($element)) { 432 | /** @noinspection SlowArrayOperationsInLoopInspection */ 433 | $result = array_merge( 434 | $result, 435 | $this->enumerateObjectsAndResources($element, $processed), 436 | ); 437 | } else { 438 | $result[] = $element; 439 | } 440 | } 441 | } else { 442 | $result[] = $variable; 443 | 444 | foreach ((new ObjectReflector)->getProperties($variable) as $value) { 445 | if (!is_array($value) && !is_object($value) && !is_resource($value)) { 446 | continue; 447 | } 448 | 449 | if (!is_resource($value)) { 450 | /** @noinspection SlowArrayOperationsInLoopInspection */ 451 | $result = array_merge( 452 | $result, 453 | $this->enumerateObjectsAndResources($value, $processed), 454 | ); 455 | } else { 456 | $result[] = $value; 457 | } 458 | } 459 | } 460 | 461 | return $result; 462 | } 463 | } 464 | --------------------------------------------------------------------------------