Completed
Push — master ( d5f725...837ac6 )
by Oscar
11s
created

MetaBase::filterAttribute()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.2
cc 4
eloc 5
nc 4
nop 2
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