VendorSource::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
use Vaimo\ComposerPatches\Composer\Constants as ComposerConstants;
12
13
class VendorSource implements \Vaimo\ComposerPatches\Interfaces\PatchSourceListInterface
14
{
15
    /**
16
     * @var array
17
     */
18
    private $vendors;
19
20
    /**
21
     * @param array $vendors
22
     */
23
    public function __construct(
24
        array $vendors = array()
25
    ) {
26
        $this->vendors = $vendors;
27
    }
28
29
    public function getItems(WritableRepositoryInterface $repository)
30
    {
31
        $packages = $repository->getPackages();
32
33
        if (empty($this->vendors)) {
34
            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...
35
        }
36
37
        $allowedVendors = array_fill_keys($this->vendors, true);
38
        return array_filter(
39
            $packages,
40
            function (PackageInterface $package) use ($allowedVendors) {
41
                $vendorName = strtok($package->getName(), ComposerConstants::PACKAGE_SEPARATOR);
42
43
                return isset($allowedVendors[$vendorName]);
44
            }
45
        );
46
    }
47
}
48