Passed
Push — master ( b328d9...aa9b83 )
by Allan
02:52
created

PackageRepository::getByName()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 56
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 56
rs 8.8337
c 0
b 0
f 0
cc 6
nc 14
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerChangelogs\Repositories;
7
8
use Vaimo\ComposerChangelogs\Exceptions\PackageResolverException;
9
10
class PackageRepository
11
{
12
    /**
13
     * @var \Composer\Package\RootPackageInterface
14
     */
15
    private $rootPackage;
16
17
    /**
18
     * @var \Composer\Repository\WritableRepositoryInterface
19
     */
20
    private $packageRepository;
21
22
    /**
23
     * @var \Vaimo\ComposerChangelogs\Resolvers\RealPackageResolver
24
     */
25
    private $realPackageResolver;
26
27
    /**
28
     * @param \Composer\Package\RootPackageInterface $rootPackage
29
     * @param \Composer\Repository\WritableRepositoryInterface $packageRepository
30
     */
31
    public function __construct(
32
        \Composer\Package\RootPackageInterface $rootPackage,
33
        \Composer\Repository\WritableRepositoryInterface $packageRepository
34
    ) {
35
        $this->rootPackage = $rootPackage;
36
        $this->packageRepository = $packageRepository;
37
38
        $this->realPackageResolver = new \Vaimo\ComposerChangelogs\Resolvers\RealPackageResolver();
39
    }
40
41
    public function getByName($query)
42
    {
43
        /** @var \Composer\Repository\RepositoryInterface[] $repositories */
44
        $repositories = array(
45
            new \Composer\Repository\ArrayRepository(array($this->rootPackage)),
46
            $this->packageRepository
47
        );
48
49
        $matchesGroups = array();
50
        
51
        foreach ($repositories as $repositoryId => $item) {
52
            $matchesGroups[$repositoryId] = $item->search($query);
53
        }
54
55
        if (!array_filter($matchesGroups)) {
56
            throw new PackageResolverException(
57
                sprintf('No packages for query %s', $query)
58
            );
59
        }
60
61
        $matches = array_reduce($matchesGroups, 'array_merge', array());
62
63
        $exactMatches = array_filter($matches, function (array $match) use ($query) {
64
            return $match['name'] === $query;
65
        });
66
        
67
        if ($exactMatches) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $exactMatches of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
68
            $matches = $exactMatches;
69
        }
70
        
71
        if (count($matches) > 1) {
72
            $exception = new PackageResolverException(
73
                sprintf('Multiple packages found for query %s:', $query)
74
            );
75
76
            $exception->setExtraInfo(array_map(function ($match) {
77
                return $match['name'];
78
            }, $matches));
79
80
            throw $exception;
81
        }
82
83
        $repositoryKey = key(array_filter($matchesGroups));
84
85
        $repository = $repositories[$repositoryKey];
86
        $firstMatch = reset($matches);
87
88
        $package = $repository->findPackage($firstMatch['name'], '*');
89
        
90
        if (!$package) {
91
            throw new PackageResolverException(
92
                sprintf('No packages for query %s', $query)
93
            );
94
        }
95
        
96
        return $package;
97
    }
98
}
99