Completed
Pull Request — master (#8)
by ARCANEDEV
06:34
created

RepositoriesTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 64
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addRepositories() 0 20 4
A addRepository() 0 17 4
1
<?php namespace Arcanedev\Composer\Entities\PackageTraits;
2
3
use Arcanedev\Composer\Entities\PluginState;
4
use Composer\Package\RootPackageInterface;
5
use Composer\Repository\RepositoryManager;
6
7
/**
8
 * Trait     RepositoriesTrait
9
 *
10
 * @package  Arcanedev\Composer\Entities\PackageTraits
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
trait RepositoriesTrait
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Main Functions
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /**
20
     * Add a collection of repositories described by the given configuration
21
     * to the given package and the global repository manager.
22
     *
23
     * @param  \Composer\Package\RootPackageInterface    $root
24
     * @param  \Arcanedev\Composer\Entities\PluginState  $state
25
     */
26
    private function addRepositories(RootPackageInterface $root, PluginState $state)
27
    {
28
        if (isset($this->json['repositories'])) {
29
            $prepend      = $state->shouldPrependRepositories();
30
            $repoManager  = $this->composer->getRepositoryManager();
0 ignored issues
show
Bug introduced by
The property composer does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31
            $repositories = [];
32
33
            foreach ($this->json['repositories'] as $repoJson) {
0 ignored issues
show
Bug introduced by
The property json does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34
                $this->addRepository($repoManager, $repositories, $repoJson, $prepend);
35
            }
36
37
            /** @var \Composer\Package\RootPackageInterface $unwrapped */
38
            $unwrapped   = self::unwrapIfNeeded($root, 'setRepositories');
39
            $mergedRepos = $prepend
40
                ? array_merge($repositories, $root->getRepositories())
41
                : array_merge($root->getRepositories(), $repositories);
42
43
            $unwrapped->setRepositories($mergedRepos);
44
        }
45
    }
46
47
    /* ------------------------------------------------------------------------------------------------
48
     |  Other Functions
49
     | ------------------------------------------------------------------------------------------------
50
     */
51
    /**
52
     * Add a repository to collection of repositories.
53
     *
54
     * @param  \Composer\Repository\RepositoryManager  $repoManager
55
     * @param  array                                   $repositories
56
     * @param  array                                   $repoJson
57
     * @param  bool                                    $prepend
58
     */
59
    private function addRepository(
60
        RepositoryManager $repoManager, array &$repositories, $repoJson, $prepend
61
    ) {
62
        if (isset($repoJson['type'])) {
63
            $this->logger->info(
0 ignored issues
show
Bug introduced by
The property logger does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
64
                $prepend ? "Prepending {$repoJson['type']} repository" : "Adding {$repoJson['type']} repository"
65
            );
66
67
            $repository = $repoManager->createRepository($repoJson['type'], $repoJson);
68
69
            $prepend
70
                ? $repoManager->prependRepository($repository)
71
                : $repoManager->addRepository($repository);
72
73
            $repositories[] = $repository;
74
        }
75
    }
76
}
77