Completed
Push — master ( 192e2c...b945f9 )
by Christophe
02:46
created

GherkinFileLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Behat Gherkin.
5
 * (c) Konstantin Kudryashov <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Behat\Gherkin\Loader;
12
13
use Behat\Gherkin\Cache\CacheInterface;
14
use Behat\Gherkin\Node\FeatureNode;
15
use Behat\Gherkin\Parser;
16
17
/**
18
 * Gherkin *.feature files loader.
19
 *
20
 * @author Konstantin Kudryashov <[email protected]>
21
 */
22
class GherkinFileLoader extends AbstractFileLoader
23
{
24
    protected $parser;
25
    protected $cache;
26
27
    /**
28
     * Initializes loader.
29
     *
30
     * @param Parser         $parser Parser
31
     * @param CacheInterface $cache  Cache layer
32
     */
33 5
    public function __construct(Parser $parser, CacheInterface $cache = null)
34
    {
35 5
        $this->parser = $parser;
36 5
        $this->cache = $cache;
37 5
    }
38
39
    /**
40
     * Sets cache layer.
41
     *
42
     * @param CacheInterface $cache Cache layer
43
     */
44 2
    public function setCache(CacheInterface $cache)
45
    {
46 2
        $this->cache = $cache;
47 2
    }
48
49
    /**
50
     * Checks if current loader supports provided resource.
51
     *
52
     * @param mixed $path Resource to load
53
     *
54
     * @return Boolean
55
     */
56 2
    public function supports($path)
57
    {
58 2
        return is_string($path)
59 2
        && is_file($absolute = $this->findAbsolutePath($path))
60 2
        && 'feature' === pathinfo($absolute, PATHINFO_EXTENSION);
61
    }
62
63
    /**
64
     * Loads features from provided resource.
65
     *
66
     * @param string $path Resource to load
67
     *
68
     * @return FeatureNode[]
69
     */
70 4
    public function load($path)
71
    {
72 4
        $path = $this->findAbsolutePath($path);
73
74 4
        if ($this->cache) {
75 2
            if ($this->cache->isFresh($path, filemtime($path))) {
0 ignored issues
show
Security Bug introduced by
It seems like $path defined by $this->findAbsolutePath($path) on line 72 can also be of type false; however, Behat\Gherkin\Cache\CacheInterface::isFresh() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
76 1
                $feature = $this->cache->read($path);
0 ignored issues
show
Security Bug introduced by
It seems like $path defined by $this->findAbsolutePath($path) on line 72 can also be of type false; however, Behat\Gherkin\Cache\CacheInterface::read() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
77 2
            } elseif (null !== $feature = $this->parseFeature($path)) {
0 ignored issues
show
Security Bug introduced by
It seems like $path defined by $this->findAbsolutePath($path) on line 72 can also be of type false; however, Behat\Gherkin\Loader\Ghe...eLoader::parseFeature() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
78 1
                $this->cache->write($path, $feature);
0 ignored issues
show
Security Bug introduced by
It seems like $path defined by $this->findAbsolutePath($path) on line 72 can also be of type false; however, Behat\Gherkin\Cache\CacheInterface::write() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
79 1
            }
80 2
        } else {
81 2
            $feature = $this->parseFeature($path);
0 ignored issues
show
Security Bug introduced by
It seems like $path defined by $this->findAbsolutePath($path) on line 72 can also be of type false; however, Behat\Gherkin\Loader\Ghe...eLoader::parseFeature() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
82
        }
83
84 4
        return null !== $feature ? array($feature) : array();
85
    }
86
87
    /**
88
     * Parses feature at provided absolute path.
89
     *
90
     * @param string $path Feature path
91
     *
92
     * @return FeatureNode
93
     */
94 3
    protected function parseFeature($path)
95
    {
96 3
        $filename = $this->findRelativePath($path);
97 3
        $content = file_get_contents($path);
98 3
        $feature = $this->parser->parse($content, $filename);
99
100 3
        return $feature;
101
    }
102
}
103