Completed
Push — master ( e02cbc...0dd416 )
by Andrii
13:13
created

ComposerJson::getAuthors()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 2
nop 0
1
<?php
2
3
/*
4
 * Composer plugin for HiDev
5
 *
6
 * @link      https://github.com/hiqdev/hidev-composer
7
 * @package   hidev-composer
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\composer\components;
13
14
/**
15
 * `composer.json` config file.
16
 * @author Andrii Vasyliev <[email protected]>
17
 */
18
class ComposerJson extends \hidev\base\ConfigFile
19
{
20
    protected $_file = 'composer.json';
21
22
    public function load()
23
    {
24
        parent::load();
25
        $sets = [
26
            'name'        => $this->getName(),
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
27
            'type'        => $this->getType(),
28
            'description' => $this->take('package')->title,
29
            'keywords'    => $this->take('package')->keywords,
30
            'homepage'    => $this->take('package')->homepage,
31
            'license'     => $this->take('package')->license,
32
            'support'     => $this->getSupport(),
33
            'authors'     => $this->getAuthors(),
34
            'require'     => $this->getRequire(),
35
            'require-dev' => $this->getRequireDev(),
36
            'autoload'    => $this->getAutoload(),
37
        ];
38
        $this->setItems($sets, 'first');
39
        foreach (['require', 'require-dev'] as $k) {
40
            if (!$this->get($k)) {
41
                $this->delete($k);
42
            }
43
        }
44
    }
45
46
    /**
47
     * Converts hidev type to composer type.
48
     * TODO composer type can be different from package type.
49
     * @return string
50
     */
51
    public function getType()
52
    {
53
        return $this->take('package')->type;
54
    }
55
56
    /**
57
     * Converts hidev full name to composer name.
58
     * TODO composer name can be different from package full name.
59
     * @return string
60
     */
61
    public function getName()
62
    {
63
        return $this->take('package')->fullName;
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    public function getFullName()
70
    {
71
        return $this->getName();
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
72
    }
73
74
    public function getSupport()
75
    {
76
        return array_merge(array_filter([
77
            'email'  => $this->take('vendor')->email,
78
            'source' => $this->take('package')->source,
79
            'issues' => $this->take('package')->issues,
80
            'wiki'   => $this->take('package')->wiki,
81
            'forum'  => $this->take('package')->forum,
82
        ]), (array) $this->getItem('support'));
83
    }
84
85
    public function getAuthors()
86
    {
87
        if ($this->take('package')->authors) {
88
            foreach ($this->take('package')->authors as $all_data) {
89
                $data = [];
90
                foreach (['name', 'role', 'email', 'homepage'] as $k) {
91
                    if (!empty($all_data[$k])) {
92
                        $data[$k] = $all_data[$k];
93
                    }
94
                }
95
                $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...
96
            }
97
        }
98
99
        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...
100
    }
101
102
    public function getAutoload()
103
    {
104
        $autoload   = $this->rawItem('autoload');
105
        $psr4       = $autoload['psr-4'] ?: [];
106
        $namespace  = $this->take('package')->namespace;
107
        if (!array_key_exists($namespace, $psr4)) {
108
            $psr4 = [$namespace . '\\' => $this->take('package')->src] + $psr4;
109
            $autoload['psr-4'] = $psr4;
110
            $this->setItem('autoload', $autoload);
111
        }
112
113
        return $autoload;
114
    }
115
116
    /**
117
     * @return array
118
     */
119
    public function getRequire()
120
    {
121
        return $this->getItem('require') ?: [];
122
    }
123
124
    /**
125
     * @return array
126
     */
127
    public function getRequireDev()
128
    {
129
        return $this->getItem('require-dev') ?: [];
130
    }
131
}
132