Passed
Pull Request — master (#34)
by
unknown
07:19
created

DependencyFilter::getDependencies()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 2
nop 1
dl 0
loc 19
ccs 12
cts 12
cp 1
crap 3
rs 9.9
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\Violation\Filter;
8
9
use Composer\Composer;
10
use Composer\Package\PackageInterface;
11
use Composer\Repository\CompositeRepository;
12
use Composer\Repository\RepositoryInterface;
13
use Mediact\DependencyGuard\Candidate\Candidate;
14
use Mediact\DependencyGuard\Violation\Violation;
15
use Mediact\DependencyGuard\Violation\ViolationInterface;
16
17
class DependencyFilter implements ViolationFilterInterface
18
{
19
    /** @var ViolationFilterInterface */
20
    private $filter;
21
22
    /** @var CompositeRepository */
23
    private $repository;
24
25
    /** @var array */
26
    private $dependencies;
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param RepositoryInterface      $repository
32
     * @param ViolationFilterInterface $filter
33
     */
34 1
    public function __construct(
35
        RepositoryInterface $repository,
36
        ViolationFilterInterface $filter
37
    ) {
38 1
        $this->filter     = $filter;
39 1
        $this->repository = new CompositeRepository([$repository]);
40 1
    }
41
42
    /**
43
     * Filter violations.
44
     *
45
     * @param ViolationInterface $violation
46
     *
47
     * @return bool
48
     */
49 5
    public function __invoke(ViolationInterface $violation): bool
50
    {
51 5
        return array_reduce(
52 5
            array_map(
53
                function (
54
                    array $dependent
55
                ) use ($violation): ViolationInterface {
56
                    /** @var PackageInterface $package */
57 2
                    [$package] = $dependent;
58 2
                    return new Violation(
59 2
                        sprintf(
60 2
                            'Package "%s" provides violating package "%s".',
61 2
                            $package->getName(),
62 2
                            $violation->getPackage()->getName()
63
                        ),
64 2
                        new Candidate(
65 2
                            $package,
66 2
                            $violation->getSymbols()
67
                        )
68
                    );
69 5
                },
70 5
                $this->getDependencies($violation->getPackage()->getName())
71
            ),
72
            function (bool $carry, ViolationInterface $violation): bool {
73 2
                return $carry || $this->filter->__invoke($violation);
74 5
            },
75 5
            false
76
        );
77
    }
78
79
    /**
80
     * Retrieves the dependencies of a package in a recursive way.
81
     *
82
     * @param string $packageName
83
     *
84
     * @return array
85
     */
86 5
    private function getDependencies(string $packageName): array
87
    {
88 5
        if (!isset($this->dependencies[$packageName])) {
89 5
            $this->dependencies[$packageName] = $this->repository->getDependents(
90 5
                $packageName,
91 5
                null,
92 5
                false,
93 5
                false
94
            );
95
96 5
            foreach ($this->dependencies[$packageName] as $key => $dependent) {
97 2
                $this->dependencies[$packageName] = array_merge(
98 2
                    $this->dependencies[$packageName],
99 2
                    $this->getDependencies($key)
100
                );
101
            }
102
        }
103
104 5
        return $this->dependencies[$packageName];
105
    }
106
}
107