Passed
Push — v3 ( f67e42...ebf6a0 )
by Andrew
13:20 queued 13s
created

MetaTitle::prepForRender()   C

Complexity

Conditions 10
Paths 145

Size

Total Lines 72
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 45
CRAP Score 10.0077

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 72
ccs 45
cts 47
cp 0.9574
rs 6.9242
c 0
b 0
f 0
cc 10
nc 145
nop 1
crap 10.0077

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * SEOmatic plugin for Craft CMS 3.x
4
 *
5
 * A turnkey SEO implementation for Craft CMS that is comprehensive, powerful,
6
 * and flexible
7
 *
8
 * @link      https://nystudio107.com
9
 * @copyright Copyright (c) 2017 nystudio107
10
 */
11
12
namespace nystudio107\seomatic\models;
13
14
use nystudio107\seomatic\base\MetaItem;
15
use nystudio107\seomatic\helpers\MetaValue as MetaValueHelper;
16
use nystudio107\seomatic\helpers\Text as TextHelper;
17
use nystudio107\seomatic\Seomatic;
18
19
use yii\helpers\Html;
20
21
/**
22
 * @author    nystudio107
23
 * @package   Seomatic
24
 * @since     3.0.0
25
 */
26
class MetaTitle extends MetaItem
27
{
28
    // Constants
29
    // =========================================================================
30
31
    const ITEM_TYPE = 'MetaTitle';
32
    const DEFAULT_TITLE_KEY = 'title';
33
34
    // Static Methods
35
    // =========================================================================
36
37
    /**
38
     * @param array $config
39
     *
40
     * @return MetaTitle
41
     */
42 1
    public static function create(array $config = []): MetaTitle
43
    {
44 1
        return new MetaTitle($config);
45
    }
46
47
    // Public Properties
48
    // =========================================================================
49
50
    /**
51
     * @var string
52
     */
53
    public $title;
54
55
    /**
56
     * @var string
57
     */
58
    public $siteName;
59
60
    /**
61
     * @var string
62
     */
63
    public $siteNamePosition;
64
65
    /**
66
     * @var string
67
     */
68
    public $separatorChar;
69
70
    // Public Methods
71
    // =========================================================================
72
73
    /**
74
     * @inheritdoc
75
     */
76 1
    public function init()
77
    {
78 1
        parent::init();
79
80
        // Make sure we have a valid key
81 1
        $this->key = $this->key ?: lcfirst(self::DEFAULT_TITLE_KEY);
82 1
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87
    public function rules()
88
    {
89
        $rules = parent::rules();
90
        $rules = array_merge($rules, [
91
            [['title'], 'required'],
92
            [['title'], 'string', 'length' => [40, Seomatic::$settings->maxTitleLength], 'on' => ['warning']],
93
        ]);
94
95
        return $rules;
96
    }
97
98
    /**
99
     * @inheritdoc
100
     */
101
    public function fields()
102
    {
103
        $fields = parent::fields();
104
        switch ($this->scenario) {
105
            case 'render':
106
                $fields = array_diff_key(
107
                    $fields,
108
                    array_flip([
109
                        'siteName',
110
                        'siteNamePosition',
111
                        'separatorChar',
112
                    ])
113
                );
114
                break;
115
        }
116
117
        return $fields;
118
    }
119
120
    /**
121
     * @inheritdoc
122
     */
123 1
    public function prepForRender(&$data): bool
124
    {
125 1
        $shouldRender = parent::prepForRender($data);
126 1
        if ($shouldRender) {
127
            // handle the site name
128 1
            $separator = MetaValueHelper::parseString($this->separatorChar);
129 1
            $position = MetaValueHelper::parseString($this->siteNamePosition);
130 1
            switch ($position) {
131 1
                case 'before':
132 1
                    $prefix = MetaValueHelper::parseString($this->siteName)
133 1
                        . ' '
134 1
                        . $separator
135 1
                        . ' ';
136 1
                    $suffix = '';
137 1
                    break;
138 1
                case 'after':
139 1
                    $prefix = '';
140
                    $suffix = ' '
141 1
                        . $separator
142 1
                        . ' '
143 1
                        . MetaValueHelper::parseString($this->siteName);
144 1
                    break;
145
                default:
146 1
                    $prefix = '';
147 1
                    $suffix = '';
148 1
                    break;
149
            }
150
            // Handle the case of empty titles
151 1
            if ($prefix === (' ' . $separator . ' ')) {
152 1
                $prefix = '';
153
            }
154 1
            if ($suffix === (' ' . $separator)) {
155
                $suffix = '';
156
            }
157
            // Remove potential double spaces
158 1
            $prefix = preg_replace('/\s+/', ' ', $prefix);
159
            ;
160 1
            $suffix = preg_replace('/\s+/', ' ', $suffix);
161
            ;
162 1
            $lengthAdjust = mb_strlen($prefix . $suffix);
163
            // Parse the data
164 1
            $scenario = $this->scenario;
165 1
            $this->setScenario('render');
166 1
            $data = MetaValueHelper::parseString($data);
167 1
            $this->setScenario($scenario);
168
            // Handle truncating the title
169 1
            $truncLen = Seomatic::$settings->maxTitleLength - $lengthAdjust;
170 1
            if ($truncLen < 0) {
171
                $truncLen = 0;
172
            }
173 1
            if (!empty($data)) {
174 1
                if (Seomatic::$settings->truncateTitleTags) {
175 1
                    $data = TextHelper::truncateOnWord(
176 1
                        $data,
177
                        $truncLen,
178 1
                        '…'
179
                    );
180
                }
181 1
                $data = $prefix . $data . $suffix;
182
            } else {
183
                // If no title is provided, just use the site name
184 1
                $data = MetaValueHelper::parseString($this->siteName);
185
            }
186
            // Trim whitespace
187 1
            $data = trim($data);
188
            // devMode
189 1
            if (Seomatic::$devMode) {
190 1
                $data = Seomatic::$settings->devModeTitlePrefix . $data;
191
            }
192
        }
193
194 1
        return $shouldRender;
195
    }
196
197
    /**
198
     * @inheritdoc
199
     */
200 1
    public function render(array $params = []): string
201
    {
202 1
        $html = '';
203 1
        $title = $this->title;
204 1
        if ($this->prepForRender($title)) {
205 1
            $html = Html::tag('title', $title, []);
206
        }
207
208 1
        return $html;
209
    }
210
211
    /**
212
     * @inheritdoc
213
     */
214
    public function renderAttributes(array $params = []): array
215
    {
216
        $attributes = [];
217
        $title = $this->title;
218
        if ($this->prepForRender($title)) {
219
            $attributes = ['title' => $title];
220
        }
221
222
        return $attributes;
223
    }
224
}
225