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 | 9 | public function init() |
|||
79 | { |
||||
80 | 9 | if (!$this->request) |
|||
81 | 9 | $this->request = Yii::$app->request; |
|||
0 ignored issues
–
show
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 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;
}
![]() |
|||||
82 | 9 | 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 | 1 | public function setTitle(string $title) |
|||
100 | { |
||||
101 | 1 | $this->title = $title; |
|||
102 | 1 | 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 | 9 | public function setRequest($request) |
|||
120 | { |
||||
121 | 9 | $this->request = $request; |
|||
122 | 9 | 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 | 1 | public function setDescription(string $description) |
|||
141 | { |
||||
142 | 1 | $this->description = $description; |
|||
143 | 1 | 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
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
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
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 | 1 | public function setImage(string $image, string $imagePath = null) |
|||
162 | { |
||||
163 | 1 | $this->image = $image; |
|||
164 | 1 | $this->imagePath = $imagePath; |
|||
165 | 1 | return $this; |
|||
166 | } |
||||
167 | |||||
168 | /** Register meta tags in View |
||||
169 | * @param View $view |
||||
170 | */ |
||||
171 | 5 | public function register(View $view) |
|||
172 | { |
||||
173 | 5 | $this->view = $view; |
|||
174 | 5 | $this->registerCoreInfo(); |
|||
175 | 5 | $this->registerTitle(); |
|||
176 | 5 | $this->registerDescription(); |
|||
177 | 5 | $this->registerImage(); |
|||
178 | } |
||||
179 | |||||
180 | /** |
||||
181 | * Register core meta and og tags |
||||
182 | */ |
||||
183 | 5 | private function registerCoreInfo() |
|||
184 | { |
||||
185 | 5 | $this->registerOrUpdateMetaTag(['property' => 'og:site_name', 'content' => $this->siteName]); |
|||
186 | 5 | $this->registerOrUpdateMetaTag(['property' => 'og:type', 'content' => $this->type]); |
|||
187 | 5 | $this->registerOrUpdateMetaTag(['property' => 'og:url', 'content' => $this->url ?: $this->getAbsoluteUrl()]); |
|||
188 | 5 | $this->registerOrUpdateMetaTag(['name' => 'twitter:card', 'content' => 'summary']); |
|||
189 | 5 | $this->registerOrUpdateMetaTag(['name' => 'twitter:domain', 'content' => $this->getAbsoluteUrl('')]); |
|||
190 | 5 | $this->registerOrUpdateMetaTag(['name' => 'twitter:site', 'content' => $this->siteName]); |
|||
191 | 5 | $this->registerOrUpdateLinkTag(['rel' => 'canonical', 'href' => $this->url ?: $this->getAbsoluteUrl()]); |
|||
192 | } |
||||
193 | |||||
194 | private function registerOrUpdateMetaTag($tag) |
||||
195 | { |
||||
196 | $existingTags = $this->view->metaTags; |
||||
197 | $tagKey = $this->generateTagKey($tag); |
||||
198 | 9 | ||||
199 | if (array_key_exists($tagKey, $existingTags)) { |
||||
200 | 9 | unset($this->view->metaTags[$tagKey]); |
|||
201 | 5 | } |
|||
202 | |||||
203 | $this->view->registerMetaTag($tag, $tagKey); |
||||
204 | 9 | } |
|||
205 | 9 | ||||
206 | private function registerOrUpdateLinkTag($tag) |
||||
207 | { |
||||
208 | 9 | $existingTags = $this->view->linkTags; |
|||
209 | $tagKey = $tag['rel']; |
||||
210 | |||||
211 | if (array_key_exists($tagKey, $existingTags)) { |
||||
212 | unset($this->view->linkTags[$tagKey]); |
||||
213 | } |
||||
214 | 5 | ||||
215 | $this->view->registerLinkTag($tag, $tagKey); |
||||
216 | 5 | } |
|||
217 | 1 | ||||
218 | 1 | private function generateTagKey($tag) |
|||
219 | 1 | { |
|||
220 | return md5(json_encode($tag)); |
||||
221 | } |
||||
222 | |||||
223 | /** |
||||
224 | * @param null $absoluteUrl |
||||
0 ignored issues
–
show
|
|||||
225 | * @return mixed |
||||
226 | 5 | */ |
|||
227 | public function getAbsoluteUrl($absoluteUrl = null) |
||||
228 | 5 | { |
|||
229 | 1 | if ($absoluteUrl === null) { |
|||
0 ignored issues
–
show
|
|||||
230 | 1 | $absoluteUrl = $this->request->absoluteUrl; |
|||
231 | 1 | } |
|||
232 | |||||
233 | if (substr($absoluteUrl, 0, 4) !== 'http') { |
||||
234 | $absoluteUrl = $this->request->getHostInfo() . $absoluteUrl; |
||||
235 | } |
||||
236 | |||||
237 | return preg_replace('/https|http/', $this->protocol, $absoluteUrl, -1, $count); |
||||
238 | } |
||||
239 | 5 | ||||
240 | /** |
||||
241 | 5 | * Register title |
|||
242 | 5 | */ |
|||
243 | 5 | private function registerTitle() |
|||
244 | 5 | { |
|||
245 | 5 | if ($this->title) { |
|||
246 | 5 | $this->view->title = $this->title; |
|||
247 | $this->registerOrUpdateMetaTag(['property' => 'og:title', 'content' => $this->title]); |
||||
248 | $this->registerOrUpdateMetaTag(['itemprop' => 'name', 'content' => $this->title]); |
||||
249 | } |
||||
250 | 5 | } |
|||
251 | 5 | ||||
252 | 1 | /** |
|||
253 | * Register description |
||||
254 | 5 | */ |
|||
255 | 1 | private function registerDescription() |
|||
256 | 1 | { |
|||
257 | 1 | if ($this->description) { |
|||
258 | $this->registerOrUpdateMetaTag(['name' => 'description', 'content' => $this->description]); |
||||
259 | $this->registerOrUpdateMetaTag(['property' => 'og:description', 'content' => $this->description]); |
||||
260 | $this->registerOrUpdateMetaTag(['name' => 'twitter:description', 'content' => $this->description]); |
||||
261 | 6 | ||||
262 | } |
||||
263 | 6 | } |
|||
264 | |||||
265 | |||||
266 | /** |
||||
267 | * Register image |
||||
268 | */ |
||||
269 | private function registerImage() |
||||
270 | { |
||||
271 | $image = $this->image ?: $this->defaultImage; |
||||
272 | if ($image) { |
||||
273 | $imageUrl = $this->getAbsoluteUrl($image); |
||||
274 | $this->registerOrUpdateMetaTag(['property' => 'og:image', 'content' => $imageUrl]); |
||||
275 | $this->registerOrUpdateMetaTag(['property' => 'twitter:image:src', 'content' => $imageUrl]); |
||||
276 | $this->registerOrUpdateMetaTag(['itemprop' => 'image', 'content' => $imageUrl]); |
||||
277 | |||||
278 | } |
||||
279 | |||||
280 | $path = Yii::getAlias($this->imagePath ?: $this->web . $image); |
||||
281 | if ($this->imagePath) { |
||||
282 | $path = $this->imagePath; |
||||
283 | } |
||||
284 | if (file_exists($path)) { |
||||
0 ignored issues
–
show
It seems like
$path can also be of type false ; 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
![]() |
|||||
285 | $imageSize = getimagesize($path); |
||||
0 ignored issues
–
show
It seems like
$path can also be of type false ; 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
![]() |
|||||
286 | $this->registerOrUpdateMetaTag(['property' => 'og:image:width', 'content' => $imageSize[0]]); |
||||
287 | $this->registerOrUpdateMetaTag(['property' => 'og:image:height', 'content' => $imageSize[1]]); |
||||
288 | } |
||||
289 | } |
||||
290 | |||||
291 | public function getRequest(): Request |
||||
292 | { |
||||
293 | return $this->request; |
||||
294 | } |
||||
295 | } |
||||
296 |
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 theid
property of an instance of theAccount
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.