Completed
Push — master ( aa9f24...ead1f2 )
by Andrea
20:48 queued 17:21
created

AssetExtension::assetExists()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.4746

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 16
ccs 5
cts 8
cp 0.625
crap 3.4746
rs 10
c 0
b 0
f 0
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 1
    public function getFunctions()
15
    {
16 1
        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