Completed
Push — master ( ef54d6...631171 )
by Andrii
01:51
created

ComposerJson::old_getAutoload()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 0
cts 12
cp 0
rs 9.4285
cc 3
eloc 9
nc 4
nop 0
crap 12
1
<?php
2
/**
3
 * Composer plugin for HiDev
4
 *
5
 * @link      https://github.com/hiqdev/hidev-composer
6
 * @package   hidev-composer
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\composer\components;
12
13
/**
14
 * `composer.json` config file.
15
 * @author Andrii Vasyliev <[email protected]>
16
 */
17
class ComposerJson extends \hidev\base\ConfigFile
18
{
19
    protected $_file = 'composer.json';
20
21
    public function load()
22
    {
23
        parent::load();
24
        $sets = array_filter([
25
            'name'        => $this->getName(),
26
            'type'        => $this->getType(),
27
            'description' => $this->take('package')->title,
28
            'keywords'    => $this->take('package')->keywords,
29
            'homepage'    => $this->take('package')->homepage,
30
            'license'     => $this->take('package')->license,
31
            'support'     => $this->getSupport(),
32
            'authors'     => $this->getAuthors(),
33
            'require'     => $this->getRequire(),
34
            'require-dev' => $this->getRequireDev(),
35
        ]);
36
        $this->setItems($sets, 'first');
37
    }
38
39
    /**
40
     * Converts hidev type to composer type.
41
     * TODO composer type can be different from package type.
42
     * @return string
43
     */
44
    public function getType()
45
    {
46
        return $this->take('package')->type;
47
    }
48
49
    /**
50
     * Converts hidev full name to composer name.
51
     * TODO composer name can be different from package full name.
52
     * @return string
53
     */
54
    public function getName()
55
    {
56
        return $this->take('package')->fullName;
57
    }
58
59
    /**
60
     * @return string
61
     */
62
    public function getFullName()
63
    {
64
        return $this->getName();
65
    }
66
67
    public function getSupport()
68
    {
69
        return array_merge(array_filter([
70
            'email'  => $this->take('vendor')->email,
71
            'source' => $this->take('package')->source,
72
            'issues' => $this->take('package')->issues,
73
            'wiki'   => $this->take('package')->wiki,
74
            'forum'  => $this->take('package')->forum,
75
        ]), (array) $this->getItem('support'));
76
    }
77
78
    public function getAuthors()
79
    {
80
        if ($this->take('package')->authors) {
81
            foreach ($this->take('package')->authors as $all_data) {
82
                $data = [];
83
                foreach (['name', 'role', 'email', 'homepage'] as $k) {
84
                    if (!empty($all_data[$k])) {
85
                        $data[$k] = $all_data[$k];
86
                    }
87
                }
88
                $res[] = $data;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$res was never initialized. Although not strictly required by PHP, it is generally a good practice to add $res = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
89
            }
90
        }
91
92
        return $res;
0 ignored issues
show
Bug introduced by
The variable $res does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
93
    }
94
95
    /**
96
     * XXX DISABLED, think if it's needed :-/.
97
     * @return array
98
     */
99
    public function old_getAutoload()
100
    {
101
        $autoload   = $this->rawItem('autoload');
102
        $psr4       = $autoload['psr-4'] ?: [];
103
        $namespace  = $this->take('package')->namespace;
104
        if (!array_key_exists($namespace, $psr4)) {
105
            $psr4 = [$namespace . '\\' => $this->take('package')->src] + $psr4;
106
            $autoload['psr-4'] = $psr4;
107
            $this->setItem('autoload', $autoload);
108
        }
109
110
        return $autoload;
111
    }
112
113
    /**
114
     * @return array
115
     */
116
    public function getRequire()
117
    {
118
        return $this->getItem('require') ?: [];
119
    }
120
121
    /**
122
     * @return array
123
     */
124
    public function getRequireDev()
125
    {
126
        return $this->getItem('require-dev') ?: [];
127
    }
128
}
129