1 | <?php |
||||
2 | /** |
||||
3 | * SEOmatic plugin for Craft CMS |
||||
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 Craft; |
||||
15 | use nystudio107\codeeditor\validators\TwigTemplateValidator; |
||||
16 | use nystudio107\seomatic\base\FrontendTemplate; |
||||
17 | use nystudio107\seomatic\helpers\PluginTemplate as PluginTemplateHelper; |
||||
18 | |||||
19 | /** |
||||
20 | * @author nystudio107 |
||||
21 | * @package Seomatic |
||||
22 | * @since 3.0.0 |
||||
23 | */ |
||||
24 | class EditableTemplate extends FrontendTemplate |
||||
25 | { |
||||
26 | // Constants |
||||
27 | // ========================================================================= |
||||
28 | |||||
29 | public const TEMPLATE_TYPE = 'EditableTemplate'; |
||||
30 | |||||
31 | // Static Methods |
||||
32 | // ========================================================================= |
||||
33 | /** |
||||
34 | * @var string |
||||
35 | * @deprecated This is no longer used |
||||
36 | */ |
||||
37 | public $templateVersion = '1.0.0'; |
||||
38 | |||||
39 | |||||
40 | // Public Properties |
||||
41 | // ========================================================================= |
||||
42 | /** |
||||
43 | * The template to render this FrontendTemplate |
||||
44 | * |
||||
45 | * @var string |
||||
46 | */ |
||||
47 | public $templateString; |
||||
48 | /** |
||||
49 | * @var int |
||||
50 | */ |
||||
51 | public $siteId; |
||||
52 | |||||
53 | /** |
||||
54 | * @param array $config |
||||
55 | * |
||||
56 | * @return null|EditableTemplate |
||||
57 | */ |
||||
58 | public static function create(array $config = []) |
||||
59 | { |
||||
60 | $model = new EditableTemplate($config); |
||||
61 | // Load $templateString from the source template if it's not set |
||||
62 | if (empty($model->templateString)) { |
||||
63 | $model->loadTemplate(); |
||||
64 | } |
||||
65 | |||||
66 | return $model; |
||||
67 | } |
||||
68 | |||||
69 | // Public Methods |
||||
70 | // ========================================================================= |
||||
71 | |||||
72 | /** |
||||
73 | * Load the existing template into a string |
||||
74 | */ |
||||
75 | public function loadTemplate() |
||||
76 | { |
||||
77 | $this->templateString = ''; |
||||
78 | // First try it from the Craft template directory |
||||
79 | $path = Craft::getAlias('@templates/') |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
80 | . $this->template; |
||||
81 | if (file_exists($path)) { |
||||
82 | $this->templateString = @file_get_contents($path); |
||||
0 ignored issues
–
show
It seems like
@file_get_contents($path) can also be of type false . However, the property $templateString is declared as type string . 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;
}
![]() |
|||||
83 | } else { |
||||
84 | // Next try from our plugin directory first |
||||
85 | $path = Craft::getAlias('@nystudio107/seomatic/templates/') |
||||
0 ignored issues
–
show
Are you sure
Craft::getAlias('@nystud...7/seomatic/templates/') of type false|string can be used in concatenation ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
86 | . $this->template; |
||||
87 | if (file_exists($path)) { |
||||
88 | $this->templateString = @file_get_contents($path); |
||||
89 | } |
||||
90 | } |
||||
91 | } |
||||
92 | |||||
93 | /** |
||||
94 | * @inheritdoc |
||||
95 | */ |
||||
96 | public function rules(): array |
||||
97 | { |
||||
98 | $rules = parent::rules(); |
||||
99 | $rules = array_merge($rules, [ |
||||
100 | [['templateString'], 'required'], |
||||
101 | [['templateString'], 'string'], |
||||
102 | [['templateString'], TwigTemplateValidator::class], |
||||
103 | ]); |
||||
104 | |||||
105 | return $rules; |
||||
106 | } |
||||
107 | |||||
108 | /** |
||||
109 | * @inheritdoc |
||||
110 | */ |
||||
111 | public function render(array $params = []): string |
||||
112 | { |
||||
113 | return PluginTemplateHelper::renderStringTemplate($this->templateString); |
||||
114 | } |
||||
115 | } |
||||
116 |