PackageRequirementsFilter::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 19
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 28
ccs 18
cts 18
cp 1
crap 2
rs 9.6333
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\Package\Locker;
10
use Composer\Package\PackageInterface;
11
use Mediact\DependencyGuard\Candidate\Candidate;
12
use Mediact\DependencyGuard\Composer\Locker\PackageRequirementsResolver;
13
use Mediact\DependencyGuard\Composer\Locker\PackageRequirementsResolverInterface;
14
use Mediact\DependencyGuard\Violation\Violation;
15
use Mediact\DependencyGuard\Violation\ViolationInterface;
16
17
class PackageRequirementsFilter implements ViolationFilterInterface
18
{
19
    /** @var Locker */
20
    private $locker;
21
22
    /** @var ViolationFilterInterface */
23
    private $filter;
24
25
    /** @var PackageRequirementsResolverInterface */
26
    private $resolver;
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param Locker                                    $locker
32
     * @param ViolationFilterInterface                  $filter
33
     * @param PackageRequirementsResolverInterface|null $resolver
34
     */
35 1
    public function __construct(
36
        Locker $locker,
37
        ViolationFilterInterface $filter,
38
        PackageRequirementsResolverInterface $resolver = null
39
    ) {
40 1
        $this->locker   = $locker;
41 1
        $this->filter   = $filter;
42 1
        $this->resolver = $resolver ?? new PackageRequirementsResolver();
43 1
    }
44
45
    /**
46
     * Filter violations.
47
     *
48
     * @param ViolationInterface $violation
49
     *
50
     * @return bool
51
     */
52 5
    public function __invoke(ViolationInterface $violation): bool
53
    {
54 5
        return array_reduce(
55 5
            array_map(
56
                function (
57
                    PackageInterface $package
58
                ) use ($violation): ViolationInterface {
59 3
                    return new Violation(
60 3
                        sprintf(
61 3
                            'Package "%s" provides violating package "%s".',
62 3
                            $package->getName(),
63 3
                            $violation->getPackage()->getName()
64
                        ),
65 3
                        new Candidate(
66 3
                            $package,
67 3
                            $violation->getSymbols()
68
                        )
69
                    );
70 5
                },
71 5
                $this->resolver->getDependents(
72 5
                    $violation->getPackage()->getName(),
73 5
                    $this->locker
74
                )
75
            ),
76
            function (bool $carry, ViolationInterface $violation): bool {
77 3
                return $carry || $this->filter->__invoke($violation);
78 5
            },
79 5
            false
80
        );
81
    }
82
}
83