├── ChangeLog.md ├── LICENSE ├── README.md ├── SECURITY.md ├── composer.json └── src └── Context.php /ChangeLog.md: -------------------------------------------------------------------------------- 1 | # ChangeLog 2 | 3 | All notable changes are documented in this file using the [Keep a CHANGELOG](https://keepachangelog.com/) principles. 4 | 5 | ## [7.0.0] - 2025-02-07 6 | 7 | ### Removed 8 | 9 | * This component is no longer supported on PHP 8.2 10 | 11 | ## [6.0.2] - 2024-07-03 12 | 13 | ### Changed 14 | 15 | * This project now uses PHPStan instead of Psalm for static analysis 16 | 17 | ## [6.0.1] - 2024-06-17 18 | 19 | ### Changed 20 | 21 | * [#30](https://github.com/sebastianbergmann/recursion-context/pull/30): Use more efficient `spl_object_id()` over `spl_object_hash()` 22 | 23 | ## [6.0.0] - 2024-02-02 24 | 25 | ### Removed 26 | 27 | * This component is no longer supported on PHP 8.1 28 | 29 | ## [5.0.0] - 2023-02-03 30 | 31 | ### Removed 32 | 33 | * This component is no longer supported on PHP 7.3, PHP 7.4 and PHP 8.0 34 | 35 | ## [4.0.5] - 2023-02-03 36 | 37 | ### Fixed 38 | 39 | * [#26](https://github.com/sebastianbergmann/recursion-context/pull/26): Don't clobber `null` values if `array_key_exists(PHP_INT_MAX, $array)` 40 | 41 | ## [4.0.4] - 2020-10-26 42 | 43 | ### Fixed 44 | 45 | * `SebastianBergmann\RecursionContext\Exception` now correctly extends `\Throwable` 46 | 47 | ## [4.0.3] - 2020-09-28 48 | 49 | ### Changed 50 | 51 | * [#21](https://github.com/sebastianbergmann/recursion-context/pull/21): Add type annotations for in/out parameters 52 | * Changed PHP version constraint in `composer.json` from `^7.3 || ^8.0` to `>=7.3` 53 | 54 | ## [4.0.2] - 2020-06-26 55 | 56 | ### Added 57 | 58 | * This component is now supported on PHP 8 59 | 60 | ## [4.0.1] - 2020-06-15 61 | 62 | ### Changed 63 | 64 | * Tests etc. are now ignored for archive exports 65 | 66 | [7.0.0]: https://github.com/sebastianbergmann/recursion-context/compare/6.0...7.0.0 67 | [6.0.2]: https://github.com/sebastianbergmann/recursion-context/compare/6.0.1...6.0.2 68 | [6.0.1]: https://github.com/sebastianbergmann/recursion-context/compare/6.0.0...6.0.1 69 | [6.0.0]: https://github.com/sebastianbergmann/recursion-context/compare/5.0...6.0.0 70 | [5.0.0]: https://github.com/sebastianbergmann/recursion-context/compare/4.0.5...5.0.0 71 | [4.0.5]: https://github.com/sebastianbergmann/recursion-context/compare/4.0.4...4.0.5 72 | [4.0.4]: https://github.com/sebastianbergmann/recursion-context/compare/4.0.3...4.0.4 73 | [4.0.3]: https://github.com/sebastianbergmann/recursion-context/compare/4.0.2...4.0.3 74 | [4.0.2]: https://github.com/sebastianbergmann/recursion-context/compare/4.0.1...4.0.2 75 | [4.0.1]: https://github.com/sebastianbergmann/recursion-context/compare/4.0.0...4.0.1 76 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2002-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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Latest Stable Version](https://poser.pugx.org/sebastian/recursion-context/v)](https://packagist.org/packages/sebastian/recursion-context) 2 | [![CI Status](https://github.com/sebastianbergmann/recursion-context/workflows/CI/badge.svg)](https://github.com/sebastianbergmann/recursion-context/actions) 3 | [![codecov](https://codecov.io/gh/sebastianbergmann/recursion-context/branch/main/graph/badge.svg)](https://codecov.io/gh/sebastianbergmann/recursion-context) 4 | 5 | # sebastian/recursion-context 6 | 7 | ## Installation 8 | 9 | You can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/): 10 | 11 | ``` 12 | composer require sebastian/recursion-context 13 | ``` 14 | 15 | 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: 16 | 17 | ``` 18 | composer require --dev sebastian/recursion-context 19 | ``` 20 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sebastian/recursion-context", 3 | "description": "Provides functionality to recursively process PHP variables", 4 | "homepage": "https://github.com/sebastianbergmann/recursion-context", 5 | "license": "BSD-3-Clause", 6 | "authors": [ 7 | { 8 | "name": "Sebastian Bergmann", 9 | "email": "sebastian@phpunit.de" 10 | }, 11 | { 12 | "name": "Jeff Welch", 13 | "email": "whatthejeff@gmail.com" 14 | }, 15 | { 16 | "name": "Adam Harvey", 17 | "email": "aharvey@php.net" 18 | } 19 | ], 20 | "support": { 21 | "issues": "https://github.com/sebastianbergmann/recursion-context/issues", 22 | "security": "https://github.com/sebastianbergmann/recursion-context/security/policy" 23 | }, 24 | "prefer-stable": true, 25 | "config": { 26 | "platform": { 27 | "php": "8.3.0" 28 | }, 29 | "optimize-autoloader": true, 30 | "sort-packages": true 31 | }, 32 | "require": { 33 | "php": ">=8.3" 34 | }, 35 | "require-dev": { 36 | "phpunit/phpunit": "^12.0" 37 | }, 38 | "autoload": { 39 | "classmap": [ 40 | "src/" 41 | ] 42 | }, 43 | "extra": { 44 | "branch-alias": { 45 | "dev-main": "7.0-dev" 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/Context.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\RecursionContext; 11 | 12 | use const PHP_INT_MAX; 13 | use const PHP_INT_MIN; 14 | use function array_key_exists; 15 | use function array_pop; 16 | use function array_slice; 17 | use function count; 18 | use function is_array; 19 | use function is_int; 20 | use function random_int; 21 | use function spl_object_id; 22 | use SplObjectStorage; 23 | 24 | final class Context 25 | { 26 | /** 27 | * @var list> 28 | */ 29 | private array $arrays = []; 30 | 31 | /** 32 | * @var SplObjectStorage 33 | */ 34 | private SplObjectStorage $objects; 35 | 36 | public function __construct() 37 | { 38 | $this->objects = new SplObjectStorage; 39 | } 40 | 41 | /** 42 | * @codeCoverageIgnore 43 | */ 44 | public function __destruct() 45 | { 46 | foreach ($this->arrays as &$array) { 47 | if (is_array($array)) { 48 | array_pop($array); 49 | array_pop($array); 50 | } 51 | } 52 | } 53 | 54 | /** 55 | * @template T of object|array 56 | * 57 | * @param T $value 58 | * 59 | * @param-out T $value 60 | */ 61 | public function add(array|object &$value): int 62 | { 63 | if (is_array($value)) { 64 | /* @phpstan-ignore paramOut.type */ 65 | return $this->addArray($value); 66 | } 67 | 68 | return $this->addObject($value); 69 | } 70 | 71 | /** 72 | * @template T of object|array 73 | * 74 | * @param T $value 75 | * 76 | * @param-out T $value 77 | */ 78 | public function contains(array|object &$value): false|int 79 | { 80 | if (is_array($value)) { 81 | return $this->containsArray($value); 82 | } 83 | 84 | return $this->containsObject($value); 85 | } 86 | 87 | /** 88 | * @param array $array 89 | */ 90 | private function addArray(array &$array): int 91 | { 92 | $key = $this->containsArray($array); 93 | 94 | if ($key !== false) { 95 | return $key; 96 | } 97 | 98 | $key = count($this->arrays); 99 | $this->arrays[] = &$array; 100 | 101 | if (!array_key_exists(PHP_INT_MAX, $array) && !array_key_exists(PHP_INT_MAX - 1, $array)) { 102 | $array[] = $key; 103 | $array[] = $this->objects; 104 | } else { 105 | /* Cover the improbable case, too. 106 | * 107 | * Note that array_slice() (used in containsArray()) will return the 108 | * last two values added, *not necessarily* the highest integer keys 109 | * in the array. Therefore, the order of these writes to $array is 110 | * important, but the actual keys used is not. */ 111 | do { 112 | /** @noinspection PhpUnhandledExceptionInspection */ 113 | $key = random_int(PHP_INT_MIN, PHP_INT_MAX); 114 | } while (array_key_exists($key, $array)); 115 | 116 | $array[$key] = $key; 117 | 118 | do { 119 | /** @noinspection PhpUnhandledExceptionInspection */ 120 | $key = random_int(PHP_INT_MIN, PHP_INT_MAX); 121 | } while (array_key_exists($key, $array)); 122 | 123 | $array[$key] = $this->objects; 124 | } 125 | 126 | return $key; 127 | } 128 | 129 | private function addObject(object $object): int 130 | { 131 | if (!$this->objects->contains($object)) { 132 | $this->objects->attach($object); 133 | } 134 | 135 | return spl_object_id($object); 136 | } 137 | 138 | /** 139 | * @param array $array 140 | */ 141 | private function containsArray(array $array): false|int 142 | { 143 | $end = array_slice($array, -2); 144 | 145 | if (isset($end[1]) && 146 | $end[1] === $this->objects && 147 | isset($end[0]) && 148 | is_int($end[0])) { 149 | return $end[0]; 150 | } 151 | 152 | return false; 153 | } 154 | 155 | private function containsObject(object $value): false|int 156 | { 157 | if ($this->objects->contains($value)) { 158 | return spl_object_id($value); 159 | } 160 | 161 | return false; 162 | } 163 | } 164 | --------------------------------------------------------------------------------