Completed
Pull Request — master (#112)
by Christophe
02:27
created

YamlFileLoader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 53
wmc 5
lcom 1
cbo 4
ccs 25
cts 25
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A load() 0 22 1
A supports() 0 6 3
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\Node\FeatureNode;
14
use Symfony\Component\Yaml\Yaml;
15
16
/**
17
 * Yaml files loader.
18
 *
19
 * @author Konstantin Kudryashov <[email protected]>
20
 */
21
class YamlFileLoader extends AbstractFileLoader
22
{
23
    private $loader;
24
25 35
    public function __construct()
26
    {
27 35
        $this->loader = new ArrayLoader();
28 35
    }
29
30
    /**
31
     * Checks if current loader supports provided resource.
32
     *
33
     * @param mixed $path Resource to load
34
     *
35
     * @return Boolean
36
     */
37 1
    public function supports($path)
38
    {
39 1
        return is_string($path)
40 1
            && is_file($absolute = $this->findAbsolutePath($path))
41 1
            && 'yml' === pathinfo($absolute, PATHINFO_EXTENSION);
42
    }
43
44
    /**
45
     * Loads features from provided resource.
46
     *
47
     * @param string $path Resource to load
48
     *
49
     * @return FeatureNode[]
50
     */
51 34
    public function load($path)
52
    {
53 34
        $path = $this->findAbsolutePath($path);
54 34
        $hash = Yaml::parse(file_get_contents($path));
55
56 34
        $features = $this->loader->load($hash);
57 34
        $filename = $this->findRelativePath($path);
0 ignored issues
show
Security Bug introduced by
It seems like $path defined by $this->findAbsolutePath($path) on line 53 can also be of type false; however, Behat\Gherkin\Loader\Abs...der::findRelativePath() 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...
58
59 34
        return array_map(function (FeatureNode $feature) use ($filename) {
60 34
            return new FeatureNode(
61 34
                $feature->getTitle(),
62 34
                $feature->getDescription(),
63 34
                $feature->getTags(),
64 34
                $feature->getBackground(),
65 34
                $feature->getScenarios(),
66 34
                $feature->getKeyword(),
67 34
                $feature->getLanguage(),
68 34
                $filename,
69 34
                $feature->getLine()
70 34
            );
71 34
        }, $features);
72
    }
73
}
74