Completed
Push — master ( b4a6ad...e2bab1 )
by Andrii
13:50
created

InitController::actionPerform()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 8.8571
cc 6
eloc 12
nc 12
nop 2
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) 2014-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
    use \hiqdev\yii2\collection\ObjectTrait;
24
25
    protected $_file = '.hidev/config.yml';
26
27
    public $vendor;
28
    public $package;
29
30
    public function actionPerform($name = null, $template = '.hidev/config')
31
    {
32
        list($vendor, $package) = explode('/', $name, 2);
33
        if ($vendor) {
34
            $this->vendor = $vendor;
35
        }
36
        if ($package) {
37
            $this->package = $package;
38
        }
39
        if (!$this->package || !$this->vendor) {
40
            throw new InvalidParamException('Wrong vendor/package given: ' . $name);
41
        }
42
        $this->template = $template;
0 ignored issues
show
Bug introduced by
The property template does not seem to exist. Did you mean _template?

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...
43
44
        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...
45
            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...
46
        }
47
48
        return parent::actionPerform();
49
    }
50
51
    public function options($actionId)
52
    {
53
        return array_merge(parent::options($actionId), explode(',', 'namespace,headline,title,type,license,keywords,description,year,nick,author,email,novendor,norequire'));
54
    }
55
56
    public function getType()
57
    {
58
        return $this->getItem('type') ?: 'project';
59
    }
60
61
    public function getTitle()
62
    {
63
        return $this->getItem('title') ?: Helper::titleize($this->package);
64
    }
65
66
    public function getKeywords()
67
    {
68
        return $this->getItem('keywords') ?: implode(', ', explode('-', $this->package));
69
    }
70
71
    /// TODO think of better getting nick
72
    public function getNick()
73
    {
74
        return $this->getItem('nick') ?: preg_replace('/[^a-zA-Z_0-9]+/', '', `id -un`);
75
    }
76
77
    public function getAuthor()
78
    {
79
        return $this->getItem('author') ?: $this->getVcs()->getUserName();
0 ignored issues
show
Documentation Bug introduced by
The method getVcs does not exist on object<hidev\controllers\InitController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
80
    }
81
82
    public function getEmail()
83
    {
84
        return $this->getItem('email') ?: $this->getVcs()->getUserEmail();
0 ignored issues
show
Documentation Bug introduced by
The method getVcs does not exist on object<hidev\controllers\InitController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
85
    }
86
}
87