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

ReferencesTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 58
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A mergeReferences() 0 22 3
A extractReferences() 0 14 4
1
<?php namespace Arcanedev\Composer\Entities\PackageTraits;
2
3
use Composer\Package\BasePackage;
4
use Composer\Package\RootPackageInterface;
5
use Composer\Package\Version\VersionParser;
6
7
/**
8
 * Trait     ReferencesTrait
9
 *
10
 * @package  Arcanedev\Composer\Entities\PackageTraits
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
trait ReferencesTrait
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Main Functions
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /**
20
     * Update the root packages reference information.
21
     *
22
     * @param  \Composer\Package\RootPackageInterface  $root
23
     */
24
    protected function mergeReferences(RootPackageInterface $root)
25
    {
26
        // Merge source reference information for merged packages.
27
        // @see \Composer\Package\Loader\RootPackageLoader::load
28
        $references = [];
29
        $unwrapped  = $this->unwrapIfNeeded($root, 'setReferences');
0 ignored issues
show
Bug introduced by
It seems like unwrapIfNeeded() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
30
31
        foreach (['require', 'require-dev'] as $linkType) {
32
            $linkInfo = BasePackage::$supportedLinkTypes[$linkType];
33
            $method   = 'get'.ucfirst($linkInfo['method']);
34
            $links    = [];
35
36
            /** @var  \Composer\Package\Link  $link */
37
            foreach ($unwrapped->$method() as $link) {
38
                $links[$link->getTarget()] = $link->getConstraint()->getPrettyString();
39
            }
40
41
            $references = $this->extractReferences($links, $references);
42
        }
43
44
        $unwrapped->setReferences($references);
45
    }
46
47
    /**
48
     * Extract vcs revision from version constraint (dev-master#abc123).
49
     * @see \Composer\Package\Loader\RootPackageLoader::extractReferences()
50
     *
51
     * @param  array  $requires
52
     * @param  array  $references
53
     *
54
     * @return array
55
     */
56
    private function extractReferences(array $requires, array $references)
57
    {
58
        foreach ($requires as $reqName => $reqVersion) {
59
            $reqVersion = preg_replace('{^([^,\s@]+) as .+$}', '$1', $reqVersion);
60
            if (
61
                preg_match('{^[^,\s@]+?#([a-f0-9]+)$}', $reqVersion, $match) &&
62
                VersionParser::parseStability($reqVersion) === 'dev'
63
            ) {
64
                $references[strtolower($reqName)] = $match[1];
65
            }
66
        }
67
68
        return $references;
69
    }
70
}
71