Completed
Push — master ( 97ee3e...cbe820 )
by Andrii
12:37
created

ComposerJsonController::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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\controllers;
13
14
/**
15
 * Goal for composer.json.
16
 */
17
class ComposerJsonController extends \hidev\controllers\FileController
18
{
19
    protected $_file = 'composer.json';
20
21
    public function actionLoad()
22
    {
23
        parent::actionLoad();
24
        $sets = [
25
            '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...
26
            'type'        => $this->getType(),
27
            'description' => $this->takePackage()->title,
28
            'keywords'    => $this->takePackage()->keywords,
29
            'homepage'    => $this->takePackage()->homepage,
30
            'license'     => $this->takePackage()->license,
31
            'support'     => $this->support,
32
            'authors'     => $this->authors,
33
            'require'     => $this->require,
34
            'require-dev' => $this->get('require-dev'),
35
            'autoload'    => $this->autoload,
36
        ];
37
        $this->setItems($sets, 'first');
38
        foreach (['require', 'require-dev'] as $k) {
39
            if (!$this->get($k)) {
40
                $this->delete($k);
41
            }
42
        }
43
    }
44
45
    /**
46
     * Converts hidev type to composer type.
47
     * TODO composer type can be different from package type.
48
     * @return string
49
     */
50
    public function getType()
51
    {
52
        return $this->takePackage()->type;
53
    }
54
55
    /**
56
     * Converts hidev full name to composer name.
57
     * TODO composer name can be different from package full name.
58
     * @return string
59
     */
60
    public function getName()
61
    {
62
        return $this->takePackage()->fullName;
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getFullName()
69
    {
70
        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...
71
    }
72
73
    public function getSupport()
74
    {
75
        return array_merge(array_filter([
76
            'email'  => $this->takeVendor()->email,
77
            'source' => $this->takePackage()->source,
78
            'issues' => $this->takePackage()->issues,
79
            'wiki'   => $this->takePackage()->wiki,
80
            'forum'  => $this->takePackage()->forum,
81
        ]), (array) $this->getItem('support'));
82
    }
83
84
    public function getAuthors()
85
    {
86
        $res = [];
87
        if ($this->takePackage()->authors) {
88
            foreach ($this->takePackage()->authors as $nick => $all_data) {
89
                $data = [];
90
                foreach (['name', 'role', 'email', 'homepage'] as $k) {
91
                    if ($all_data[$k]) {
92
                        $data[$k] = $all_data[$k];
93
                    }
94
                }
95
                $res[] = $data;
96
            }
97
        }
98
99
        return $res;
100
    }
101
102
    public function getAutoload()
103
    {
104
        $autoload   = $this->rawItem('autoload');
105
        $psr4       = $autoload['psr-4'] ?: [];
106
        $namespace  = $this->takePackage()->namespace;
107
        if (!array_key_exists($namespace, $psr4)) {
108
            $psr4 = [$namespace . '\\' => $this->takePackage()->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