Completed
Pull Request — master (#13)
by
unknown
01:49
created

MetaBase   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
generateTags() 0 1 ?
A addMeta() 0 6 1
A addLink() 0 4 1
A escape() 0 4 1
A filterAttribute() 0 11 4
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
    protected $page;
14
    protected $prefix;
15
    protected $characterLimits = [];
16
17
    /**
18
     * Constructor.
19
     *
20
     * @param Page $page
21
     */
22
    public function __construct(Page $page)
23
    {
24
        $this->page = $page;
25
        $this->generateTags();
26
    }
27
28
    /**
29
     * Generate all tags.
30
     *
31
     * @return array
32
     */
33
    abstract protected function generateTags();
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function addMeta($name, $content)
39
    {
40
        $content = $this->filterAttribute($name, $content);
41
42
        $this[$name] = '<meta name="'.$this->prefix.static::escape($name).'" content="'.static::escape($content).'">';
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function addLink($rel, $href)
49
    {
50
        $this[$rel] = '<link rel="'.$this->prefix.static::escape($rel).'" href="'.static::escape($href).'">';
51
    }
52
53
    /**
54
     * Escapes the value of an attribute.
55
     *
56
     * @param string $value
57
     *
58
     * @return string
59
     */
60
    protected static function escape($value)
61
    {
62
        return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
63
    }
64
65
    /**
66
     * Filters attribute values to trim by length
67
     *
68
     * @param string $value
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
69
     *
70
     * @return string
71
     */
72
    protected function filterAttribute($name, $content)
73
    {
74
        $limit = isset($this->characterLimits[$name]) ? $this->characterLimits[$name] : null;
75
76
        if($limit && strlen($content) > $limit)
77
        {
78
            $content = substr($content, 0, $limit - 3).'...';
79
        }
80
81
        return $content;
82
    }
83
}
84