Completed
Push — master ( c331ed...f9d48b )
by Andrii
02:44
created

InitController::prepareData()   C

Complexity

Conditions 7
Paths 20

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 23
ccs 0
cts 23
cp 0
rs 6.7272
cc 7
eloc 16
nc 20
nop 1
crap 56
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
            $vendorPlugin = "$vendor/hidev-vendor";
34
            try {
35
                $exists = @file_get_contents("https://packagist.org/packages/$vendorPlugin.json");
36
            } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class hidev\controllers\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
37
                $exists = false;
38
            }
39
            if ($exists) {
40
                $this->setItem('vendorRequire', str_pad($vendorPlugin . ':', 23) . ' "*"');
0 ignored issues
show
Documentation introduced by
str_pad($vendorPlugin . ':', 23) . ' "*"' 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...
41
                $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...
42
            }
43
        }
44
        if ($package) {
45
            $this->package = $package;
46
        }
47
        if (!$this->package || !$this->vendor) {
48
            throw new InvalidParamException('Wrong vendor/package given: ' . $name);
49
        }
50
    }
51
52
    public function actionPerform($name = null, $template = '.hidev/config')
53
    {
54
        $this->_template = $template;
55
        $this->prepareData($name);
56
        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...
57
            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...
58
        }
59
60
        return parent::actionPerform();
61
    }
62
63
    public function options($actionId)
64
    {
65
        return array_merge(parent::options($actionId), explode(',', 'namespace,headline,title,type,license,keywords,description,year,nick,author,email,novendor,norequire'));
66
    }
67
68
    public function getType()
69
    {
70
        return $this->getItem('type') ?: 'project';
71
    }
72
73
    public function getTitle()
74
    {
75
        return $this->getItem('title') ?: Helper::titleize($this->package);
76
    }
77
78
    public function getKeywords()
79
    {
80
        return $this->getItem('keywords') ?: implode(', ', explode('-', $this->package));
81
    }
82
83
    /// TODO think of better getting nick
84
    public function getNick()
85
    {
86
        return $this->getItem('nick') ?: preg_replace('/[^a-zA-Z_0-9]+/', '', `id -un`);
87
    }
88
89
    public function getAuthor()
90
    {
91
        return $this->getItem('author') ?: $this->takeVcs()->getUserName();
92
    }
93
94
    public function getEmail()
95
    {
96
        return $this->getItem('email') ?: $this->takeVcs()->getUserEmail();
97
    }
98
}
99