Snapshot.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of sebastian/global-state.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  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. use function array_keys;
  12. use function array_merge;
  13. use function array_reverse;
  14. use function assert;
  15. use function get_declared_classes;
  16. use function get_declared_interfaces;
  17. use function get_declared_traits;
  18. use function get_defined_constants;
  19. use function get_defined_functions;
  20. use function get_included_files;
  21. use function in_array;
  22. use function ini_get_all;
  23. use function is_array;
  24. use function is_object;
  25. use function is_resource;
  26. use function is_scalar;
  27. use function serialize;
  28. use function unserialize;
  29. use ReflectionClass;
  30. use SebastianBergmann\ObjectReflector\ObjectReflector;
  31. use SebastianBergmann\RecursionContext\Context;
  32. use Throwable;
  33. /**
  34. * A snapshot of global state.
  35. */
  36. final class Snapshot
  37. {
  38. private ExcludeList $excludeList;
  39. /**
  40. * @var array<string, mixed>
  41. */
  42. private array $globalVariables = [];
  43. /**
  44. * @var list<string>
  45. */
  46. private array $superGlobalArrays = [];
  47. /**
  48. * @var array<string, array<string, mixed>>
  49. */
  50. private array $superGlobalVariables = [];
  51. /**
  52. * @var array<string, array<string, mixed>>
  53. */
  54. private array $staticProperties = [];
  55. /**
  56. * @var array<non-empty-string, array{global_value: string, local_value: string, access: int}>
  57. */
  58. private array $iniSettings = [];
  59. /**
  60. * @var list<string>
  61. */
  62. private array $includedFiles = [];
  63. /**
  64. * @var array<string, mixed>
  65. */
  66. private array $constants = [];
  67. /**
  68. * @var list<callable-string>
  69. */
  70. private array $functions = [];
  71. /**
  72. * @var list<class-string>
  73. */
  74. private array $interfaces = [];
  75. /**
  76. * @var list<class-string>
  77. */
  78. private array $classes = [];
  79. /**
  80. * @var list<class-string>
  81. */
  82. private array $traits = [];
  83. 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)
  84. {
  85. $this->excludeList = $excludeList ?: new ExcludeList;
  86. if ($includeConstants) {
  87. $this->snapshotConstants();
  88. }
  89. if ($includeFunctions) {
  90. $this->snapshotFunctions();
  91. }
  92. if ($includeClasses || $includeStaticProperties) {
  93. $this->snapshotClasses();
  94. }
  95. if ($includeInterfaces) {
  96. $this->snapshotInterfaces();
  97. }
  98. if ($includeGlobalVariables) {
  99. $this->setupSuperGlobalArrays();
  100. $this->snapshotGlobals();
  101. }
  102. if ($includeStaticProperties) {
  103. $this->snapshotStaticProperties();
  104. }
  105. if ($includeIniSettings) {
  106. $iniSettings = ini_get_all(null, false);
  107. assert($iniSettings !== false);
  108. $this->iniSettings = $iniSettings;
  109. }
  110. if ($includeIncludedFiles) {
  111. $this->includedFiles = get_included_files();
  112. }
  113. if ($includeTraits) {
  114. $this->traits = get_declared_traits();
  115. }
  116. }
  117. public function excludeList(): ExcludeList
  118. {
  119. return $this->excludeList;
  120. }
  121. /**
  122. * @return array<string, mixed>
  123. */
  124. public function globalVariables(): array
  125. {
  126. return $this->globalVariables;
  127. }
  128. /**
  129. * @return array<string, array<string, mixed>>
  130. */
  131. public function superGlobalVariables(): array
  132. {
  133. return $this->superGlobalVariables;
  134. }
  135. /**
  136. * @return list<string>
  137. */
  138. public function superGlobalArrays(): array
  139. {
  140. return $this->superGlobalArrays;
  141. }
  142. /**
  143. * @return array<string, array<string, mixed>>
  144. */
  145. public function staticProperties(): array
  146. {
  147. return $this->staticProperties;
  148. }
  149. /**
  150. * @return array<non-empty-string, array{global_value: string, local_value: string, access: int}>
  151. */
  152. public function iniSettings(): array
  153. {
  154. return $this->iniSettings;
  155. }
  156. /**
  157. * @return list<string>
  158. */
  159. public function includedFiles(): array
  160. {
  161. return $this->includedFiles;
  162. }
  163. /**
  164. * @return array<string, mixed>
  165. */
  166. public function constants(): array
  167. {
  168. return $this->constants;
  169. }
  170. /**
  171. * @return list<callable-string>
  172. */
  173. public function functions(): array
  174. {
  175. return $this->functions;
  176. }
  177. /**
  178. * @return list<class-string>
  179. */
  180. public function interfaces(): array
  181. {
  182. return $this->interfaces;
  183. }
  184. /**
  185. * @return list<class-string>
  186. */
  187. public function classes(): array
  188. {
  189. return $this->classes;
  190. }
  191. /**
  192. * @return list<class-string>
  193. */
  194. public function traits(): array
  195. {
  196. return $this->traits;
  197. }
  198. private function snapshotConstants(): void
  199. {
  200. $constants = get_defined_constants(true);
  201. if (isset($constants['user'])) {
  202. $this->constants = $constants['user'];
  203. }
  204. }
  205. private function snapshotFunctions(): void
  206. {
  207. $functions = get_defined_functions();
  208. $this->functions = $functions['user'];
  209. }
  210. private function snapshotClasses(): void
  211. {
  212. foreach (array_reverse(get_declared_classes()) as $className) {
  213. $class = new ReflectionClass($className);
  214. if (!$class->isUserDefined()) {
  215. break;
  216. }
  217. $this->classes[] = $className;
  218. }
  219. $this->classes = array_reverse($this->classes);
  220. }
  221. private function snapshotInterfaces(): void
  222. {
  223. foreach (array_reverse(get_declared_interfaces()) as $interfaceName) {
  224. $class = new ReflectionClass($interfaceName);
  225. if (!$class->isUserDefined()) {
  226. break;
  227. }
  228. $this->interfaces[] = $interfaceName;
  229. }
  230. $this->interfaces = array_reverse($this->interfaces);
  231. }
  232. private function snapshotGlobals(): void
  233. {
  234. $superGlobalArrays = $this->superGlobalArrays();
  235. foreach ($superGlobalArrays as $superGlobalArray) {
  236. $this->snapshotSuperGlobalArray($superGlobalArray);
  237. }
  238. foreach (array_keys($GLOBALS) as $key) {
  239. if ($key !== 'GLOBALS' &&
  240. !in_array($key, $superGlobalArrays, true) &&
  241. $this->canBeSerialized($GLOBALS[$key]) &&
  242. !$this->excludeList->isGlobalVariableExcluded($key)) {
  243. /* @noinspection UnserializeExploitsInspection */
  244. $this->globalVariables[$key] = unserialize(serialize($GLOBALS[$key]));
  245. }
  246. }
  247. }
  248. private function snapshotSuperGlobalArray(string $superGlobalArray): void
  249. {
  250. $this->superGlobalVariables[$superGlobalArray] = [];
  251. if (isset($GLOBALS[$superGlobalArray]) && is_array($GLOBALS[$superGlobalArray])) {
  252. foreach ($GLOBALS[$superGlobalArray] as $key => $value) {
  253. /* @noinspection UnserializeExploitsInspection */
  254. $this->superGlobalVariables[$superGlobalArray][$key] = unserialize(serialize($value));
  255. }
  256. }
  257. }
  258. private function snapshotStaticProperties(): void
  259. {
  260. foreach ($this->classes as $className) {
  261. $class = new ReflectionClass($className);
  262. $snapshot = [];
  263. foreach ($class->getProperties() as $property) {
  264. if ($property->isStatic()) {
  265. $name = $property->getName();
  266. if ($this->excludeList->isStaticPropertyExcluded($className, $name)) {
  267. continue;
  268. }
  269. if (!$property->isInitialized()) {
  270. continue;
  271. }
  272. $value = $property->getValue();
  273. if ($this->canBeSerialized($value)) {
  274. /* @noinspection UnserializeExploitsInspection */
  275. $snapshot[$name] = unserialize(serialize($value));
  276. }
  277. }
  278. }
  279. if (!empty($snapshot)) {
  280. $this->staticProperties[$className] = $snapshot;
  281. }
  282. }
  283. }
  284. private function setupSuperGlobalArrays(): void
  285. {
  286. $this->superGlobalArrays = [
  287. '_ENV',
  288. '_POST',
  289. '_GET',
  290. '_COOKIE',
  291. '_SERVER',
  292. '_FILES',
  293. '_REQUEST',
  294. ];
  295. }
  296. private function canBeSerialized(mixed $variable): bool
  297. {
  298. if (is_scalar($variable) || $variable === null) {
  299. return true;
  300. }
  301. if (is_resource($variable)) {
  302. return false;
  303. }
  304. foreach ($this->enumerateObjectsAndResources($variable) as $value) {
  305. if (is_resource($value)) {
  306. return false;
  307. }
  308. if (is_object($value)) {
  309. $class = new ReflectionClass($value);
  310. if ($class->isAnonymous()) {
  311. return false;
  312. }
  313. try {
  314. @serialize($value);
  315. } catch (Throwable $t) {
  316. return false;
  317. }
  318. }
  319. }
  320. return true;
  321. }
  322. /**
  323. * @return array<mixed>
  324. */
  325. private function enumerateObjectsAndResources(mixed $variable, Context $processed = new Context): array
  326. {
  327. $result = [];
  328. if ($processed->contains($variable)) {
  329. return $result;
  330. }
  331. $array = $variable;
  332. /* @noinspection UnusedFunctionResultInspection */
  333. $processed->add($variable);
  334. if (is_array($variable)) {
  335. /** @phpstan-ignore foreach.nonIterable */
  336. foreach ($array as $element) {
  337. if (!is_array($element) && !is_object($element) && !is_resource($element)) {
  338. continue;
  339. }
  340. if (!is_resource($element)) {
  341. /** @noinspection SlowArrayOperationsInLoopInspection */
  342. $result = array_merge(
  343. $result,
  344. $this->enumerateObjectsAndResources($element, $processed),
  345. );
  346. } else {
  347. $result[] = $element;
  348. }
  349. }
  350. } else {
  351. $result[] = $variable;
  352. foreach ((new ObjectReflector)->getProperties($variable) as $value) {
  353. if (!is_array($value) && !is_object($value) && !is_resource($value)) {
  354. continue;
  355. }
  356. if (!is_resource($value)) {
  357. /** @noinspection SlowArrayOperationsInLoopInspection */
  358. $result = array_merge(
  359. $result,
  360. $this->enumerateObjectsAndResources($value, $processed),
  361. );
  362. } else {
  363. $result[] = $value;
  364. }
  365. }
  366. }
  367. return $result;
  368. }
  369. }