Completed
Pull Request — develop (#128)
by
unknown
13:33
created

Properties::parseString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Noodlehaus\Parser;
4
5
use Noodlehaus\Exception\ParseException;
6
7
/**
8
 * Properties parser.
9
 *
10
 * @package    Config
11
 * @author     Jesus A. Domingo <[email protected]>
12
 * @author     Hassan Khan <[email protected]>
13
 * @author     Filip Š <[email protected]>
14
 * @author     Mark de Groot <[email protected]>
15
 * @link       https://github.com/noodlehaus/config
16
 * @license    MIT
17
 */
18
class Properties implements ParserInterface
19
{
20
    /**
21
     * {@inheritdoc}
22
     * Parses a Properties file as an array.
23
     */
24
    public function parseFile($filename)
25
    {
26
        return $this->parse(file_get_contents($filename));
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     * Parses a Properties string as an array.
32
     */
33
    public function parseString($config)
34
    {
35
        return $this->parse($config);
36
    }
37
38
    private function parse($txtProperties)
39
    {
40
        $result = [];
41
42
        // first remove all escaped whitespace characters:
43
        $txtProperties = preg_replace('/(?<!\\\\)\\\\[\r\n\t\f\v][ \r]*/', '', $txtProperties);
44
45
        // next split all lines not starting with # or ! on characters = or : (unless escaped):
46
        preg_match_all('/^([^#!].*)(?<!\\\\)[=:](.*)$/mU', $txtProperties, $matches, PREG_SET_ORDER, 0);
47
48
        foreach ($matches as $match) {
0 ignored issues
show
Bug introduced by
The expression $matches of type null|array<integer,array<integer,string>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
49
            $result[trim(stripslashes($match[1]))] = trim(stripslashes($match[2]));
50
        }
51
52
        return $result;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public static function getSupportedExtensions()
59
    {
60
        return ['properties'];
61
    }
62
}
63