BaseHandler::renderPrepared()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Automation tool mixed with code generator for easier continuous development
4
 *
5
 * @link      https://github.com/hiqdev/hidev
6
 * @package   hidev
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\handlers;
12
13
use hidev\helpers\FileHelper;
14
use Yii;
15
use yii\base\InvalidConfigException;
16
use yii\helpers\ArrayHelper;
17
18
/**
19
 * Base Handler.
20
 * Knows how to parse and render it's file type.
21
 */
22
class BaseHandler extends \yii\base\BaseObject
23
{
24
    /**
25
     * @var Goal
0 ignored issues
show
Bug introduced by
The type hidev\handlers\Goal was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
     */
27
    public $goal;
28
29
    /**
30
     * @var string template file name to be used for rendering
31
     */
32
    public $template;
33
34
    /**
35
     * @var View object
0 ignored issues
show
Bug introduced by
The type hidev\handlers\View was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
     */
37
    protected $_view;
38
39
    /**
40
     * Returns the view object that can be used to render views or view files.
41
     * If not set, it will default to the "view" application component.
42
     * @return View the view object that can be used to render views or view files
43
     */
44
    public function getView()
45
    {
46
        if ($this->_view === null) {
47
            $this->_view = Yii::$app->getView();
0 ignored issues
show
Documentation Bug introduced by
It seems like Yii::app->getView() of type yii\base\View or yii\web\View is incompatible with the declared type hidev\handlers\View of property $_view.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
48
        }
49
50
        return $this->_view;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->_view also could return the type yii\base\View|yii\web\View which is incompatible with the documented return type hidev\handlers\View.
Loading history...
51
    }
52
53
    /**
54
     * Renders prepared data.
55
     * Must be redefined in child.
56
     * @param array $data
57
     * @throws InvalidConfigException
58
     * @return string file content
59
     */
60
    public function renderPrepared(array $data)
61
    {
62
        throw new InvalidConfigException('Render not available');
63
    }
64
65
    /**
66
     * Renders raw data.
67
     * Prepares data with ArrayHelper::toArray.
68
     * @param mixed $data
69
     * @return string file content
70
     */
71
    public function render($data)
72
    {
73
        return $this->renderPrepared($this->prepareData($data));
74
    }
75
76
    public function prepareData($data)
77
    {
78
        return ArrayHelper::toArray($data, [], false);
79
        //return $data;
80
    }
81
82
    /**
83
     * Renders file content using given data.
84
     * Converts to array with ArrayHelper::toArray.
85
     * @param mixed $data
86
     * @return string file content
87
     */
88 3
    public function renderPath($path, $data)
89
    {
90 3
        return $this->write($path, $this->render($data));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->write($path, $this->render($data)) returns the type boolean which is incompatible with the documented return type string.
Loading history...
91
    }
92
93
    public function parsePath($path, $minimal = null)
0 ignored issues
show
Unused Code introduced by
The parameter $minimal is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

93
    public function parsePath($path, /** @scrutinizer ignore-unused */ $minimal = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
94
    {
95
        return $this->parse($this->read($path));
96
    }
97
98
    /**
99
     * Parses string input. To be redefined in real handlers.
100
     * @param string $input to parse
101
     * @return array
102
     */
103
    public function parse($input)
0 ignored issues
show
Unused Code introduced by
The parameter $input is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

103
    public function parse(/** @scrutinizer ignore-unused */ $input)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
104
    {
105
        return [];
106
    }
107
108
    /**
109
     * Writes given content to the file.
110
     * Doesn't touch file if it has exactly same content.
111
     * Creates intermediate directories when necessary.
112
     * @param string $path
113
     * @param string $content
114
     * @return bool true if file was changed
115
     */
116 3
    public function write($path, $content)
117
    {
118 3
        return FileHelper::write($path, $content);
119
    }
120
121
    /**
122
     * Creates directory if not exists.
123
     * @param string $path
124
     * @return bool true if directory did not exist and was created
125
     */
126
    public function mkdir($path)
127
    {
128
        return FileHelper::mkdir($path);
129
    }
130
131
    /**
132
     * Read file into a string or array.
133
     * @param string $path
134
     * @param bool $asArray
135
     * @return string|array
136
     */
137
    public function read($path, $asArray = false)
138
    {
139
        if (file_exists($path)) {
140
            Yii::info('Read file: ' . $path, 'file');
141
142
            return $asArray ? file($path) : file_get_contents($path);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $asArray ? file($...ile_get_contents($path) could also return false which is incompatible with the documented return type array|string. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
143
        } else {
144
            Yii::warning('Couldn\'t read file: ' . $path, 'file');
145
146
            return;
147
        }
148
    }
149
150
    /**
151
     * Read file into array of strings.
152
     * @param string $path
153
     * @return array
154
     */
155
    public function readArray($path)
156
    {
157
        return $this->read($path, true);
158
    }
159
}
160