Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
InterfaceHierarchyResolver
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
8
100.00% covered (success)
100.00%
1 / 1
 direct
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
8
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <dev@noladev.com>
8 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Code\Reflection\Support;
16
17/**
18 * Interface hierarchy resolver class
19 *
20 * Shared by ClassReflection and InterfaceReflection, which previously each carried their own
21 * identical copy of this filter.
22 *
23 * @category   Pop
24 * @package    Pop\Code
25 * @author     Nick Sagona, III <dev@noladev.com>
26 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
27 * @license    https://www.popphp.org/license     New BSD License
28 * @version    6.0.0
29 */
30class InterfaceHierarchyResolver
31{
32
33    /**
34     * Filter a ReflectionClass::getInterfaces() result down to the interfaces directly declared
35     * here -- getInterfaces() returns the full transitive closure, so a candidate is excluded if
36     * it's already reachable via $excludeNames (e.g. a parent class's own interfaces) or is
37     * implied by another candidate already in the set (i.e. reported by that candidate's own
38     * getInterfaceNames(), meaning it isn't a distinct direct declaration here).
39     *
40     * @param  array<string, \ReflectionClass> $interfaces
41     * @param  array<int, string>              $excludeNames
42     * @return array<string, \ReflectionClass>
43     */
44    public static function direct(array $interfaces, array $excludeNames = []): array
45    {
46        // getInterfaceNames() is called once per candidate here, not once per candidate per
47        // candidate -- the transitivity check below needs every candidate's own interface list,
48        // not just the one currently being tested.
49        $interfaceNamesByCandidate = [];
50        foreach ($interfaces as $otherName => $other) {
51            $interfaceNamesByCandidate[$otherName] = $other->getInterfaceNames();
52        }
53
54        $direct = [];
55
56        foreach ($interfaces as $candidateName => $candidate) {
57            if (in_array($candidateName, $excludeNames, true)) {
58                continue;
59            }
60
61            $isTransitive = false;
62            foreach ($interfaceNamesByCandidate as $otherName => $otherInterfaceNames) {
63                if (($otherName !== $candidateName) && in_array($candidateName, $otherInterfaceNames, true)) {
64                    $isTransitive = true;
65                    break;
66                }
67            }
68
69            if (!$isTransitive) {
70                $direct[$candidateName] = $candidate;
71            }
72        }
73
74        return $direct;
75    }
76
77}