Test Failed
Pull Request — master (#35)
by Jan-Marten
10:48
created

PackageRequirementsFilter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 63
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 28 2
A __construct() 0 8 1
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
    public function __construct(
36
        Locker $locker,
37
        ViolationFilterInterface $filter,
38
        PackageRequirementsResolverInterface $resolver = null
39
    ) {
40
        $this->locker   = $locker;
41
        $this->filter   = $filter;
42
        $this->resolver = $resolver ?? new PackageRequirementsResolver();
43
    }
44
45
    /**
46
     * Filter violations.
47
     *
48
     * @param ViolationInterface $violation
49
     *
50
     * @return bool
51
     */
52
    public function __invoke(ViolationInterface $violation): bool
53
    {
54
        return array_reduce(
55
            array_map(
56
                function (
57
                    PackageInterface $package
58
                ) use ($violation): ViolationInterface {
59
                    return new Violation(
60
                        sprintf(
61
                            'Package "%s" provides violating package "%s".',
62
                            $package->getName(),
63
                            $violation->getPackage()->getName()
64
                        ),
65
                        new Candidate(
66
                            $package,
67
                            $violation->getSymbols()
68
                        )
69
                    );
70
                },
71
                $this->resolver->getDependents(
72
                    $violation->getPackage()->getName(),
73
                    $this->locker
74
                )
75
            ),
76
            function (bool $carry, ViolationInterface $violation): bool {
77
                return $carry || $this->filter->__invoke($violation);
78
            },
79
            false
80
        );
81
    }
82
}
83