Completed
Push — master ( 040ef3...d11a3f )
by Andrea
59:12 queued 55:09
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\AbstractExtension
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']])];
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated: since Twig 2.7, use "Twig\TwigFunction" instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

16
        return [/** @scrutinizer ignore-deprecated */ new \Twig_SimpleFunction('asset_exists', [$this, 'assetExists'], ['is_safe' => ['html']])];
Loading history...
17
    }
18
19 1
    public function assetExists($path)
20
    {
21 1
        $publicRoot = realpath($this->projectpath.'/public/').DIRECTORY_SEPARATOR;
22 1
        $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 1
        if (!is_file($toCheck)) {
26 1
            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