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

BaseTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 62
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B replaceSelfVersionDependencies() 0 30 3
A unwrapIfNeeded() 0 7 3
1
<?php namespace Arcanedev\Composer\Entities\PackageTraits;
2
3
use Composer\Package\BasePackage;
4
use Composer\Package\Link;
5
use Composer\Package\RootAliasPackage;
6
use Composer\Package\RootPackageInterface;
7
8
/**
9
 * Trait     BaseTrait
10
 *
11
 * @package  Arcanedev\Composer\Entities\PackageTraits
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
trait BaseTrait
15
{
16
    /* ------------------------------------------------------------------------------------------------
17
     |  Other Functions
18
     | ------------------------------------------------------------------------------------------------
19
     */
20
    /**
21
     * Update Links with a 'self.version' constraint with the root package's version.
22
     *
23
     * @param  string                                  $type
24
     * @param  array                                   $links
25
     * @param  \Composer\Package\RootPackageInterface  $root
26
     *
27
     * @return array
28
     */
29
    protected function replaceSelfVersionDependencies(
30
        $type, array $links, RootPackageInterface $root
31
    ) {
32
        $linkType      = BasePackage::$supportedLinkTypes[$type];
33
        $version       = $root->getVersion();
34
        $prettyVersion = $root->getPrettyVersion();
35
        $vp            = $this->versionParser;
0 ignored issues
show
Bug introduced by
The property versionParser 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...
36
        $packages      = $root->{'get' . ucfirst($linkType['method'])}();
37
38
        return array_map(function (Link $link) use ($linkType, $version, $prettyVersion, $vp, $packages) {
39
            if ($link->getPrettyConstraint() !== 'self.version') {
40
                return $link;
41
            }
42
43
            if (isset($packages[$link->getSource()])) {
44
                /** @var  \Composer\Package\Link  $package */
45
                $package       = $packages[$link->getSource()];
46
                $version       = $package->getConstraint()->getPrettyString();
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $version, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
47
                $prettyVersion = $package->getPrettyConstraint();
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $prettyVersion, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
48
            }
49
50
            return new Link(
51
                $link->getSource(),
52
                $link->getTarget(),
53
                $vp->parseConstraints($version),
54
                $linkType['description'],
55
                $prettyVersion
56
            );
57
        }, $links);
58
    }
59
60
    /**
61
     * Get a full featured Package from a RootPackageInterface.
62
     *
63
     * @param  \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage  $root
64
     * @param  string                                                                $method
65
     *
66
     * @return \Composer\Package\RootPackageInterface|\Composer\Package\RootPackage
67
     */
68
    private static function unwrapIfNeeded(
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
69
        RootPackageInterface $root, $method = 'setExtra'
70
    ) {
71
        return ($root instanceof RootAliasPackage && ! method_exists($root, $method))
72
            ? $root->getAliasOf()
73
            : $root;
74
    }
75
}
76