├── README.md ├── composer.json ├── LICENSE ├── SECURITY.md ├── src └── Enumerator.php └── ChangeLog.md /README.md: -------------------------------------------------------------------------------- 1 | [![Latest Stable Version](https://poser.pugx.org/sebastian/object-enumerator/v)](https://packagist.org/packages/sebastian/object-enumerator) 2 | [![CI Status](https://github.com/sebastianbergmann/object-enumerator/workflows/CI/badge.svg)](https://github.com/sebastianbergmann/object-enumerator/actions) 3 | [![codecov](https://codecov.io/gh/sebastianbergmann/object-enumerator/branch/main/graph/badge.svg)](https://codecov.io/gh/sebastianbergmann/object-enumerator) 4 | 5 | # sebastian/object-enumerator 6 | 7 | Traverses array structures and object graphs to enumerate all referenced objects. 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/object-enumerator 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/object-enumerator 21 | ``` 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sebastian/object-enumerator", 3 | "description": "Traverses array structures and object graphs to enumerate all referenced objects", 4 | "homepage": "https://github.com/sebastianbergmann/object-enumerator/", 5 | "license": "BSD-3-Clause", 6 | "authors": [ 7 | { 8 | "name": "Sebastian Bergmann", 9 | "email": "sebastian@phpunit.de" 10 | } 11 | ], 12 | "support": { 13 | "issues": "https://github.com/sebastianbergmann/object-enumerator/issues", 14 | "security": "https://github.com/sebastianbergmann/object-enumerator/security/policy" 15 | }, 16 | "prefer-stable": true, 17 | "config": { 18 | "platform": { 19 | "php": "8.3.0" 20 | }, 21 | "optimize-autoloader": true, 22 | "sort-packages": true 23 | }, 24 | "require": { 25 | "php": ">=8.3", 26 | "sebastian/object-reflector": "^5.0", 27 | "sebastian/recursion-context": "^7.0" 28 | }, 29 | "require-dev": { 30 | "phpunit/phpunit": "^12.0" 31 | }, 32 | "autoload": { 33 | "classmap": [ 34 | "src/" 35 | ] 36 | }, 37 | "autoload-dev": { 38 | "classmap": [ 39 | "tests/_fixture/" 40 | ] 41 | }, 42 | "extra": { 43 | "branch-alias": { 44 | "dev-main": "7.0-dev" 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2016-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/Enumerator.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\ObjectEnumerator; 11 | 12 | use function array_merge; 13 | use function is_array; 14 | use function is_object; 15 | use SebastianBergmann\ObjectReflector\ObjectReflector; 16 | use SebastianBergmann\RecursionContext\Context; 17 | 18 | final class Enumerator 19 | { 20 | /** 21 | * @param array|object $variable 22 | * 23 | * @return list 24 | */ 25 | public function enumerate(array|object $variable, Context $processed = new Context): array 26 | { 27 | $objects = []; 28 | 29 | if ($processed->contains($variable) !== false) { 30 | return $objects; 31 | } 32 | 33 | $array = $variable; 34 | 35 | /* @noinspection UnusedFunctionResultInspection */ 36 | $processed->add($variable); 37 | 38 | if (is_array($variable)) { 39 | /** @phpstan-ignore foreach.nonIterable */ 40 | foreach ($array as $element) { 41 | if (!is_array($element) && !is_object($element)) { 42 | continue; 43 | } 44 | 45 | /** @noinspection SlowArrayOperationsInLoopInspection */ 46 | $objects = array_merge( 47 | $objects, 48 | $this->enumerate($element, $processed), 49 | ); 50 | } 51 | 52 | return $objects; 53 | } 54 | 55 | $objects[] = $variable; 56 | 57 | foreach ((new ObjectReflector)->getProperties($variable) as $value) { 58 | if (!is_array($value) && !is_object($value)) { 59 | continue; 60 | } 61 | 62 | /** @noinspection SlowArrayOperationsInLoopInspection */ 63 | $objects = array_merge( 64 | $objects, 65 | $this->enumerate($value, $processed), 66 | ); 67 | } 68 | 69 | return $objects; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /ChangeLog.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to `sebastianbergmann/object-enumerator` are documented in this file using the [Keep a CHANGELOG](http://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.1] - 2024-07-03 12 | 13 | ### Changed 14 | 15 | * This project now uses PHPStan instead of Psalm for static analysis 16 | 17 | ## [6.0.0] - 2024-02-02 18 | 19 | ### Removed 20 | 21 | * This component is no longer supported on PHP 8.1 22 | 23 | ## [5.0.0] - 2023-02-03 24 | 25 | ### Removed 26 | 27 | * This component is no longer supported on PHP 7.3, PHP 7.4 and PHP 8.0 28 | 29 | ## [4.0.4] - 2020-10-26 30 | 31 | ### Fixed 32 | 33 | * `SebastianBergmann\ObjectEnumerator\Exception` now correctly extends `\Throwable` 34 | 35 | ## [4.0.3] - 2020-09-28 36 | 37 | ### Changed 38 | 39 | * Changed PHP version constraint in `composer.json` from `^7.3 || ^8.0` to `>=7.3` 40 | 41 | ## [4.0.2] - 2020-06-26 42 | 43 | ### Added 44 | 45 | * This component is now supported on PHP 8 46 | 47 | ## [4.0.1] - 2020-06-15 48 | 49 | ### Changed 50 | 51 | * Tests etc. are now ignored for archive exports 52 | 53 | ## [4.0.0] - 2020-02-07 54 | 55 | ### Removed 56 | 57 | * This component is no longer supported on PHP 7.0, PHP 7.1, and PHP 7.2 58 | 59 | ## [3.0.3] - 2017-08-03 60 | 61 | ### Changed 62 | 63 | * Bumped required version of `sebastian/object-reflector` 64 | 65 | ## [3.0.2] - 2017-03-12 66 | 67 | ### Changed 68 | 69 | * `sebastian/object-reflector` is now a dependency 70 | 71 | ## [3.0.1] - 2017-03-12 72 | 73 | ### Fixed 74 | 75 | * Objects aggregated in inherited attributes are not enumerated 76 | 77 | ## [3.0.0] - 2017-03-03 78 | 79 | ### Removed 80 | 81 | * This component is no longer supported on PHP 5.6 82 | 83 | ## [2.0.1] - 2017-02-18 84 | 85 | ### Fixed 86 | 87 | * Fixed [#2](https://github.com/sebastianbergmann/phpunit/pull/2): Exceptions in `ReflectionProperty::getValue()` are not handled 88 | 89 | ## [2.0.0] - 2016-11-19 90 | 91 | ### Changed 92 | 93 | * This component is now compatible with `sebastian/recursion-context: ~1.0.4` 94 | 95 | ## 1.0.0 - 2016-02-04 96 | 97 | ### Added 98 | 99 | * Initial release 100 | 101 | [7.0.0]: https://github.com/sebastianbergmann/object-enumerator/compare/6.0...7.0.0 102 | [6.0.1]: https://github.com/sebastianbergmann/object-enumerator/compare/6.0.0...6.0.1 103 | [6.0.0]: https://github.com/sebastianbergmann/object-enumerator/compare/5.0...6.0.0 104 | [5.0.0]: https://github.com/sebastianbergmann/object-enumerator/compare/4.0.4...5.0.0 105 | [4.0.4]: https://github.com/sebastianbergmann/object-enumerator/compare/4.0.3...4.0.4 106 | [4.0.3]: https://github.com/sebastianbergmann/object-enumerator/compare/4.0.2...4.0.3 107 | [4.0.2]: https://github.com/sebastianbergmann/object-enumerator/compare/4.0.1...4.0.2 108 | [4.0.1]: https://github.com/sebastianbergmann/object-enumerator/compare/4.0.0...4.0.1 109 | [4.0.0]: https://github.com/sebastianbergmann/object-enumerator/compare/3.0.3...4.0.0 110 | [3.0.3]: https://github.com/sebastianbergmann/object-enumerator/compare/3.0.2...3.0.3 111 | [3.0.2]: https://github.com/sebastianbergmann/object-enumerator/compare/3.0.1...3.0.2 112 | [3.0.1]: https://github.com/sebastianbergmann/object-enumerator/compare/3.0.0...3.0.1 113 | [3.0.0]: https://github.com/sebastianbergmann/object-enumerator/compare/2.0...3.0.0 114 | [2.0.1]: https://github.com/sebastianbergmann/object-enumerator/compare/2.0.0...2.0.1 115 | [2.0.0]: https://github.com/sebastianbergmann/object-enumerator/compare/1.0...2.0.0 116 | 117 | --------------------------------------------------------------------------------