Passed
Pull Request — master (#47)
by Sander
02:09
created

DependencyResolver::resolveForPackage()   A

Complexity

Conditions 4
Paths 12

Size

Total Lines 52
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 25
c 2
b 0
f 0
dl 0
loc 52
rs 9.52
cc 4
nc 12
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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the composer-link plugin.
7
 *
8
 * Copyright (c) 2021-2022 Sander Visser <[email protected]>.
9
 *
10
 * For the full copyright and license information, please view the LICENSE.md
11
 * file that was distributed with this source code.
12
 *
13
 * @link https://github.com/SanderSander/composer-link
14
 */
15
16
namespace ComposerLink;
17
18
use Composer\Composer;
19
use Composer\DependencyResolver\DefaultPolicy;
20
use Composer\DependencyResolver\Operation\OperationInterface;
21
use Composer\DependencyResolver\Request;
22
use Composer\DependencyResolver\Solver;
23
use Composer\DependencyResolver\SolverProblemsException;
24
use Composer\Filter\PlatformRequirementFilter\PlatformRequirementFilterFactory;
25
use Composer\IO\IOInterface;
26
use Composer\Repository\PathRepository;
27
use Composer\Repository\RepositorySet;
28
use Composer\Repository\RootPackageRepository;
29
use Composer\Semver\Constraint\Constraint;
30
use Composer\Util\ProcessExecutor;
31
32
class DependencyResolver
33
{
34
    protected Composer $composer;
35
36
    protected IOInterface $io;
37
38
    public function __construct(Composer $composer, IOInterface $io)
39
    {
40
        $this->composer = $composer;
41
        $this->io = $io;
42
    }
43
44
    /**
45
     * @return OperationInterface[]
46
     */
47
    public function resolveForPackage(LinkedPackage $package): array
48
    {
49
        // TODO use configuration of original composer.json
50
        $repositorySet = new RepositorySet(
51
            'dev',
52
            [],
53
            [],
54
        );
55
56
        // TODO we can't use this
57
        $exexutor = new ProcessExecutor($this->io);
58
        $exexutor->enableAsync();
59
60
        // Fill repositories first the root package repository
61
        $repositorySet->addRepository(new RootPackageRepository($this->composer->getPackage()));
62
63
        // The locked repository, we don't do a full upgrade, so we want to keep as much as possible packages up to date
64
        $repositorySet->addRepository($this->composer->getLocker()->getLockedRepository(true));
65
66
        // No we add our custom path repositories, we  need to do this before we add the original repositories
67
        // otherwise we do not get precedence over the other repositories
68
        $repo = new PathRepository(['url' => $package->getPath()], $this->io, $this->composer->getConfig(), null, null, $exexutor);
69
        $repositorySet->addRepository($repo);
70
71
        // Add custom repositories defined in the composer file
72
        $repositories = $this->composer->getRepositoryManager()->getRepositories();
73
        foreach ($repositories as $repository) {
74
            $repositorySet->addRepository($repository);
75
        }
76
77
        // Make request for package, and fix all the currently existing packages
78
        $request = new Request();
79
        $request->requireName($package->getName(), new Constraint('=', 'dev-master'));
80
        foreach ($this->composer->getRepositoryManager()->getLocalRepository()->getPackages() as $localPackage) {
81
            $request->fixPackage($localPackage);
82
        }
83
        $policy = new DefaultPolicy(true);
84
85
        // Create pool and solve?
86
        $pool = $repositorySet->createPool($request, $this->io);
87
        $solver = new Solver($policy, $pool, $this->io);
88
89
        $operations = [];
90
91
        try {
92
            $transaction = $solver->solve($request, PlatformRequirementFilterFactory::ignoreAll());
93
            $operations = $transaction->getOperations();
94
        } catch (SolverProblemsException $exception) {
95
            $this->io->write($exception->getPrettyString($repositorySet, $request, $pool, true));
96
        }
97
98
        return $operations;
99
    }
100
}
101