Completed
Pull Request — master (#516)
by Michael
10:59
created

HTMLPurifier_AttrTransform_TargetNoopener   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 25
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 25
loc 25
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
B transform() 16 16 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
// must be called POST validation
4
5
/**
6
 * Adds rel="noopener" to any links which target a different window
7
 * than the current one.  This is used to prevent malicious websites
8
 * from silently replacing the original window, which could be used
9
 * to do phishing.
10
 * This transform is controlled by %HTML.TargetNoopener.
11
 */
12 View Code Duplication
class HTMLPurifier_AttrTransform_TargetNoopener extends HTMLPurifier_AttrTransform
0 ignored issues
show
Duplication introduced by
This class 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.

Loading history...
13
{
14
    /**
15
     * @param array $attr
16
     * @param HTMLPurifier_Config $config
17
     * @param HTMLPurifier_Context $context
18
     * @return array
19
     */
20
    public function transform($attr, $config, $context)
21
    {
22
        if (isset($attr['rel'])) {
23
            $rels = explode(' ', $attr['rel']);
24
        } else {
25
            $rels = array();
26
        }
27
        if (isset($attr['target']) && !in_array('noopener', $rels)) {
28
            $rels[] = 'noopener';
29
        }
30
        if (!empty($rels) || isset($attr['rel'])) {
31
            $attr['rel'] = implode(' ', $rels);
32
        }
33
34
        return $attr;
35
    }
36
}
37
38