Completed
Pull Request — master (#3)
by Nikola
32:48 queued 50s
created

CommentsCountWidget   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 58
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B render() 0 36 5
A visit() 0 6 1
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;
0 ignored issues
show
Unused Code introduced by
return $html; does not seem to be reachable.

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 or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

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.

Loading history...
70
    }
71
72
    public function visit(Code $code) : Code
73
    {
74
        $code->addScriptFile(self::SCRIPT_NAME);
75
76
        return $code;
77
    }
78
}
79