Completed
Pull Request — master (#37)
by San
10:18
created

Extension   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 54
Duplicated Lines 11.11 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 0
cbo 0
dl 6
loc 54
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getFunctions() 0 6 1
A color() 0 6 1
A hslToRgb() 0 14 3
B hue2rgb() 6 19 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
namespace PommProject\SymfonyBridge\Twig;
4
5
final class Extension extends \Twig\Extension\AbstractExtension
6
{
7
    public function getFunctions()
8
    {
9
        return array(
10
            new \Twig\TwigFunction('color', array($this, 'color')),
11
        );
12
    }
13
14
    /*
15
     * https://stackoverflow.com/questions/2353211/hsl-to-rgb-color-conversion
16
     */
17
    public function color($percent)
18
    {
19
        $hue = (100 - $percent) * 1.2 / 360;
20
        $rgb = $this->hslToRgb($hue, .9, .4);
21
        return sprintf("rgb(%d, %d, %d)", $rgb[0], $rgb[1], $rgb[2]);
22
    }
23
24
    private function hslToRgb($h, $s, $l)
25
    {
26
        $r = $g = $b = $l;
27
28
        if ($s !== 0) {
29
            $q = $l < 0.5 ? $l * (1 + $s) : $l + $s - $l * $s;
30
            $p = 2 * $l - $q;
31
            $r = $this->hue2rgb($p, $q, $h + 1 / 3);
32
            $g = $this->hue2rgb($p, $q, $h);
33
            $b = $this->hue2rgb($p, $q, $h - 1 / 3);
34
        }
35
36
        return array($r * 255, $g * 255, $b * 255);
37
    }
38
39
    private function hue2rgb($p, $q, $t)
40
    {
41
        if ($t < 0) {
42
            $t += 1;
43
        }
44
        if ($t > 1) {
45
            $t -= 1;
46
        }
47 View Code Duplication
        if ($t < 1 / 6) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
48
            return $p + ($q - $p) * 6 * $t;
49
        }
50
        if ($t < 1 / 2) {
51
            return $q;
52
        }
53 View Code Duplication
        if ($t < 2 / 3) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
54
            return $p + ($q - $p) * (2 / 3 - $t) * 6;
55
        }
56
        return $p;
57
    }
58
}
59