Completed
Push — master ( e30981...c331ed )
by Andrii
11:15
created

InitController::prepareData()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 0
cts 16
cp 0
rs 8.8571
cc 6
eloc 11
nc 12
nop 1
crap 42
1
<?php
2
3
/*
4
 * Task runner, code generator and build tool for easier continuos integration
5
 *
6
 * @link      https://github.com/hiqdev/hidev
7
 * @package   hidev
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\controllers;
13
14
use hidev\helpers\Helper;
15
use yii\base\InvalidParamException;
16
17
/**
18
 * Init controller.
19
 * Builds .hidev/config.yml by template and params.
20
 */
21
class InitController extends TemplateController
22
{
23
    protected $_file = '.hidev/config.yml';
24
25
    public $vendor;
26
    public $package;
27
28
    public function prepareData($name)
29
    {
30
        list($vendor, $package) = explode('/', $name, 2);
31
        if ($vendor) {
32
            $this->vendor = $vendor;
33
            if (GithubController::exists($name)) {
34
                $this->setItem('vendorRequire', str_pad($name . ':', 16));
0 ignored issues
show
Documentation introduced by
str_pad($name . ':', 16) is of type string, 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...
35
                $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...
36
            }
37
        }
38
        if ($package) {
39
            $this->package = $package;
40
        }
41
        if (!$this->package || !$this->vendor) {
42
            throw new InvalidParamException('Wrong vendor/package given: ' . $name);
43
        }
44
    }
45
46
    public function actionPerform($name = null, $template = '.hidev/config')
47
    {
48
        $this->_template = $template;
49
        $this->prepareData($name);
50
        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...
51
            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...
52
        }
53
54
        return parent::actionPerform();
55
    }
56
57
    public function options($actionId)
58
    {
59
        return array_merge(parent::options($actionId), explode(',', 'namespace,headline,title,type,license,keywords,description,year,nick,author,email,novendor,norequire'));
60
    }
61
62
    public function getType()
63
    {
64
        return $this->getItem('type') ?: 'project';
65
    }
66
67
    public function getTitle()
68
    {
69
        return $this->getItem('title') ?: Helper::titleize($this->package);
70
    }
71
72
    public function getKeywords()
73
    {
74
        return $this->getItem('keywords') ?: implode(', ', explode('-', $this->package));
75
    }
76
77
    /// TODO think of better getting nick
78
    public function getNick()
79
    {
80
        return $this->getItem('nick') ?: preg_replace('/[^a-zA-Z_0-9]+/', '', `id -un`);
81
    }
82
83
    public function getAuthor()
84
    {
85
        return $this->getItem('author') ?: $this->takeVcs()->getUserName();
86
    }
87
88
    public function getEmail()
89
    {
90
        return $this->getItem('email') ?: $this->takeVcs()->getUserEmail();
91
    }
92
}
93