Completed
Push — master ( 267784...4905d2 )
by Andrii
02:05
created

PackageManager::isAnyVersion()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 3
eloc 2
nc 3
nop 1
1
<?php
2
3
/*
4
 * Composer plugin for bower/npm assets
5
 *
6
 * @link      https://github.com/hiqdev/composer-asset-plugin
7
 * @package   composer-asset-plugin
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\composerassetplugin;
13
14
use Composer\Json\JsonFile;
15
use Composer\Package\CompletePackage;
16
17
/**
18
 * Abstract package manager class.
19
 *
20
 * @author Andrii Vasyliev <[email protected]>
21
 */
22
abstract class PackageManager
23
{
24
    /**
25
     * The plugin.
26
     * @var Plugin
27
     */
28
    protected $plugin;
29
30
    protected $config = [];
31
32
    protected $opTable = [
33
        'require'     => 'dependencies',
34
        'require-dev' => 'devDependencies',
35
    ];
36
37
    protected $file;
38
39
    protected $name;
40
41
    protected $jsonFile;
42
43
    /**
44
     * Reads config file or dist config if exists.
45
     */
46
    public function __construct(Plugin $plugin)
47
    {
48
        $this->plugin = $plugin;
49
        $dist = $this->file . '.dist';
50
        $this->config = $this->readConfig(file_exists($dist) ? $dist : $this->file);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->readConfig(file_e... ? $dist : $this->file) of type * is incompatible with the declared type array of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
51
    }
52
53
    public function readConfig($file)
54
    {
55
        $jsonFile = new JsonFile($file);
56
        $config = $jsonFile->exists() ? $jsonFile->read() : [];
57
        foreach ($this->opTable as $key) {
58
            if (!isset($config[$key])) {
59
                $config[$key] = [];
60
            }
61
        }
62
        return $config;
63
    }
64
65
    public function writeConfig($file, $config)
66
    {
67
        foreach ($this->opTable as $key) {
68
            if (isset($config[$key]) && !$config[$key]) {
69
                unset($config[$key]);
70
            }
71
        }
72
        $jsonFile = new JsonFile($file);
73
        $jsonFile->write($config);
74
    }
75
76
    public function scanPackage(CompletePackage $package)
77
    {
78
        $extra = $package->getExtra();
79
        $config = [];
80
        foreach (['require', 'require-dev'] as $key) {
81
            $name = $this->name . '-' . $key;
82
            if (isset($extra[$name])) {
83
                $config[$key] = $extra[$name];
84
            }
85
        }
86
        if ($config) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $config 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...
87
            $this->mergeConfig($config);
88
        }
89
    }
90
91
    protected function mergeConfig($config)
92
    {
93
        foreach ($config as $key => $packages) {
94
            $key = $this->opTable[$key];
95
            foreach ($packages as $name => $version) {
96
                $this->config[$key][$name] = isset($this->config[$key][$name])
97
                    ? $this->mergeVersions($this->config[$key][$name], $version)
98
                    : $version;
99
            }
100
        }
101
    }
102
103
    protected function mergeVersions($a, $b)
104
    {
105
        $a = trim($a);
106
        $b = trim($b);
107
        if ($a === $b || $this->isMoreVersion($b, $a)) {
108
            return $a;
109
        } elseif ($this->isMoreVersion($a, $b)) {
110
            return $b;
111
        } else {
112
            return $a . ' ' . $b;
113
        }
114
    }
115
116
    /**
117
     * Check if $a is more then $b, like: a="1.1 || 2.2" b="1.1"
118
     * Possible optimization.
119
     */
120
    public function isMoreVersion($a, $b)
0 ignored issues
show
Unused Code introduced by
The parameter $b is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
121
    {
122
        return $this->isAnyVersion($a);
123
        // WRONG: return strpos($b, $a) !== false;
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
124
    }
125
126
    public function isAnyVersion($version)
127
    {
128
        return !$version || $version === '*' || $version === '>=0.0.0';
129
    }
130
131
    public function loadPackages(array $config)
132
    {
133
        $this->config = $config;
134
    }
135
136
    abstract public function installPackages();
137
}
138