Completed
Push — feature/issue-45 ( 7692eb...be53bc )
by Mikaël
26:59
created

ReservedKeywords::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\ConfigurationReader;
4
5
class ReservedKeywords extends AbstractYamlReader
6
{
7
    /**
8
     * @var string
9
     */
10
    const MAIN_KEY = 'reserved_keywords';
11
    /**
12
     * @var string
13
     */
14
    const CASE_SENSITIVE_KEY = 'case_sensitive';
15
    /**
16
     * @var string
17
     */
18
    const CASE_INSENSITIVE_KEY = 'case_insensitive';
19
    /**
20
     * List of PHP reserved keywords from config file
21
     * @var array
22
     */
23
    protected $keywords;
24
    /**
25
     * @param string $filename
26
     */
27 12
    protected function __construct($filename)
28
    {
29 12
        $this->keywords = array();
30 12
        $this->parseReservedKeywords($filename);
31 8
    }
32
    /**
33
     * @param string $filename
34
     * @return ReservedKeywords
35
     */
36 12
    protected function parseReservedKeywords($filename)
37
    {
38 12
        $allKeywords = $this->parseSimpleArray($filename, self::MAIN_KEY);
39 8
        $caseSensitiveKeywords = $allKeywords[self::CASE_SENSITIVE_KEY];
40 8
        $caseInsensitiveKeywords = $allKeywords[self::CASE_INSENSITIVE_KEY];
41
42 8
        foreach ($caseInsensitiveKeywords as $index => $keyword) {
43 8
            $caseInsensitiveKeywords[$index] = strtolower($keyword);
44 6
        }
45
46 8
        $this->keywords = array(
47 8
            self::CASE_SENSITIVE_KEY => $caseSensitiveKeywords,
48 8
            self::CASE_INSENSITIVE_KEY => $caseInsensitiveKeywords
49 6
        );
50 8
        return $this;
51
    }
52
    /**
53
     * @param string options's file to parse
54
     * @return ReservedKeywords
55
     */
56 680
    public static function instance($filename = null)
57
    {
58 680
        return parent::instance(empty($filename) ? dirname(__FILE__) . '/../resources/config/reserved_keywords.yml' : $filename);
59
    }
60
    /**
61
     * @param string $keyword
62
     * @return bool
63
     */
64 672
    public function is($keyword)
65
    {
66 672
        return in_array($keyword, $this->keywords[self::CASE_SENSITIVE_KEY], true)
67 672
            || in_array(strtolower($keyword), $this->keywords[self::CASE_INSENSITIVE_KEY], true);
68
    }
69
}
70