This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace SocialLinks\Metas; |
||
4 | |||
5 | use SocialLinks\Page; |
||
6 | use ArrayObject; |
||
7 | |||
8 | /** |
||
9 | * Base class extended by all metas. |
||
10 | */ |
||
11 | abstract class MetaBase extends ArrayObject |
||
12 | { |
||
13 | const META_ATTRIBUTE_NAME = 'name'; |
||
14 | const META_NAME_PREFIX = ''; |
||
15 | |||
16 | protected $page; |
||
17 | protected static $characterLimits = array(); |
||
18 | |||
19 | /** |
||
20 | * Constructor. |
||
21 | * |
||
22 | * @param Page $page |
||
23 | */ |
||
24 | public function __construct(Page $page) |
||
25 | { |
||
26 | $this->page = $page; |
||
27 | $this->generateTags(); |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * Convert all tags to html |
||
32 | * |
||
33 | * @return string |
||
34 | */ |
||
35 | public function __toString() |
||
36 | { |
||
37 | $html = []; |
||
38 | |||
39 | foreach ($this as $tag) { |
||
40 | if (is_array($tag)) { |
||
41 | $html = array_merge($html, $tag); |
||
42 | } else { |
||
43 | $html[] = $tag; |
||
44 | } |
||
45 | } |
||
46 | |||
47 | return implode("\n", $html); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Generate all tags. |
||
52 | * |
||
53 | * @return array |
||
54 | */ |
||
55 | abstract protected function generateTags(); |
||
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | View Code Duplication | public function addMeta($name, $content) |
|
0 ignored issues
–
show
|
|||
61 | { |
||
62 | if (is_array($content)) { |
||
63 | $content = array_map(function ($content) use ($name) { |
||
64 | return static::getHtmlMeta($name, $content); |
||
65 | }, array_filter($content)); |
||
66 | |||
67 | if (empty($content)) { |
||
68 | return; |
||
69 | } |
||
70 | } elseif (!empty($content)) { |
||
71 | $content = static::getHtmlMeta($name, $content); |
||
72 | } else { |
||
73 | return; |
||
74 | } |
||
75 | |||
76 | $this[$name] = $content; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | */ |
||
82 | View Code Duplication | public function addLink($rel, $href) |
|
0 ignored issues
–
show
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. ![]() |
|||
83 | { |
||
84 | if (is_array($href)) { |
||
85 | $href = array_map(function ($href) use ($rel) { |
||
86 | return static::getHtmlLink($rel, $href); |
||
87 | }, array_filter($href)); |
||
88 | |||
89 | if (empty($href)) { |
||
90 | return; |
||
91 | } |
||
92 | } elseif (!empty($href)) { |
||
93 | $href = static::getHtmlLink($rel, $href); |
||
94 | } else { |
||
95 | return; |
||
96 | } |
||
97 | |||
98 | $this[$rel] = $href; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Adds an array of metas. |
||
103 | * |
||
104 | * @param array $metas |
||
105 | */ |
||
106 | protected function addMetas(array $metas) |
||
107 | { |
||
108 | foreach ($metas as $name => $content) { |
||
109 | if (!empty($content)) { |
||
110 | $this->addMeta($name, $content); |
||
111 | } |
||
112 | } |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Adds an array of links. |
||
117 | * |
||
118 | * @param array $links |
||
119 | */ |
||
120 | protected function addLinks(array $links) |
||
121 | { |
||
122 | foreach ($links as $rel => $href) { |
||
123 | if (!empty($href)) { |
||
124 | $this->addLink($rel, $href); |
||
125 | } |
||
126 | } |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Escapes the value of an attribute. |
||
131 | * |
||
132 | * @param string $value |
||
133 | * |
||
134 | * @return string |
||
135 | */ |
||
136 | protected static function escape($value) |
||
137 | { |
||
138 | return htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Generates the html code of a link |
||
143 | * |
||
144 | * @param string $rel |
||
145 | * @param string $href |
||
146 | */ |
||
147 | protected static function getHtmlLink($rel, $href) |
||
148 | { |
||
149 | return '<link rel="'.static::escape($rel).'" href="'.static::escape($href).'">'; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Generates the html code of a meta |
||
154 | * |
||
155 | * @param string $name |
||
156 | * @param string $content |
||
157 | */ |
||
158 | protected static function getHtmlMeta($name, $content) |
||
159 | { |
||
160 | $content = static::trim($name, $content); |
||
161 | |||
162 | return '<meta '.static::META_ATTRIBUTE_NAME.'="'.static::META_NAME_PREFIX.static::escape($name).'" content="'.static::escape($content).'">'; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Filters attribute values to trim by length. |
||
167 | * |
||
168 | * @param string $name |
||
169 | * @param string $content |
||
170 | * |
||
171 | * @return string |
||
172 | */ |
||
173 | protected static function trim($name, $content) |
||
174 | { |
||
175 | $limit = isset(static::$characterLimits[$name]) ? static::$characterLimits[$name] : null; |
||
176 | |||
177 | if ($limit && mb_strlen($content) > $limit) { |
||
178 | $content = mb_substr($content, 0, $limit - 3).'...'; |
||
179 | } |
||
180 | |||
181 | return $content; |
||
182 | } |
||
183 | } |
||
184 |
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.