Passed
Push — master ( ead1f2...80923f )
by Andrea
15:57
created

AssetExtension   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 53.33%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 35
ccs 8
cts 15
cp 0.5333
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A assetExists() 0 16 3
A getName() 0 3 1
A getFunctions() 0 3 1
1
<?php
2
3
namespace Cdf\BiCoreBundle\Twig\Extension;
4
5
class AssetExtension extends \Twig_Extension
6
{
7
    private $projectpath;
8
9 26
    public function __construct($projectpath)
10
    {
11 26
        $this->projectpath = $projectpath;
12 26
    }
13
14
    public function getFunctions()
15
    {
16
        return [new \Twig_SimpleFunction('asset_exists', [$this, 'assetExists'], ['is_safe' => ['html']])];
17
    }
18
19 11
    public function assetExists($path)
20
    {
21 11
        $publicRoot = realpath($this->projectpath.'/public/').DIRECTORY_SEPARATOR;
22 11
        $toCheck = $publicRoot.$path;
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...
23
24
        // check if the file exists
25 11
        if (!is_file($toCheck)) {
26 11
            return false;
27
        }
28
29
        // check if file is well contained in web/ directory (prevents ../ in paths)
30
        if (0 !== strncmp($publicRoot, $toCheck, strlen($publicRoot))) {
31
            return false;
32
        }
33
34
        return true;
35
    }
36
37
    public function getName()
38
    {
39
        return 'asset_exists';
40
    }
41
}
42