Completed
Push — master ( 3204cd...9afd5c )
by Andrii
02:53
created

InitController::actionComposer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1.0723

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 12
cp 0.5833
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
crap 1.0723
1
<?php
2
/**
3
 * Automation tool mixed with code generator for easier continuous development
4
 *
5
 * @link      https://github.com/hiqdev/hidev
6
 * @package   hidev
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\controllers;
12
13
use Exception;
14
use hidev\base\File;
15
use hidev\helpers\Helper;
16
use yii\base\InvalidParamException;
17
18
/**
19
 * Init controller.
20
 * Builds .hidev/config.yml by template and params.
21
 */
22
class InitController extends TemplateController
23
{
24
    protected $_file = '.hidev/config.yml';
25
26
    public $name;
27
    public $vendor;
28
    public $package;
29
30 3
    public function prepareData($name)
31
    {
32 3
        $this->name = $name;
33
        list($vendor, $package) = explode('/', $name, 2);
34 3
        if ($vendor) {
35 3
            $this->vendor = $vendor;
36 3
            $vendorPlugin = "$vendor/hidev-$vendor";
37
            try {
38
                $exists = @file_get_contents("https://packagist.org/packages/$vendorPlugin.json");
39
            } catch (Exception $e) {
40
                $exists = false;
41 3
            }
42 3
            if ($exists) {
43
                $this->setItem('vendorRequire', $vendorPlugin);
44
                $this->setItem('novendor', true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
45
            }
46
        }
47 3
        if ($package) {
48 3
            $this->package = $package;
49
        }
50 3
        if (!$this->package || !$this->vendor) {
51
            throw new InvalidParamException('Wrong vendor/package given: ' . $name);
52
        }
53 3
    }
54
55 3
    public function actionPerform($name = null, $template = '.hidev/config')
56
    {
57 3
        $this->_template = $template;
58
        $this->prepareData($name);
59
        if (!file_exists($this->dirname)) {
0 ignored issues
show
Documentation introduced by
The property dirname does not exist on object<hidev\controllers\InitController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
60
            mkdir($this->dirname);
0 ignored issues
show
Documentation introduced by
The property dirname does not exist on object<hidev\controllers\InitController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
61
        }
62
        if (!$this->nocomposer) {
0 ignored issues
show
Documentation introduced by
The property nocomposer does not exist on object<hidev\controllers\InitController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
63
            $this->actionComposer();
64
        }
65
66
        return parent::actionPerform();
67
    }
68
69 2
    public function actionComposer()
70
    {
71
        $file = new File(['path' => 'composer.json']);
72
        $data = array_filter([
73 2
            'name'        => $this->name,
74 2
            'type'        => $this->getType(),
75 2
            'description' => $this->getTitle(),
76
            'keywords'    => preg_split('/\s*,\s*/', $this->getKeywords()),
77 2
            'require-dev' => $this->getPlugins(),
78 2
            'license'     => $this->getItem('license'),
79
        ]);
80
        $file->save($data);
81
        $this->setItem('norequire', true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82 2
    }
83
84
    public function options($actionId)
85
    {
86
        return array_merge(parent::options($actionId), explode(',', 'namespace,headline,title,type,license,keywords,description,year,nick,author,email,novendor,norequire,nocomposer'));
87
    }
88
89
    public function getType()
90
    {
91
        return $this->getItem('type') ?: 'project';
92
    }
93
94
    public function getTitle()
95
    {
96
        return $this->getItem('title') ?: Helper::titleize($this->package);
97
    }
98
99
    public function getKeywords()
100
    {
101
        return $this->getItem('keywords') ?: implode(', ', explode('-', $this->package));
102
    }
103
104
    /// TODO think of better getting nick
105
    public function getNick()
106
    {
107
        return $this->getItem('nick') ?: preg_replace('/[^a-zA-Z_0-9]+/', '', `id -un`);
108
    }
109
110
    public function getAuthor()
111
    {
112
        return $this->getItem('author') ?: $this->takeVcs()->getUserName();
113
    }
114
115
    public function getEmail()
116
    {
117
        return $this->getItem('email') ?: $this->takeVcs()->getUserEmail();
118
    }
119
120
    /**
121
     * Returns list of plugins in composer requirements format: name => version.
122
     * @return array
123
     */
124 2
    public function getPlugins()
125
    {
126
        if ($this->norequire) {
0 ignored issues
show
Documentation introduced by
The property norequire does not exist on object<hidev\controllers\InitController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
127
            return [];
128
        }
129
        $res = [
130
            'hiqdev/hidev-php' => '<2.0 || dev-master',
131 2
        ];
132
        if ($this->vendorRequire) {
0 ignored issues
show
Bug introduced by
The property vendorRequire does not seem to exist. Did you mean vendor?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
133
            $res[$this->vendorRequire] = '<2.0 || dev-master';
0 ignored issues
show
Bug introduced by
The property vendorRequire does not seem to exist. Did you mean vendor?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
134
        }
135
136 2
        return $res;
137
    }
138
}
139