Passed
Push — master ( 2a39ec...3b6c56 )
by Andrea
03:42
created

PermessiTwigExtension   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 74.07%

Importance

Changes 0
Metric Value
wmc 9
eloc 30
dl 0
loc 59
ccs 20
cts 27
cp 0.7407
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getControllerName() 0 7 1
A getFilters() 0 4 1
A getFunctions() 0 3 1
A singoloPermesso() 0 21 5
1
<?php
2
3
namespace Fi\CoreBundle\Twig\Extension;
4
5
use Symfony\Component\DependencyInjection\ContainerInterface;
6
use Symfony\Component\HttpFoundation\RequestStack;
7
8
class PermessiTwigExtension extends \Twig_Extension
9
{
10
11
    protected $requeststack;
12
    protected $container;
13
14 35
    public function __construct(ContainerInterface $container, RequestStack $request_stack)
15
    {
16 35
        $this->container = $container;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
17 35
        $this->requeststack = $request_stack;
18 35
    }
19
20
    /**
21
     * Get current controller name.
22
     */
23 6
    public function getControllerName()
24
    {
25 6
        $pattern = "#Controller\\\([a-zA-Z]*)Controller#";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal #Controller\\\([a-zA-Z]*)Controller# does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
26 6
        $matches = array();
27 6
        preg_match($pattern, $this->requeststack->getCurrentRequest()->get('_controller'), $matches);
28
29 6
        return $matches[1];
30
    }
31
32
    public function getFunctions()
33
    {
34
        return array(
35
                //'permesso' => new \Twig_Function_Method($this, 'controllaPermesso'),
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
36
        );
37
    }
38
39
    public function getFilters()
40
    {
41
        return array(
42
            new \Twig_SimpleFilter('permesso', array($this, 'singoloPermesso')),
43
        );
44
    }
45
46 6
    public function singoloPermesso($lettera)
47
    {
48 6
        $gestionepermessi = $this->container->get("ficorebundle.gestionepermessi");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal ficorebundle.gestionepermessi does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
49
50 6
        $parametri = array();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
51 6
        $parametri['modulo'] = $this->getControllerName();
52 6
        switch ($lettera) {
53 6
            case 'c':
54
                return $gestionepermessi->creare($parametri);
55
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
56 6
            case 'r':
57
                return $gestionepermessi->leggere($parametri);
58
                break;
59 6
            case 'u':
60 6
                return $gestionepermessi->aggiornare($parametri);
61
                break;
62 6
            case 'd':
63 6
                return $gestionepermessi->cancellare($parametri);
64
                break;
65
            default:
66
                break;
67
        }
68
    }
69
}
70