Completed
Push — master ( 806351...b146b2 )
by Sebastian
03:20
created

StyleSheet::loadLocalesMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * citeproc-php
4
 *
5
 * @link        http://github.com/seboettg/citeproc-php for the source repository
6
 * @copyright   Copyright (c) 2016 Sebastian Böttger.
7
 * @license     https://opensource.org/licenses/MIT
8
 */
9
10
namespace Seboettg\CiteProc;
11
12
use Seboettg\CiteProc\Exception\CiteProcException;
13
14
/**
15
 * Class StyleSheet
16
 *
17
 * Helper class for loading CSL styles and CSL locales
18
 *
19
 * @package Seboettg\CiteProc
20
 * @author Sebastian Böttger <[email protected]>
21
 */
22
class StyleSheet
23
{
24
25
    /**
26
     * Loads xml formatted CSL stylesheet of a given stylesheet name, e.g. "american-physiological-society" for
27
     * apa style.
28
     *
29
     * See in styles folder (which is included as git submodule) for all available style sheets
30
     *
31
     * @param string $styleName e.g. "american-physiological-society" for apa
32
     * @return string
33
     * @throws CiteProcException
34
     */
35
    public static function loadStyleSheet($styleName)
36
    {
37
        $stylesPath = self::vendorPath() . "/citation-style-language/styles-distribution/";
38
        return file_get_contents($stylesPath . $styleName . '.csl');
39
    }
40
41
    /**
42
     * Loads xml formatted locales of given language key
43
     *
44
     * @param string $langKey e.g. "en-US", or "de-CH"
45
     * @return string
46
     * @throws CiteProcException
47
     */
48
    public static function loadLocales($langKey)
49
    {
50
        $localesPath = self::vendorPath() . "/citation-style-language/locales/";
51
        $localeFile = $localesPath . "locales-" . $langKey . '.xml';
52
        if (file_exists($localeFile)) {
53
            $data = file_get_contents($localeFile);
54
        } else {
55
            $metadata = self::loadLocalesMetadata();
56
            if (!empty($metadata->{'primary-dialects'}->{$langKey})) {
57
                $data = file_get_contents($localesPath . "locales-" . $metadata->{'primary-dialects'}->{$langKey} . '.xml');
58
            }
59
        }
60
61
        return $data;
0 ignored issues
show
Bug introduced by
The variable $data does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
62
    }
63
64
    public static function loadLocalesMetadata()
65
    {
66
        $localesMetadataPath = self::vendorPath() . "/citation-style-language/locales/locales.json";
67
        return json_decode(file_get_contents($localesMetadataPath));
68
    }
69
70
    /**
71
     * @return bool|string
72
     * @throws CiteProcException
73
     */
74
    private static function vendorPath()
75
    {
76
        include_once __DIR__ . '/../../../vendorPath.php';
77
        if (!($vendorPath = vendorPath())) {
78
            // @codeCoverageIgnoreStart
79
            throw new CiteProcException('vendor path not found. Use composer to initialize your project');
80
            // @codeCoverageIgnoreEnd
81
        }
82
        return $vendorPath;
83
    }
84
}
85