Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

src/Kunstmaan/FixturesBundle/Parser/Spec/Range.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\FixturesBundle\Parser\Spec;
4
5
use Kunstmaan\FixturesBundle\Loader\Fixture;
6
7
class Range implements SpecParserInterface
8
{
9
    const REGEX = '/{(\d)+\.\.(\d)+}$/';
10
11
    /**
12
     * Check if this parser is applicable
13
     *
14
     * @return bool
0 ignored issues
show
Should the return type not be integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
15
     */
16
    public function canParse($value)
17
    {
18
        return preg_match(self::REGEX, $value);
19
    }
20
21
    /**
22
     * Parse provided value into new data
23
     *
24
     * @param $spec
25
     * @param $fixture
26
     * @param $fixtures
27
     *
28
     * @return mixed
0 ignored issues
show
Consider making the return type a bit more specific; maybe use null|array.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
29
     *
30
     * @throws \Exception
31
     */
32
    public function parse(Fixture $fixture, array $fixtures, $spec)
33
    {
34
        preg_match(self::REGEX, $spec, $ranges);
35
        if (empty($ranges)) {
36
            return;
37
        }
38
39
        $range = $ranges[0];
40
        $name = substr($spec, 0, strpos($spec, $range));
41
42
        preg_match_all('/\d+/', $range, $keys);
43
        if (empty($keys)) {
44
            return;
45
        }
46
47
        $start = $keys[0][0];
48
        $end = $keys[0][1];
49
50
        if ($start > $end) {
51
            throw new \Exception('Range start can not be biggen than range end for fixture ' . $spec);
52
        }
53
54
        for ($i = $start; $i <= $end; ++$i) {
55
            $newFixture = clone $fixture;
56
            $newFixture->setName($name . $i);
57
            $newFixture->setSpec($i);
58
            $fixtures[$name . $i] = $newFixture;
59
        }
60
61
        return $fixtures;
62
    }
63
}
64