1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Disqus Helper package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Nikola Posa <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For full copyright and license information, please refer to the LICENSE file, |
8
|
|
|
* located at the package root folder. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace DisqusHelper\Widget; |
12
|
|
|
|
13
|
|
|
use DisqusHelper\Code; |
14
|
|
|
use DisqusHelper\Exception\RuntimeException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Comments count widget. |
18
|
|
|
* |
19
|
|
|
* @author Nikola Posa <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
final class CommentsCountWidget extends BaseWidget |
22
|
|
|
{ |
23
|
|
|
const SCRIPT_NAME = 'count.js'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private static $defaultOptions = [ |
29
|
|
|
'url' => null, |
30
|
|
|
'label' => null, |
31
|
|
|
'as_link' => true, |
32
|
|
|
'identifier' => null, |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
public function render(array $options = []) : string |
36
|
|
|
{ |
37
|
|
|
$options = array_merge(self::$defaultOptions, $options); |
38
|
|
|
|
39
|
|
|
$label = htmlspecialchars((string) $options['label'], ENT_QUOTES, 'UTF-8'); |
40
|
|
|
|
41
|
|
|
$attribs = []; |
42
|
|
|
|
43
|
|
|
if (isset($options['identifier'])) { |
44
|
|
|
$attribs['data-disqus-identifier'] = $options['identifier']; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ($options['as_link']) { |
48
|
|
|
if (empty($options['url'])) { |
49
|
|
|
throw new RuntimeException("URL option is missing for the Comments count widget"); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$url = $options['url'] . '#disqus_thread'; |
53
|
|
|
|
54
|
|
|
return '<a href="' . $url . '"' |
55
|
|
|
. ' ' . $this->htmlAttribsToString($attribs) . '>' |
56
|
|
|
. $label |
57
|
|
|
. '</a>'; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (!empty($options['url'])) { |
61
|
|
|
$attribs['data-disqus-url'] = $options['url']; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return '<span class="disqus-comment-count" data-disqus-identifier="article_1_identifier"' |
65
|
|
|
. ' ' . $this->htmlAttribsToString($attribs) . '>' |
66
|
|
|
. $label |
67
|
|
|
. '</span>'; |
68
|
|
|
|
69
|
|
|
return $html; |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function visit(Code $code) : Code |
73
|
|
|
{ |
74
|
|
|
$code->addScriptFile(self::SCRIPT_NAME); |
75
|
|
|
|
76
|
|
|
return $code; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.