JobQueueExtension   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 92
Duplicated Lines 21.74 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 4
dl 20
loc 92
ccs 0
cts 72
cp 0
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getTests() 0 6 1
A getFunctions() 0 6 1
A getFilters() 0 7 1
A formatArgs() 0 22 4
A isLinkable() 0 10 3
A generatePath() 10 10 3
A getLinkname() 10 10 3
A getName() 0 4 1

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 JMS\JobQueueBundle\Twig;
4
5
class JobQueueExtension extends \Twig\Extension\AbstractExtension
6
{
7
    private $linkGenerators = array();
8
9
    public function __construct(array $generators = array())
10
    {
11
        $this->linkGenerators = $generators;
12
    }
13
14
    public function getTests()
15
    {
16
        return array(
17
            new \Twig\TwigTest('jms_job_queue_linkable', array($this, 'isLinkable'))
18
        );
19
    }
20
21
    public function getFunctions()
22
    {
23
        return array(
24
            new \Twig\TwigFunction('jms_job_queue_path', array($this, 'generatePath'), array('is_safe' => array('html' => true)))
25
        );
26
    }
27
28
    public function getFilters()
29
    {
30
        return array(
31
            new \Twig\TwigFilter('jms_job_queue_linkname', array($this, 'getLinkname')),
32
            new \Twig\TwigFilter('jms_job_queue_args', array($this, 'formatArgs'))
33
        );
34
    }
35
36
    public function formatArgs(array $args, $maxLength = 60)
37
    {
38
        $str = '';
39
        $first = true;
40
        foreach ($args as $arg) {
41
            $argLength = strlen($arg);
42
43
            if (!$first) {
44
                $str .= ' ';
45
            }
46
            $first = false;
47
48
            if (strlen($str) + $argLength > $maxLength) {
49
                $str .= substr($arg, 0, $maxLength - strlen($str) - 4) . '...';
50
                break;
51
            }
52
53
            $str .= escapeshellarg($arg);
54
        }
55
56
        return $str;
57
    }
58
59
    public function isLinkable($entity)
60
    {
61
        foreach ($this->linkGenerators as $generator) {
62
            if ($generator->supports($entity)) {
63
                return true;
64
            }
65
        }
66
67
        return false;
68
    }
69
70 View Code Duplication
    public function generatePath($entity)
0 ignored issues
show
Duplication introduced by
This method 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...
71
    {
72
        foreach ($this->linkGenerators as $generator) {
73
            if ($generator->supports($entity)) {
74
                return $generator->generate($entity);
75
            }
76
        }
77
78
        throw new \RuntimeException(sprintf('The entity "%s" has no link generator.', get_class($entity)));
79
    }
80
81 View Code Duplication
    public function getLinkname($entity)
0 ignored issues
show
Duplication introduced by
This method 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...
82
    {
83
        foreach ($this->linkGenerators as $generator) {
84
            if ($generator->supports($entity)) {
85
                return $generator->getLinkname($entity);
86
            }
87
        }
88
89
        throw new \RuntimeException(sprintf('The entity "%s" has no link generator.', get_class($entity)));
90
    }
91
92
    public function getName()
93
    {
94
        return 'jms_job_queue';
95
    }
96
}
97