Completed
Push — master ( e0cd62...080a96 )
by Andrii
03:16
created

DirectoryController::actionSave()   C

Complexity

Conditions 8
Paths 10

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 22
ccs 0
cts 22
cp 0
rs 6.6037
cc 8
eloc 14
nc 10
nop 0
crap 72
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 Yii;
15
16
/**
17
 * Directory controller.
18
 */
19
class DirectoryController extends FileController
20
{
21
    public $recursive = [];
22
    /**
23
     * Load action.
24
     * @return void
25
     */
26
    public function actionLoad()
27
    {
28
        /// nothing for the moment
29
    }
30
31
    public function hasPath()
32
    {
33
        return $this->_file || $this->_path;
34
    }
35
36
    public function actionSave()
37
    {
38
        if (!file_exists($this->path)) {
0 ignored issues
show
Documentation introduced by
The property path does not exist on object<hidev\controllers\DirectoryController>. 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...
39
            $this->mkdir($this->path);
0 ignored issues
show
Documentation introduced by
The property path does not exist on object<hidev\controllers\DirectoryController>. 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...
40
        }
41
        foreach ($this->getItems() as $id => $config) {
42
            $defaults = [
43
                'class' => isset($config['template']) ? 'template' : 'directory',
44
                'file'  => $this->path . '/' . $id,
0 ignored issues
show
Documentation introduced by
The property path does not exist on object<hidev\controllers\DirectoryController>. 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
            ];
46
            if ($this->recursive) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->recursive of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
47
                $defaults['recursive'] = $this->recursive;
48
                foreach ((array) $this->recursive as $key) {
49
                    if ($this->{$key}) {
50
                        $defaults[$key] = $this->{$key};
51
                    }
52
                }
53
            }
54
            $goal = $this->takeConfig()->createItem($id, array_merge($defaults, $config ?: []));
55
            $goal->perform();
56
        }
57
    }
58
59
    public function mkdir($path)
60
    {
61
        Yii::warning('mkdir ' . $path, 'dir');
62
        mkdir($path);
63
    }
64
}
65