PackageSource::getItems()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 14
rs 10
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerPatches\Sources;
7
8
use Composer\Repository\WritableRepositoryInterface;
9
use Composer\Package\PackageInterface;
10
11
class PackageSource implements \Vaimo\ComposerPatches\Interfaces\PatchSourceListInterface
12
{
13
    /**
14
     * @var array
15
     */
16
    private $packages;
17
18
    /**
19
     * @var \Vaimo\ComposerPatches\Utils\FilterUtils
20
     */
21
    private $filterUtils;
22
23
    /**
24
     * @param array $packages
25
     */
26
    public function __construct(
27
        array $packages = array()
28
    ) {
29
        $this->packages = $packages;
30
31
        $this->filterUtils = new \Vaimo\ComposerPatches\Utils\FilterUtils();
32
    }
33
34
    public function getItems(WritableRepositoryInterface $repository)
35
    {
36
        $packages = $repository->getPackages();
37
38
        if (empty($this->packages)) {
39
            return $packages;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $packages returns the type Composer\Package\PackageInterface[] which is incompatible with the return type mandated by Vaimo\ComposerPatches\In...stInterface::getItems() of array<mixed,string[]>.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
40
        }
41
42
        $filter = $this->filterUtils->composeRegex($this->packages, '/');
43
44
        return array_filter(
45
            $packages,
46
            function (PackageInterface $package) use ($filter) {
47
                return preg_match($filter, $package->getName());
48
            }
49
        );
50
    }
51
}
52