Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractYamlReader
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
4 / 4
9
100.00% covered (success)
100.00%
1 / 1
 __construct
n/a
0 / 0
n/a
0 / 0
0
 getDefaultConfigurationPath
n/a
0 / 0
n/a
0 / 0
0
 instance
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
5
 resetInstances
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 loadYaml
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 parseSimpleArray
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace WsdlToPhp\PackageGenerator\ConfigurationReader;
6
7use Symfony\Component\Yaml\Parser;
8
9abstract class AbstractYamlReader
10{
11    protected static array $instances;
12
13    protected string $filename;
14
15    abstract protected function __construct(string $filename);
16
17    abstract public static function getDefaultConfigurationPath(): string;
18
19    public static function instance(?string $filename = null): self
20    {
21        $loadFilename = empty($filename) ? static::getDefaultConfigurationPath() : $filename;
22        if (empty($loadFilename) || !is_file($loadFilename)) {
23            throw new \InvalidArgumentException(sprintf('Unable to locate file "%s"', $loadFilename), __LINE__);
24        }
25
26        $key = sprintf('%s_%s', static::class, $loadFilename);
27        if (!isset(self::$instances[$key])) {
28            self::$instances[$key] = new static($loadFilename);
29        }
30
31        return self::$instances[$key];
32    }
33
34    /**
35     * For tests purpose only!
36     */
37    public static function resetInstances(): void
38    {
39        self::$instances = [];
40    }
41
42    protected function loadYaml(string $filename)
43    {
44        $ymlParser = new Parser();
45
46        return $ymlParser->parse(file_get_contents($filename));
47    }
48
49    protected function parseSimpleArray(string $filename, string $mainKey): array
50    {
51        $values = $this->loadYaml($filename);
52        if (!array_key_exists($mainKey, $values)) {
53            throw new \InvalidArgumentException(sprintf('Unable to find section "%s" in "%s"', $mainKey, $filename), __LINE__);
54        }
55
56        return $values[$mainKey];
57    }
58}