Completed
Push — master ( a9ea1a...62657b )
by Andrii
05:05
created

InitController::getNick()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
crap 6
1
<?php
2
3
/*
4
 * Automation tool mixed with code generator for easier continuous development
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 Exception;
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 $vendor;
27
    public $package;
28
29 3
    public function prepareData($name)
30
    {
31
        list($vendor, $package) = explode('/', $name, 2);
32 3
        if ($vendor) {
33 3
            $this->vendor = $vendor;
34 3
            $vendorPlugin = "$vendor/hidev-$vendor";
35
            try {
36
                $exists = @file_get_contents("https://packagist.org/packages/$vendorPlugin.json");
37
            } catch (Exception $e) {
38
                $exists = false;
39 3
            }
40 3
            if ($exists) {
41
                $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...
42
                $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...
43
            }
44
        }
45 3
        if ($package) {
46 3
            $this->package = $package;
47
        }
48 3
        if (!$this->package || !$this->vendor) {
49
            throw new InvalidParamException('Wrong vendor/package given: ' . $name);
50
        }
51 3
    }
52
53 3
    public function actionPerform($name = null, $template = '.hidev/config')
54
    {
55 3
        $this->_template = $template;
56
        $this->prepareData($name);
57
        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...
58
            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...
59
        }
60
61
        return parent::actionPerform();
62
    }
63
64
    public function options($actionId)
65
    {
66
        return array_merge(parent::options($actionId), explode(',', 'namespace,headline,title,type,license,keywords,description,year,nick,author,email,novendor,norequire'));
67
    }
68
69
    public function getType()
70
    {
71
        return $this->getItem('type') ?: 'project';
72
    }
73
74
    public function getTitle()
75
    {
76
        return $this->getItem('title') ?: Helper::titleize($this->package);
77
    }
78
79
    public function getKeywords()
80
    {
81
        return $this->getItem('keywords') ?: implode(', ', explode('-', $this->package));
82
    }
83
84
    /// TODO think of better getting nick
85
    public function getNick()
86
    {
87
        return $this->getItem('nick') ?: preg_replace('/[^a-zA-Z_0-9]+/', '', `id -un`);
88
    }
89
90
    public function getAuthor()
91
    {
92
        return $this->getItem('author') ?: $this->takeVcs()->getUserName();
93
    }
94
95
    public function getEmail()
96
    {
97
        return $this->getItem('email') ?: $this->takeVcs()->getUserEmail();
98
    }
99
}
100