Passed
Push — master ( f1705a...4f3945 )
by Evgenii
01:52
created

MetaMaster::getRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace floor12\metamaster;
4
5
use Yii;
6
use yii\base\Component;
7
use yii\web\Request;
8
use yii\web\View;
9
10
/**
11
 * @package floor12\metamaster
12
 * @property string $siteName
13
 * @property string $type
14
 * @property string $title
15
 * @property string $keywords
16
 * @property string $description
17
 * @property string $url
18
 * @property string $defaultImage
19
 * @property string $image
20
 * @property string $imagePath
21
 * @property string $web
22
 * @property View $view
23
 */
24
class MetaMaster extends Component
25
{
26
    /**
27
     * @var string
28
     */
29
    public $siteName = 'My Test Application';
30
    /**
31
     * @var string
32
     */
33
    public $web = "@app/web";
34
    /**
35
     * @var string
36
     */
37
    public $defaultImage;
38
    /**
39
     * @var string
40
     */
41
    public $protocol = 'https';
42
    /**
43
     * @var string
44
     */
45
    private $type = 'article';
46
    /**
47
     * @var Request
48
     */
49
    private $request;
50
    /**
51
     * @var string
52
     */
53
    private $title;
54
    /**
55
     * @var string
56
     */
57
    private $description;
58
    /**
59
     * @var string
60
     */
61
    private $url;
62
    /**
63
     * @var string
64
     */
65
    private $image;
66
    /**
67
     * @var string
68
     */
69
    private $imagePath;
70
    /**
71
     * @var string
72
     */
73
    private $view;
74
75
    /**
76
     * @inheritDoc
77
     */
78
    public function init()
79
    {
80
        if (!$this->request)
81
            $this->request = Yii::$app->request;
0 ignored issues
show
Documentation Bug introduced by
It seems like Yii::app->request can also be of type yii\console\Request. However, the property $request is declared as type yii\web\Request. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
Documentation Bug introduced by
It seems like Yii::app->request can also be of type yii\web\Request. However, the property $request is declared as type yii\console\Request. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
82
        parent::init();
83
    }
84
85
    /** Site name setter
86
     * @param $siteName
87
     * @return $this
88
     */
89
    public function setSiteName(string $siteName)
90
    {
91
        $this->siteName = $siteName;
92
        return $this;
93
    }
94
95
    /** Page title setter
96
     * @param $title
97
     * @return $this
98
     */
99
    public function setTitle(string $title)
100
    {
101
        $this->title = $title;
102
        return $this;
103
    }
104
105
    /** Url setter
106
     * @param $title
107
     * @return $this
108
     */
109
    public function setUrl(string $url)
110
    {
111
        $this->url = $url;
112
        return $this;
113
    }
114
115
    /** Set request object
116
     * @param $title
117
     * @return $this
118
     */
119
    public function setRequest($request)
120
    {
121
        $this->request = $request;
122
        return $this;
123
    }
124
125
126
    /** OgType setter
127
     * @param $type
128
     * @return $this
129
     */
130
    public function setType(string $type)
131
    {
132
        $this->type = $type;
133
        return $this;
134
    }
135
136
    /** Meta description setter
137
     * @param string $description
138
     * @return $this
139
     */
140
    public function setDescription(string $description)
141
    {
142
        $this->description = $description;
143
        return $this;
144
    }
145
146
    /** Meta keyword setter is deprecated from 1.1.0
147
     * @param string $keywords
148
     * @return $this
149
     * @deprecated
150
     */
151
    public function setKeywords(string $keywords)
0 ignored issues
show
Unused Code introduced by
The parameter $keywords 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

151
    public function setKeywords(/** @scrutinizer ignore-unused */ string $keywords)

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...
152
    {
153
        return $this;
154
    }
155
156
    /** Set Open Graph image tag
157
     * @param string $image
158
     * @param string|null $imagePath
159
     * @return $this
160
     */
161
    public function setImage(string $image, string $imagePath = null)
162
    {
163
        $this->image = $image;
164
        $this->imagePath = $imagePath;
165
        return $this;
166
    }
167
168
    /** Register meta tags in View
169
     * @param View $view
170
     */
171
    public function register(View $view)
172
    {
173
        $this->view = $view;
174
        $this->registerCoreInfo();
175
        $this->registerTitle();
176
        $this->registerDescription();
177
        $this->registerImage();
178
    }
179
180
    /**
181
     * Register core meta and og tags
182
     */
183
    private function registerCoreInfo()
184
    {
185
        $this->view->registerMetaTag(['property' => 'og:site_name', 'content' => $this->siteName]);
186
        $this->view->registerMetaTag(['property' => 'og:type', 'content' => $this->type]);
187
        $this->view->registerMetaTag(['property' => 'og:url', 'content' => $this->url ?: $this->getAbsoluteUrl()]);
188
        $this->view->registerMetaTag(['name' => 'twitter:card', 'content' => 'summary']);
189
        $this->view->registerMetaTag(['name' => 'twitter:domain', 'content' => $this->getAbsoluteUrl('')]);
190
        $this->view->registerMetaTag(['name' => 'twitter:site', 'content' => $this->siteName]);
191
        $this->view->registerMetaTag(['name' => 'twitter:site', 'content' => $this->siteName]);
192
        $this->view->registerLinkTag(['rel' => 'canonical', 'href' => $this->url ?: $this->getAbsoluteUrl()]);
193
    }
194
195
    /**
196
     * @param null $absoluteUrl
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $absoluteUrl is correct as it would always require null to be passed?
Loading history...
197
     * @return mixed
198
     */
199
    public function getAbsoluteUrl($absoluteUrl = null)
200
    {
201
202
        if ($absoluteUrl !== null)
0 ignored issues
show
introduced by
The condition $absoluteUrl !== null is always false.
Loading history...
203
            $absoluteUrl = $this->request->getHostInfo() . $absoluteUrl;
204
        else
205
            $absoluteUrl = $this->request->absoluteUrl;
206
        return preg_replace('/https|http/', $this->protocol, $absoluteUrl, -1, $count);
207
    }
208
209
    /**
210
     * Register title
211
     */
212
    private function registerTitle()
213
    {
214
        if ($this->title) {
215
            $this->view->title = $this->title;
216
            $this->view->registerMetaTag(['property' => 'og:title', 'content' => $this->title]);
217
            $this->view->registerMetaTag(['itemprop' => 'name', 'content' => $this->title]);
218
        }
219
    }
220
221
    /**
222
     * Register description
223
     */
224
    private function registerDescription()
225
    {
226
        if ($this->description) {
227
            $this->view->registerMetaTag(['name' => 'description', 'content' => $this->description]);
228
            $this->view->registerMetaTag(['property' => 'og:description', 'content' => $this->description]);
229
            $this->view->registerMetaTag(['name' => 'twitter:description', 'content' => $this->description]);
230
231
        }
232
    }
233
234
    /**
235
     * Register image
236
     */
237
    private function registerImage()
238
    {
239
        $image = $this->image ?: $this->defaultImage;
240
        if ($image) {
241
            $imageUrl = $this->getAbsoluteUrl($image);
242
            $this->view->registerMetaTag(['property' => 'og:image', 'content' => $imageUrl]);
243
            $this->view->registerMetaTag(['property' => 'twitter:image:src', 'content' => $imageUrl]);
244
            $this->view->registerMetaTag(['itemprop' => 'image', 'content' => $imageUrl]);
245
246
        }
247
248
        $path = Yii::getAlias($this->imagePath ?: $this->web . $image);
249
        if ($this->imagePath)
250
            $path = $this->imagePath;
251
        if (file_exists($path)) {
0 ignored issues
show
Bug introduced by
It seems like $path can also be of type boolean; however, parameter $filename of file_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

251
        if (file_exists(/** @scrutinizer ignore-type */ $path)) {
Loading history...
252
            $imageSize = getimagesize($path);
0 ignored issues
show
Bug introduced by
It seems like $path can also be of type boolean; however, parameter $filename of getimagesize() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

252
            $imageSize = getimagesize(/** @scrutinizer ignore-type */ $path);
Loading history...
253
            $this->view->registerMetaTag(['property' => 'og:image:width', 'content' => $imageSize[0]]);
254
            $this->view->registerMetaTag(['property' => 'og:image:height', 'content' => $imageSize[1]]);
255
        }
256
    }
257
258
    public function getRequest(): Request
259
    {
260
        return $this->request;
261
    }
262
}
263