PK XpK- .travis.ymlnu W+A language: php
php:
- 7.0
- 7.1
- 7.2
- master
sudo: false
before_install:
- composer self-update
- composer clear-cache
install:
- travis_retry composer update --no-interaction --no-ansi --no-progress --no-suggest --optimize-autoloader --prefer-stable
script:
- ./vendor/bin/phpunit --coverage-clover=coverage.xml
after_success:
- bash <(curl -s https://codecov.io/bash)
notifications:
email: false
PK XpKV .php_csnu W+A
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
EOF;
return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules(
[
'array_syntax' => ['syntax' => 'short'],
'binary_operator_spaces' => [
'align_double_arrow' => true,
'align_equals' => true
],
'blank_line_after_namespace' => true,
'blank_line_before_return' => true,
'braces' => true,
'cast_spaces' => true,
'concat_space' => ['spacing' => 'one'],
'elseif' => true,
'encoding' => true,
'full_opening_tag' => true,
'function_declaration' => true,
#'header_comment' => ['header' => $header, 'separate' => 'none'],
'indentation_type' => true,
'line_ending' => true,
'lowercase_constants' => true,
'lowercase_keywords' => true,
'method_argument_space' => true,
'native_function_invocation' => true,
'no_alias_functions' => true,
'no_blank_lines_after_class_opening' => true,
'no_blank_lines_after_phpdoc' => true,
'no_closing_tag' => true,
'no_empty_phpdoc' => true,
'no_empty_statement' => true,
'no_extra_consecutive_blank_lines' => true,
'no_leading_namespace_whitespace' => true,
'no_singleline_whitespace_before_semicolons' => true,
'no_spaces_after_function_name' => true,
'no_spaces_inside_parenthesis' => true,
'no_trailing_comma_in_list_call' => true,
'no_trailing_whitespace' => true,
'no_unused_imports' => true,
'no_whitespace_in_blank_line' => true,
'phpdoc_align' => true,
'phpdoc_indent' => true,
'phpdoc_no_access' => true,
'phpdoc_no_empty_return' => true,
'phpdoc_no_package' => true,
'phpdoc_scalar' => true,
'phpdoc_separation' => true,
'phpdoc_to_comment' => true,
'phpdoc_trim' => true,
'phpdoc_types' => true,
'phpdoc_var_without_name' => true,
'self_accessor' => true,
'simplified_null_return' => true,
'single_blank_line_at_eof' => true,
'single_import_per_statement' => true,
'single_line_after_imports' => true,
'single_quote' => true,
'ternary_operator_spaces' => true,
'trim_array_spaces' => true,
'visibility_required' => true,
]
)
->setFinder(
PhpCsFixer\Finder::create()
->files()
->in(__DIR__ . '/src')
->in(__DIR__ . '/tests')
->name('*.php')
);
PK XpKz z README.mdnu W+A # GlobalState
Snapshotting of global state, factored out of PHPUnit into a stand-alone component.
[![Build Status](https://travis-ci.org/sebastianbergmann/global-state.svg?branch=master)](https://travis-ci.org/sebastianbergmann/global-state)
## Installation
You can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/):
composer require sebastian/global-state
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:
composer require --dev sebastian/global-state
PK XpK<- -
.gitignorenu W+A /.idea
/.php_cs.cache
/composer.lock
/vendor
PK XpK1: : phpunit.xmlnu W+A
tests
src
PK XpKM" " src/Restorer.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace SebastianBergmann\GlobalState;
use ReflectionProperty;
/**
* Restorer of snapshots of global state.
*/
class Restorer
{
/**
* Deletes function definitions that are not defined in a snapshot.
*
* @throws RuntimeException when the uopz_delete() function is not available
*
* @see https://github.com/krakjoe/uopz
*/
public function restoreFunctions(Snapshot $snapshot)
{
if (!\function_exists('uopz_delete')) {
throw new RuntimeException('The uopz_delete() function is required for this operation');
}
$functions = \get_defined_functions();
foreach (\array_diff($functions['user'], $snapshot->functions()) as $function) {
uopz_delete($function);
}
}
/**
* Restores all global and super-global variables from a snapshot.
*/
public function restoreGlobalVariables(Snapshot $snapshot)
{
$superGlobalArrays = $snapshot->superGlobalArrays();
foreach ($superGlobalArrays as $superGlobalArray) {
$this->restoreSuperGlobalArray($snapshot, $superGlobalArray);
}
$globalVariables = $snapshot->globalVariables();
foreach (\array_keys($GLOBALS) as $key) {
if ($key != 'GLOBALS' &&
!\in_array($key, $superGlobalArrays) &&
!$snapshot->blacklist()->isGlobalVariableBlacklisted($key)) {
if (\array_key_exists($key, $globalVariables)) {
$GLOBALS[$key] = $globalVariables[$key];
} else {
unset($GLOBALS[$key]);
}
}
}
}
/**
* Restores all static attributes in user-defined classes from this snapshot.
*/
public function restoreStaticAttributes(Snapshot $snapshot)
{
$current = new Snapshot($snapshot->blacklist(), false, false, false, false, true, false, false, false, false);
$newClasses = \array_diff($current->classes(), $snapshot->classes());
unset($current);
foreach ($snapshot->staticAttributes() as $className => $staticAttributes) {
foreach ($staticAttributes as $name => $value) {
$reflector = new ReflectionProperty($className, $name);
$reflector->setAccessible(true);
$reflector->setValue($value);
}
}
foreach ($newClasses as $className) {
$class = new \ReflectionClass($className);
$defaults = $class->getDefaultProperties();
foreach ($class->getProperties() as $attribute) {
if (!$attribute->isStatic()) {
continue;
}
$name = $attribute->getName();
if ($snapshot->blacklist()->isStaticAttributeBlacklisted($className, $name)) {
continue;
}
if (!isset($defaults[$name])) {
continue;
}
$attribute->setAccessible(true);
$attribute->setValue($defaults[$name]);
}
}
}
/**
* Restores a super-global variable array from this snapshot.
*/
private function restoreSuperGlobalArray(Snapshot $snapshot, string $superGlobalArray)
{
$superGlobalVariables = $snapshot->superGlobalVariables();
if (isset($GLOBALS[$superGlobalArray]) &&
\is_array($GLOBALS[$superGlobalArray]) &&
isset($superGlobalVariables[$superGlobalArray])) {
$keys = \array_keys(
\array_merge(
$GLOBALS[$superGlobalArray],
$superGlobalVariables[$superGlobalArray]
)
);
foreach ($keys as $key) {
if (isset($superGlobalVariables[$superGlobalArray][$key])) {
$GLOBALS[$superGlobalArray][$key] = $superGlobalVariables[$superGlobalArray][$key];
} else {
unset($GLOBALS[$superGlobalArray][$key]);
}
}
}
}
}
PK XpK|!f f src/CodeExporter.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace SebastianBergmann\GlobalState;
/**
* Exports parts of a Snapshot as PHP code.
*/
class CodeExporter
{
public function constants(Snapshot $snapshot): string
{
$result = '';
foreach ($snapshot->constants() as $name => $value) {
$result .= \sprintf(
'if (!defined(\'%s\')) define(\'%s\', %s);' . "\n",
$name,
$name,
$this->exportVariable($value)
);
}
return $result;
}
public function globalVariables(Snapshot $snapshot): string
{
$result = '$GLOBALS = [];' . PHP_EOL;
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 = '';
foreach ($snapshot->iniSettings() as $key => $value) {
$result .= \sprintf(
'@ini_set(%s, %s);' . "\n",
$this->exportVariable($key),
$this->exportVariable($value)
);
}
return $result;
}
private function exportVariable($variable): string
{
if (\is_scalar($variable) || \is_null($variable) ||
(\is_array($variable) && $this->arrayOnlyContainsScalars($variable))) {
return \var_export($variable, true);
}
return 'unserialize(' . \var_export(\serialize($variable), true) . ')';
}
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 = false;
}
if ($result === false) {
break;
}
}
return $result;
}
}
PK XpKÒ! ! src/Snapshot.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace SebastianBergmann\GlobalState;
use ReflectionClass;
use Serializable;
/**
* A snapshot of global state.
*/
class Snapshot
{
/**
* @var Blacklist
*/
private $blacklist;
/**
* @var array
*/
private $globalVariables = [];
/**
* @var array
*/
private $superGlobalArrays = [];
/**
* @var array
*/
private $superGlobalVariables = [];
/**
* @var array
*/
private $staticAttributes = [];
/**
* @var array
*/
private $iniSettings = [];
/**
* @var array
*/
private $includedFiles = [];
/**
* @var array
*/
private $constants = [];
/**
* @var array
*/
private $functions = [];
/**
* @var array
*/
private $interfaces = [];
/**
* @var array
*/
private $classes = [];
/**
* @var array
*/
private $traits = [];
/**
* Creates a snapshot of the current global state.
*/
public function __construct(Blacklist $blacklist = 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;
if ($includeConstants) {
$this->snapshotConstants();
}
if ($includeFunctions) {
$this->snapshotFunctions();
}
if ($includeClasses || $includeStaticAttributes) {
$this->snapshotClasses();
}
if ($includeInterfaces) {
$this->snapshotInterfaces();
}
if ($includeGlobalVariables) {
$this->setupSuperGlobalArrays();
$this->snapshotGlobals();
}
if ($includeStaticAttributes) {
$this->snapshotStaticAttributes();
}
if ($includeIniSettings) {
$this->iniSettings = \ini_get_all(null, false);
}
if ($includeIncludedFiles) {
$this->includedFiles = \get_included_files();
}
$this->traits = \get_declared_traits();
}
public function blacklist(): Blacklist
{
return $this->blacklist;
}
public function globalVariables(): array
{
return $this->globalVariables;
}
public function superGlobalVariables(): array
{
return $this->superGlobalVariables;
}
public function superGlobalArrays(): array
{
return $this->superGlobalArrays;
}
public function staticAttributes(): array
{
return $this->staticAttributes;
}
public function iniSettings(): array
{
return $this->iniSettings;
}
public function includedFiles(): array
{
return $this->includedFiles;
}
public function constants(): array
{
return $this->constants;
}
public function functions(): array
{
return $this->functions;
}
public function interfaces(): array
{
return $this->interfaces;
}
public function classes(): array
{
return $this->classes;
}
public function traits(): array
{
return $this->traits;
}
/**
* Creates a snapshot user-defined constants.
*/
private function snapshotConstants()
{
$constants = \get_defined_constants(true);
if (isset($constants['user'])) {
$this->constants = $constants['user'];
}
}
/**
* Creates a snapshot user-defined functions.
*/
private function snapshotFunctions()
{
$functions = \get_defined_functions();
$this->functions = $functions['user'];
}
/**
* Creates a snapshot user-defined classes.
*/
private function snapshotClasses()
{
foreach (\array_reverse(\get_declared_classes()) as $className) {
$class = new ReflectionClass($className);
if (!$class->isUserDefined()) {
break;
}
$this->classes[] = $className;
}
$this->classes = \array_reverse($this->classes);
}
/**
* Creates a snapshot user-defined interfaces.
*/
private function snapshotInterfaces()
{
foreach (\array_reverse(\get_declared_interfaces()) as $interfaceName) {
$class = new ReflectionClass($interfaceName);
if (!$class->isUserDefined()) {
break;
}
$this->interfaces[] = $interfaceName;
}
$this->interfaces = \array_reverse($this->interfaces);
}
/**
* Creates a snapshot of all global and super-global variables.
*/
private function snapshotGlobals()
{
$superGlobalArrays = $this->superGlobalArrays();
foreach ($superGlobalArrays as $superGlobalArray) {
$this->snapshotSuperGlobalArray($superGlobalArray);
}
foreach (\array_keys($GLOBALS) as $key) {
if ($key != 'GLOBALS' &&
!\in_array($key, $superGlobalArrays) &&
$this->canBeSerialized($GLOBALS[$key]) &&
!$this->blacklist->isGlobalVariableBlacklisted($key)) {
$this->globalVariables[$key] = \unserialize(\serialize($GLOBALS[$key]));
}
}
}
/**
* Creates a snapshot a super-global variable array.
*/
private function snapshotSuperGlobalArray(string $superGlobalArray)
{
$this->superGlobalVariables[$superGlobalArray] = [];
if (isset($GLOBALS[$superGlobalArray]) && \is_array($GLOBALS[$superGlobalArray])) {
foreach ($GLOBALS[$superGlobalArray] as $key => $value) {
$this->superGlobalVariables[$superGlobalArray][$key] = \unserialize(\serialize($value));
}
}
}
/**
* Creates a snapshot of all static attributes in user-defined classes.
*/
private function snapshotStaticAttributes()
{
foreach ($this->classes as $className) {
$class = new ReflectionClass($className);
$snapshot = [];
foreach ($class->getProperties() as $attribute) {
if ($attribute->isStatic()) {
$name = $attribute->getName();
if ($this->blacklist->isStaticAttributeBlacklisted($className, $name)) {
continue;
}
$attribute->setAccessible(true);
$value = $attribute->getValue();
if ($this->canBeSerialized($value)) {
$snapshot[$name] = \unserialize(\serialize($value));
}
}
}
if (!empty($snapshot)) {
$this->staticAttributes[$className] = $snapshot;
}
}
}
/**
* Returns a list of all super-global variable arrays.
*/
private function setupSuperGlobalArrays()
{
$this->superGlobalArrays = [
'_ENV',
'_POST',
'_GET',
'_COOKIE',
'_SERVER',
'_FILES',
'_REQUEST'
];
if (\ini_get('register_long_arrays') == '1') {
$this->superGlobalArrays = \array_merge(
$this->superGlobalArrays,
[
'HTTP_ENV_VARS',
'HTTP_POST_VARS',
'HTTP_GET_VARS',
'HTTP_COOKIE_VARS',
'HTTP_SERVER_VARS',
'HTTP_POST_FILES'
]
);
}
}
/**
* @todo Implement this properly
*/
private function canBeSerialized($variable): bool
{
if (!\is_object($variable)) {
return !\is_resource($variable);
}
if ($variable instanceof \stdClass) {
return true;
}
$class = new ReflectionClass($variable);
do {
if ($class->isInternal()) {
return $variable instanceof Serializable;
}
} while ($class = $class->getParentClass());
return true;
}
}
PK XpKܫ9
src/Blacklist.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace SebastianBergmann\GlobalState;
use ReflectionClass;
/**
* A blacklist for global state elements that should not be snapshotted.
*/
class Blacklist
{
/**
* @var array
*/
private $globalVariables = [];
/**
* @var string[]
*/
private $classes = [];
/**
* @var string[]
*/
private $classNamePrefixes = [];
/**
* @var string[]
*/
private $parentClasses = [];
/**
* @var string[]
*/
private $interfaces = [];
/**
* @var array
*/
private $staticAttributes = [];
public function addGlobalVariable(string $variableName)
{
$this->globalVariables[$variableName] = true;
}
public function addClass(string $className)
{
$this->classes[] = $className;
}
public function addSubclassesOf(string $className)
{
$this->parentClasses[] = $className;
}
public function addImplementorsOf(string $interfaceName)
{
$this->interfaces[] = $interfaceName;
}
public function addClassNamePrefix(string $classNamePrefix)
{
$this->classNamePrefixes[] = $classNamePrefix;
}
public function addStaticAttribute(string $className, string $attributeName)
{
if (!isset($this->staticAttributes[$className])) {
$this->staticAttributes[$className] = [];
}
$this->staticAttributes[$className][$attributeName] = true;
}
public function isGlobalVariableBlacklisted(string $variableName): bool
{
return isset($this->globalVariables[$variableName]);
}
public function isStaticAttributeBlacklisted(string $className, string $attributeName): bool
{
if (\in_array($className, $this->classes)) {
return true;
}
foreach ($this->classNamePrefixes as $prefix) {
if (\strpos($className, $prefix) === 0) {
return true;
}
}
$class = new ReflectionClass($className);
foreach ($this->parentClasses as $type) {
if ($class->isSubclassOf($type)) {
return true;
}
}
foreach ($this->interfaces as $type) {
if ($class->implementsInterface($type)) {
return true;
}
}
if (isset($this->staticAttributes[$className][$attributeName])) {
return true;
}
return false;
}
}
PK XpKy˂ # src/exceptions/RuntimeException.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace SebastianBergmann\GlobalState;
class RuntimeException extends \RuntimeException implements Exception
{
}
PK XpKWZP P src/exceptions/Exception.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace SebastianBergmann\GlobalState;
interface Exception
{
}
PK XpKOu"U U build.xmlnu W+A
PK XpK tests/CodeExporterTest.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace SebastianBergmann\GlobalState;
use PHPUnit\Framework\TestCase;
/**
* @covers \SebastianBergmann\GlobalState\CodeExporter
*/
class CodeExporterTest extends TestCase
{
/**
* @runInSeparateProcess
*/
public function testCanExportGlobalVariablesToCode()
{
$GLOBALS = ['foo' => 'bar'];
$snapshot = new Snapshot(null, true, false, false, false, false, false, false, false, false);
$exporter = new CodeExporter;
$this->assertEquals(
'$GLOBALS = [];' . PHP_EOL . '$GLOBALS[\'foo\'] = \'bar\';' . PHP_EOL,
$exporter->globalVariables($snapshot)
);
}
}
PK XpK} } ( tests/_fixture/BlacklistedChildClass.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace SebastianBergmann\GlobalState\TestFixture;
class BlacklistedChildClass extends BlacklistedClass
{
}
PK XpKיR~ ~ # tests/_fixture/BlacklistedClass.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace SebastianBergmann\GlobalState\TestFixture;
class BlacklistedClass
{
private static $attribute;
}
PK XpK.GL ) tests/_fixture/BlacklistedImplementor.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace SebastianBergmann\GlobalState\TestFixture;
class BlacklistedImplementor implements BlacklistedInterface
{
private static $attribute;
}
PK XpK-|= tests/_fixture/SnapshotClass.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace SebastianBergmann\GlobalState\TestFixture;
use DomDocument;
use ArrayObject;
class SnapshotClass
{
private static $string = 'snapshot';
private static $dom;
private static $closure;
private static $arrayObject;
private static $snapshotDomDocument;
private static $resource;
private static $stdClass;
public static function init()
{
self::$dom = new DomDocument();
self::$closure = function () {};
self::$arrayObject = new ArrayObject([1, 2, 3]);
self::$snapshotDomDocument = new SnapshotDomDocument();
self::$resource = \fopen('php://memory', 'r');
self::$stdClass = new \stdClass();
}
}
PK XpK*!g g ' tests/_fixture/BlacklistedInterface.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace SebastianBergmann\GlobalState\TestFixture;
interface BlacklistedInterface
{
}
PK XpK#d d $ tests/_fixture/SnapshotFunctions.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace SebastianBergmann\GlobalState\TestFixture;
function snapshotFunction()
{
}
PK XpK[Y\ \ tests/_fixture/SnapshotTrait.phpnu W+A
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace SebastianBergmann\GlobalState\TestFixture;
trait SnapshotTrait
{
}
PK XpKyT &