DependentsResolver::getDependents()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 4
nop 3
dl 0
loc 32
ccs 18
cts 18
cp 1
crap 4
rs 9.7
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright MediaCT. All rights reserved.
4
 * https://www.mediact.nl
5
 */
6
7
namespace Mediact\DependencyGuard\Composer\Repository;
8
9
use Composer\Repository\CompositeRepository;
10
use Composer\Repository\RepositoryInterface;
11
12
/**
13
 * @deprecated The conceptual difference between dependents in Composer and
14
 *   DependencyGuard is too great to rely on the output of a dependents resolver.
15
 */
16
class DependentsResolver implements DependentsResolverInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Mediact\DependencyGuard\...ndentsResolverInterface has been deprecated: The conceptual difference between dependents in Composer and DependencyGuard is too great to rely on the output of a dependents resolver. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

16
class DependentsResolver implements /** @scrutinizer ignore-deprecated */ DependentsResolverInterface

This interface has been deprecated. The supplier of the interface has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.

Loading history...
17
{
18
    /** @var CompositeRepository */
19
    private $repository;
20
21
    /**
22
     * Constructor.
23
     *
24
     * @param RepositoryInterface $repository
25
     */
26 1
    public function __construct(RepositoryInterface $repository)
27
    {
28 1
        $this->repository = new CompositeRepository([$repository]);
29 1
    }
30
31
    /**
32
     * Resolves the dependents of the package.
33
     *
34
     * @param string $packageName
35
     *
36
     * @return array
37
     */
38 5
    public function resolve(string $packageName): array
39
    {
40 5
        return $this->getDependents(
41 5
            $packageName,
42 5
            false,
43 5
            []
44
        );
45
    }
46
47
    /**
48
     * Retrieves the dependents of a package in a recursive way.
49
     *
50
     * @param string $packageName
51
     * @param bool   $returnContext
52
     * @param array  $context
53
     *
54
     * @return array
55
     */
56 10
    private function getDependents(
57
        string $packageName,
58
        bool $returnContext,
59
        array $context = []
60
    ): array {
61 10
        if (!isset($context[$packageName])) {
62 10
            $context[$packageName] = $this->repository->getDependents(
63 10
                $packageName,
64 10
                null,
65 10
                false,
66 10
                false
67
            );
68
69 10
            foreach ($context[$packageName] as $key => $dependent) {
70 4
                $dependentContext = $this->getDependents($key, true, $context);
71 4
                $context          = array_merge(
72 4
                    $context,
73 4
                    $dependentContext
74
                );
75
76 4
                $context[$packageName] = array_merge(
77 4
                    $context[$packageName],
78 4
                    $dependentContext[$key]
79
                );
80
            }
81
        }
82
83 10
        if (!$returnContext) {
84 10
            return $context[$packageName];
85
        }
86
87 4
        return $context;
88
    }
89
}
90