Passed
Push — master ( 432b63...d5390f )
by Andrea
37:11 queued 18:11
created

AssetExtension   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 61.53%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 30
ccs 8
cts 13
cp 0.6153
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A assetExists() 0 16 3
A getFunctions() 0 3 1
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