Completed
Push — v4.0-dev ( 6e38a6...59b8ca )
by Oscar
04:48
created

YamlDictionary::fromString()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 14
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 14
loc 14
rs 9.2
cc 4
eloc 7
nc 4
nop 3
1
<?php
2
3
namespace Gettext\Extractors;
4
5
use Gettext\Translations;
6
use Symfony\Component\Yaml\Yaml;
7
8
/**
9
 * Class to get gettext strings from plain json.
10
 */
11 View Code Duplication
class YamlDictionary extends Extractor implements ExtractorInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public static function fromString($string, Translations $translations = null, $file = '')
17
    {
18
        if ($translations === null) {
19
            $translations = new Translations();
20
        }
21
22
        if (($entries = Yaml::parse($string))) {
23
            foreach ($entries as $original => $translation) {
0 ignored issues
show
Bug introduced by
The expression $entries of type array|string|object<stdClass> 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...
24
                $translations->insert(null, $original)->setTranslation($translation);
25
            }
26
        }
27
28
        return $translations;
29
    }
30
}
31