Completed
Push — master ( 4905bd...af6d05 )
by Andrii
03:36
created

AbstractPage::getQuoted()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 17
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 12
nc 4
nop 3
crap 20
1
<?php
2
/**
3
 * Yii2 Pages Module
4
 *
5
 * @link      https://github.com/hiqdev/yii2-module-pages
6
 * @package   yii2-module-pages
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\yii2\modules\pages\models;
12
13
use Symfony\Component\Yaml\Yaml;
14
use Yii;
15
16
abstract class AbstractPage extends \yii\base\Object
17
{
18
    public $layout;
19
20
    public $title;
21
22
    protected $path;
23
24
    protected $text;
25
26
    protected $data = [];
27
28
    public function setData($data)
29
    {
30
        if (!is_array($data)) {
31
            return;
32
        }
33
        $this->data = $data;
34
        foreach (['title', 'layout'] as $key) {
35
            if (isset($data[$key])) {
36
                $this->{$key} = $data[$key];
37
            }
38
        }
39
    }
40
41
    public function getData()
42
    {
43
        return $this->data;
44
    }
45
46
    public function __construct($path)
47
    {
48
        list($data, $text) = $this->extractData($path);
49
50
        $this->path = $path;
51
        $this->text = $text;
52
        $this->setData($data);
53
    }
54
55
    public function getPath()
56
    {
57
        return $this->path;
58
    }
59
60
    public function getDate()
61
    {
62
        return $this->data['date'];
63
    }
64
65
    public static function getModule()
66
    {
67
        /// XXX think
68
        return Yii::$app->getModule('pages');
69
    }
70
71
    public static function createFromFile($path)
72
    {
73
        $extension = pathinfo($path)['extension'];
74
        $class = static::getModule()->findPageClass($extension);
75
76
        return new $class($path);
77
    }
78
79 View Code Duplication
    public function extractData($path)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
    {
81
        $lines = static::getModule()->readArray($path);
82
        $yaml = $this->getQuoted($lines, '/^---$/', '/^---$/');
83
        if (empty($yaml)) {
84
            $data = [];
85
            $text = $lines;
86
        } else {
87
            $data = $this->readYaml($yaml);
88
            $text = array_slice($lines, count($yaml));
89
        }
90
91
        return [$data, implode("\n", $text)];
92
    }
93
94
    public function readFrontMatter($lines)
95
    {
96
        $yaml = $this->getQuoted($lines, '/^---$/', '/^---$/');
97
        if (empty($yaml)) {
98
            return [];
99
        }
100
101
        return empty($yaml) ? [] : $this->readYaml($yaml);
102
    }
103
104
    public function readYaml($lines)
105
    {
106
        $data = Yaml::parse(implode("\n", $lines));
107
        if (is_int($data['date'])) {
108
            $data['date'] = date('c', $data['date']);
109
        }
110
111
        return $data;
112
    }
113
114
    public function getQuoted($lines, $headMarker, $tailMarker)
115
    {
116
        $line = array_shift($lines);
117
        if (!preg_match($headMarker, $line, $matches)) {
118
            return null;
119
        }
120
        $res[] = ltrim(substr($line, strlen($matches[0])));
0 ignored issues
show
Coding Style Comprehensibility introduced by
$res was never initialized. Although not strictly required by PHP, it is generally a good practice to add $res = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
121
        while ($line) {
122
            $line = array_shift($lines);
123
            if (preg_match($tailMarker, $line, $matches)) {
124
                $res[] = rtrim(substr($line, 0, -strlen($matches[0])));
125
                break;
126
            }
127
            $res[] = $line;
128
        }
129
130
        return $res;
131
    }
132
    /**
133
     * Renders the page with given params.
134
     *
135
     * @param array $params
136
     * @abstract
137
     */
138
    abstract public function render(array $params = []);
139
}
140